forked from TooTallNate/Java-WebSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
52 lines (44 loc) · 1.41 KB
/
ChatServer.java
File metadata and controls
52 lines (44 loc) · 1.41 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
import java.io.IOException;
import net.tootallnate.websocket.WebSocket;
import net.tootallnate.websocket.WebSocketServer;
/**
* A simple WebSocketServer implementation. Keeps track of a "chatroom".
*/
public class ChatServer extends WebSocketServer {
public ChatServer(int port) {
super(port, Draft.AUTO);
}
public void onClientOpen(WebSocket conn) {
try {
this.sendToAll(conn + " entered the room!");
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println(conn + " entered the room!");
}
public void onClientClose(WebSocket conn) {
try {
this.sendToAll(conn + " has left the room!");
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println(conn + " has left the room!");
}
public void onClientMessage(WebSocket conn, String message) {
try {
this.sendToAll(conn + ": " + message);
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println(conn + ": " + message);
}
public static void main(String[] args) {
int port = 8887;
try {
port = Integer.parseInt(args[0]);
} catch(Exception ex) {}
ChatServer s = new ChatServer(port);
s.start();
System.out.println("ChatServer started on port: " + s.getPort());
}
}