Skip to content

Commit 810bd40

Browse files
committed
Few modifications to the example. Fixed some errors in 'chat.html', added ability to launch ChatServer on a desired port.
1 parent d15befc commit 810bd40

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

example/ChatServer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
* A simple WebSocketServer implementation. Keeps track of a "chatroom".
66
*/
77
public class ChatServer extends WebSocketServer {
8-
8+
9+
public ChatServer(int port) {
10+
super(port);
11+
}
12+
913
public void onClientOpen(WebSocket conn) {
1014
try {
1115
this.sendToAll(conn + " entered the room!");
@@ -34,7 +38,11 @@ public void onClientMessage(WebSocket conn, String message) {
3438
}
3539

3640
public static void main(String[] args) {
37-
ChatServer s = new ChatServer();
41+
int port = 80;
42+
try {
43+
port = Integer.parseInt(args[0]);
44+
} catch(Exception ex) {}
45+
ChatServer s = new ChatServer(port);
3846
s.start();
3947
System.out.println("ChatServer started on port: " + s.getPort());
4048
}

example/chat.html

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,38 @@
2323

2424
var ws;
2525

26-
function connect() {
26+
$("uriForm").observe("submit", function(e) {
27+
e.stop();
2728
ws = new WebSocket($F("uri"));
2829
ws.onopen = function() {
29-
log("[WebSocket#onopen] called.\n");
30-
$("uriForm").enable();
30+
log("[WebSocket#onopen]\n");
3131
}
3232
ws.onmessage = function(e) {
33-
log("[WebSocket#onmessage] called.\n\tMessage: " + e.data + "\n");
33+
log("[WebSocket#onmessage] Message: '" + e.data + "'\n");
3434
}
3535
ws.onclose = function() {
36-
log("[WebSocket#onclose] called.\n");
37-
$("uriForm").disable();
36+
log("[WebSocket#onclose]\n");
37+
$("uri", "connect").invoke("enable");
38+
$("disconnect").disable();
3839
ws = null;
3940
}
40-
}
41+
$("uri", "connect").invoke("disable");
42+
$("disconnect").enable();
43+
});
4144

42-
function send() {
45+
$("sendForm").observe("submit", function(e) {
46+
e.stop();
4347
if (ws) {
4448
var textField = $("textField");
4549
ws.send(textField.value);
46-
log("[WebSocket#send] called.\n\tSent string: " + textField.value + "\n");
50+
log("[WebSocket#send] Send: '" + textField.value + "'\n");
4751
textField.value = "";
4852
textField.focus();
4953
}
50-
}
51-
52-
$("uriForm").observe("submit", function(e) {
53-
e.stop();
54-
connect();
5554
});
5655

57-
$("sendForm").observe("submit", function(e) {
56+
$("disconnect").observe("click", function(e) {
5857
e.stop();
59-
send();
60-
});
61-
62-
$("disconnect").observe("click", function() {
6358
if (ws) {
6459
ws.close();
6560
}
@@ -68,8 +63,8 @@
6863
</script>
6964
</head>
7065
<body>
71-
<form id="uriForm"><input type="text" id="uri" value="ws://localhost" style="width:200px;"> <input type="submit" value="Connect"><input type="button" id="disconnect" value="Disconnect"></form><br>
66+
<form id="uriForm"><input type="text" id="uri" value="ws://localhost" style="width:200px;"> <input type="submit" id="connect" value="Connect"><input type="button" id="disconnect" value="Disconnect" disabled="disabled"></form><br>
7267
<form id="sendForm"><input type="text" id="textField" value="" style="width:200px;"> <input type="submit" value="Send"></form><br>
73-
<textarea id="log" rows="50" cols="100"></textarea><br>
68+
<form><textarea id="log" rows="30" cols="100" style="font-family:monospace; color:red;"></textarea></form><br>
7469
</body>
7570
</html>

0 commit comments

Comments
 (0)