-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.html
More file actions
172 lines (141 loc) · 4.88 KB
/
example.html
File metadata and controls
172 lines (141 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<html>
<head>
<title>RedDwarf-JS Example</title>
<script src="/static/Orbited.js"></script>
<script>
document.domain = document.domain;
document.port = document.port;
Orbited.settings.hostname = 'localhost';
Orbited.settings.port = 9000;
</script>
<script src="red_dwarf.js"></script>
<script>
var client;
function login(userName, password) {
client = new RedDwarf.SimpleClient('localhost', 1139);
client.registerListener({
on_loginSuccess: function(event, client) { justLoggedIn( ); },
on_loginFailure: function(event, client) { justFailedToLogIn( ); },
on_logout: function(event, client) { justLoggedOut( ); },
on_sessionMessage: function(event, client) { showSessionMessage( event.message ); },
on_channelMessage: function(event, client) { showChannelMessage( event.channel, event.message ); },
on_channelJoin: function(event, client) { justJoinedChannel( event.channel ); },
on_channelLeave: function(event, client) { justLeftChannel( event.channel ); }
});
client.login(userName || "guest-123", password || "");
}
/*
The rest of this file is probably not that interesting. The main stuff you need to see
is the call to registerListener (above) and this list of available methods:
client.login(username, password)
client.logout()
client.sessionSend(message)
client.channelSend(channel, message)
client.getChannelWithID(id)
channel.id()
channel.name()
*/
function logout() {
client.logout();
client = null;
clearChannelSelector();
}
function clearChannelSelector() {
var channelSelector = document.getElementById("channelSelector");
channelSelector.selectedIndex = 0;
for (var i = channelSelector.options.length - 1; i >= 1; --i) {
channelSelector.removeChild(channelSelector.options[i]);
}
}
function sendMessage(msg) {
var channelSelector = document.getElementById("channelSelector");
if (channelSelector.value === "_default_") {
client.sessionSend(msg);
showMessage(msg, "#0000aa");
} else {
var channel = client.getChannelWithID(parseInt(channelSelector.value));
client.channelSend(channel, msg);
showMessage("[" + channel + "]: " + msg, "#0000aa");
}
}
function showSessionMessage(msg ) { showMessage(msg, "#00aa00"); }
function showChannelMessage(channel, msg) { showMessage("[" + channel + "]: " + msg, "#00aa00"); }
function justLoggedIn() { showMessage("Logged in!", "#00aa00"); }
function justLoggedOut() { showMessage("Logged out.", "#00aa00"); }
function justFailedToLogIn() { showMessage("Login failure!", "#aa0000"); }
function showMessage(msg, color) {
var paragraph = document.createElement('p');
var lines = msg.split("\n");
for (var i = 0, n = lines.length; i < n; ++i) {
paragraph.appendChild(document.createTextNode(lines[i]));
paragraph.appendChild(document.createElement("br"));
}
paragraph.style.color = color;
document.getElementById('log').appendChild(paragraph);
return msg;
}
function justJoinedChannel(channel) {
var channelSelector = document.getElementById("channelSelector");
var option = document.createElement("option");
option.value = channel.id();
option.appendChild(document.createTextNode(channel.name()));
channelSelector.appendChild(option);
showMessage("Joined channel " + channel, "#00aa00");
}
function justLeftChannel(channel) {
var channelSelector = document.getElementById("channelSelector");
var option = document.getElementById("channel_" + channel.id());
channelSelector.removeChild(option);
showMessage("Left channel " + channel, "#00aa00");
}
function doCommand() {
try {
var cmdBox = document.getElementById('cmdBox');
var cmd = cmdBox.value;
if (cmd.indexOf("login") === 0) {
if (client) {
showMessage("You're already logged in.", "#aa0000");
} else {
login(cmd.split(" ")[1]);
}
} else if (!client) {
showMessage("You'd better log in first.", "#aa0000");
} else if (cmd === "logout") {
logout();
} else {
sendMessage(cmd)
}
cmdBox.value = "";
} catch (x) {
try {
showMessage("Exception: " + x);
} catch (xx) { // in case showMessage itself is broken
if (typeof(console) !== 'undefined') {
console.error(x);
} else {
alert(x);
}
}
}
return false;
}
</script>
</head>
<body>
<h1>RedDwarf-JS client example</h1>
<select id="channelSelector">
<option value="_default_">[session]</option>
</select>
<div id="log">
</div>
<form onsubmit="return doCommand()">
<input id="cmdBox">
</form>
<p>Available commands for SwordWorld:</p>
<ul>
<li>login</li>
<li>logout</li>
<li>look</li>
</ul>
</body>
</html>