|
@@ -0,0 +1,84 @@
|
|
1
|
+package kr.co.swh.lecture.opensource.websocket;
|
|
2
|
+
|
|
3
|
+import java.net.InetSocketAddress;
|
|
4
|
+import java.net.UnknownHostException;
|
|
5
|
+import java.nio.ByteBuffer;
|
|
6
|
+import java.util.Collections;
|
|
7
|
+
|
|
8
|
+import org.java_websocket.WebSocket;
|
|
9
|
+import org.java_websocket.drafts.Draft;
|
|
10
|
+import org.java_websocket.drafts.Draft_6455;
|
|
11
|
+import org.java_websocket.handshake.ClientHandshake;
|
|
12
|
+import org.java_websocket.server.WebSocketServer;
|
|
13
|
+
|
|
14
|
+/**
|
|
15
|
+ * <pre>
|
|
16
|
+ * kr.co.swh.lecture.opensource.websocket
|
|
17
|
+ * ExampleServer.java
|
|
18
|
+ *
|
|
19
|
+ * 설명 :
|
|
20
|
+ * </pre>
|
|
21
|
+ *
|
|
22
|
+ * @since : 2020. 8. 2.
|
|
23
|
+ * @author : tobby48
|
|
24
|
+ * @version : v1.0
|
|
25
|
+ */
|
|
26
|
+public class ExampleServer extends WebSocketServer {
|
|
27
|
+
|
|
28
|
+ public static void main(String[] args) throws UnknownHostException {
|
|
29
|
+ ExampleServer server = new ExampleServer(8887);
|
|
30
|
+ server.setReuseAddr(true);
|
|
31
|
+ server.setTcpNoDelay(false);
|
|
32
|
+ server.start();
|
|
33
|
+ }
|
|
34
|
+ public ExampleServer( int port ) throws UnknownHostException {
|
|
35
|
+ super( new InetSocketAddress( port ) );
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ public ExampleServer( InetSocketAddress address ) {
|
|
39
|
+ super( address );
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ public ExampleServer(int port, Draft_6455 draft) {
|
|
43
|
+ super( new InetSocketAddress( port ), Collections.<Draft>singletonList(draft));
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ @Override
|
|
47
|
+ public void onOpen( WebSocket conn, ClientHandshake handshake ) {
|
|
48
|
+ conn.send("Welcome to the server!"); //This method sends a message to the new client
|
|
49
|
+ broadcast( "new connection: " + handshake.getResourceDescriptor() ); //This method sends a message to all clients connected
|
|
50
|
+ System.out.println( conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!" );
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ @Override
|
|
54
|
+ public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
|
|
55
|
+ broadcast( conn + " has left the room!" );
|
|
56
|
+ System.out.println( conn + " has left the room!" );
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ @Override
|
|
60
|
+ public void onMessage( WebSocket conn, String message ) {
|
|
61
|
+ broadcast( message );
|
|
62
|
+ System.out.println( conn + ": " + message );
|
|
63
|
+ }
|
|
64
|
+ @Override
|
|
65
|
+ public void onMessage( WebSocket conn, ByteBuffer message ) {
|
|
66
|
+ broadcast( message.array() );
|
|
67
|
+ System.out.println( conn + ": " + message );
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ @Override
|
|
71
|
+ public void onError( WebSocket conn, Exception ex ) {
|
|
72
|
+ ex.printStackTrace();
|
|
73
|
+ if( conn != null ) {
|
|
74
|
+ // some errors like port binding failed may not be assignable to a specific websocket
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ @Override
|
|
79
|
+ public void onStart() {
|
|
80
|
+ System.out.println("Server started!");
|
|
81
|
+ setConnectionLostTimeout(0);
|
|
82
|
+ setConnectionLostTimeout(100);
|
|
83
|
+ }
|
|
84
|
+}
|