tobby48 4 vuotta sitten
vanhempi
commit
cd66ee1630

+ 6
- 0
pom.xml Näytä tiedosto

@@ -314,6 +314,12 @@
314 314
 			<artifactId>activemq-all</artifactId>
315 315
 			<version>5.11.0</version>
316 316
 		</dependency>
317
+		
318
+		<dependency>
319
+			<groupId>org.java-websocket</groupId>
320
+			<artifactId>Java-WebSocket</artifactId>
321
+			<version>1.5.1</version>
322
+		</dependency>
317 323
 	
318 324
 	</dependencies>
319 325
 	

+ 67
- 0
src/main/java/kr/co/swh/lecture/opensource/websocket/ExampleClient.java Näytä tiedosto

@@ -0,0 +1,67 @@
1
+package kr.co.swh.lecture.opensource.websocket; 
2
+
3
+import java.net.URI;
4
+import java.net.URISyntaxException;
5
+import java.util.Map;
6
+
7
+import org.java_websocket.client.WebSocketClient;
8
+import org.java_websocket.drafts.Draft;
9
+import org.java_websocket.handshake.ServerHandshake;
10
+
11
+/**
12
+ * <pre>
13
+ * kr.co.swh.lecture.opensource.websocket 
14
+ * ExampleClient.java
15
+ *
16
+ * 설명 :
17
+ * </pre>
18
+ * 
19
+ * @since : 2020. 8. 2.
20
+ * @author : tobby48
21
+ * @version : v1.0
22
+ */
23
+/** This example demonstrates how to create a websocket connection to a server. Only the most important callbacks are overloaded. */
24
+public class ExampleClient extends WebSocketClient {
25
+
26
+	public ExampleClient( URI serverUri , Draft draft ) {
27
+		super( serverUri, draft );
28
+	}
29
+
30
+	public ExampleClient( URI serverURI ) {
31
+		super( serverURI );
32
+	}
33
+
34
+	public ExampleClient( URI serverUri, Map<String, String> httpHeaders ) {
35
+		super(serverUri, httpHeaders);
36
+	}
37
+
38
+	@Override
39
+	public void onOpen( ServerHandshake handshakedata ) {
40
+		send("You are connected to ChatServer: " + getURI() + "\n" );
41
+		System.out.println( "opened connection" );
42
+		// if you plan to refuse connection based on ip or httpfields overload: onWebsocketHandshakeReceivedAsClient
43
+	}
44
+
45
+	@Override
46
+	public void onMessage( String message ) {
47
+		System.out.println( "received: " + message );
48
+	}
49
+
50
+	@Override
51
+	public void onClose( int code, String reason, boolean remote ) {
52
+		// The codecodes are documented in class org.java_websocket.framing.CloseFrame
53
+		System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) + " Code: " + code + " Reason: " + reason );
54
+	}
55
+
56
+	@Override
57
+	public void onError( Exception ex ) {
58
+		ex.printStackTrace();
59
+		// if the error is fatal then onClose will be called additionally
60
+	}
61
+
62
+	public static void main( String[] args ) throws URISyntaxException {
63
+		ExampleClient c = new ExampleClient( new URI( "ws://localhost:8887" )); // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts
64
+		c.connect();
65
+	}
66
+
67
+}

+ 84
- 0
src/main/java/kr/co/swh/lecture/opensource/websocket/ExampleServer.java Näytä tiedosto

@@ -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
+}

+ 3
- 3
src/main/nodejs/rabbitmq-receiver.js Näytä tiedosto

@@ -26,11 +26,11 @@ amqp.connect(url, function(error, connect){
26 26
                     else if(message){
27 27
                         console.log(message.content.toString());
28 28
                         channel.ack(message);
29
-                        setTimeout(recevieMessage, 5000);
29
+//                        setTimeout(recevieMessage, 5000);
30 30
                     }
31 31
                     else{
32
-                        console.log('NO MESSAGE');
33
-                        setTimeout(recevieMessage, 5000);
32
+//                        console.log('NO MESSAGE');
33
+//                        setTimeout(recevieMessage, 5000);
34 34
                     }
35 35
                 });
36 36
             }