-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatServer.java
91 lines (83 loc) · 3.01 KB
/
ChatServer.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chatweb;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
/**
*
* @author Epicur
*/
public class ChatServer extends WebSocketServer {
// ConcurrentHashMap to save the connections.
private ConcurrentHashMap<WebSocket, String> connections = new ConcurrentHashMap<>();
private Iterator<String> iterator;
// Initialization of the server
public ChatServer(int port) throws UnknownHostException {
super(new InetSocketAddress(port));
}
public ChatServer(InetSocketAddress address) {
super(address);
}
public static void main(String[] args) throws InterruptedException, IOException {
int port = 50000;
ChatServer s = new ChatServer(port);
s.start();
System.out.println("The server has been started on port: " + s.getPort());
}
// Sends a message to all the clients
public void sendToAll(String text) {
Collection<WebSocket> con = connections();
synchronized (con) {
for (WebSocket c : con) {
c.send(text);
}
}
}
public void onOpen(WebSocket conn, ClientHandshake handshake) {
// Actions to perform whenever a connection is opened.
System.out.println("Tenim una nova connexió");
iterator = connections.values().iterator();
while (iterator.hasNext()) {
conn.send("/newuser " + iterator.next());
}
}
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
// Actions to perform whenever a connection is closed
String str = connections.get(conn);
connections.remove(conn);
this.sendToAll("/deleteuser " + str);
this.sendToAll(str + " has left the room!");
System.out.println(str + " has left the room!");
}
public void onMessage(WebSocket conn, String message) {
// Actions to perform whenever a message is received
if (message.contains("/newuser ")) {
String[] str = message.split(" ");
connections.put(conn, str[1]);
this.sendToAll(message);
} else {
this.sendToAll(message);
}
System.out.println(message);
}
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
if (conn != null) {
// some errors like port binding failed may not be assignable to a specific websocket
}
}
}