Selaa lähdekoodia

WebSocketServer

root 5 vuotta sitten
vanhempi
commit
712ce39da2

+ 5 - 0
pom.xml

@@ -114,6 +114,11 @@
114 114
 			<artifactId>spring-boot-devtools</artifactId>
115 115
 			<optional>true</optional>
116 116
 		</dependency>
117
+		<dependency>
118
+			<groupId>org.yeauty</groupId>
119
+			<artifactId>netty-websocket-spring-boot-starter</artifactId>
120
+			<version>0.8.0</version>
121
+		</dependency>
117 122
 	</dependencies>
118 123
 
119 124
 	<build>

+ 67 - 0
src/main/java/com/kingkong/bljs/common/WebSocketServer.java

@@ -0,0 +1,67 @@
1
+package com.kingkong.bljs.common;
2
+
3
+import io.netty.handler.codec.http.HttpHeaders;
4
+import io.netty.handler.timeout.IdleStateEvent;
5
+import org.springframework.stereotype.Component;
6
+import org.yeauty.annotation.*;
7
+import org.yeauty.pojo.ParameterMap;
8
+import org.yeauty.pojo.Session;
9
+
10
+import java.io.IOException;
11
+
12
+
13
+@ServerEndpoint(prefix = "netty-websocket")
14
+@Component
15
+public class WebSocketServer {
16
+    @OnOpen
17
+    public void onOpen(Session session, HttpHeaders headers, ParameterMap parameterMap) throws IOException {
18
+        System.out.println("new connection");
19
+
20
+        //String paramValue = parameterMap.getParameter("paramKey");
21
+        //System.out.println(paramValue);
22
+    }
23
+
24
+    @OnClose
25
+    public void onClose(Session session) throws IOException {
26
+        System.out.println("one connection closed");
27
+    }
28
+
29
+    @OnError
30
+    public void onError(Session session, Throwable throwable) {
31
+        throwable.printStackTrace();
32
+    }
33
+
34
+    @OnMessage
35
+    public void onMessage(Session session, String message) {
36
+        System.out.println(message);
37
+        session.sendText("Hello Netty!");
38
+    }
39
+
40
+    @OnBinary
41
+    public void onBinary(Session session, byte[] bytes) {
42
+        for (byte b : bytes) {
43
+            System.out.println(b);
44
+        }
45
+        //session.sendBinary(bytes);
46
+    }
47
+
48
+    @OnEvent
49
+    public void onEvent(Session session, Object evt) {
50
+//        if (evt instanceof IdleStateEvent) {
51
+//            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
52
+//            switch (idleStateEvent.state()) {
53
+//                case READER_IDLE:
54
+//                    System.out.println("read idle");
55
+//                    break;
56
+//                case WRITER_IDLE:
57
+//                    System.out.println("write idle");
58
+//                    break;
59
+//                case ALL_IDLE:
60
+//                    System.out.println("all idle");
61
+//                    break;
62
+//                default:
63
+//                    break;
64
+//            }
65
+//        }
66
+    }
67
+}

+ 7 - 1
src/main/resources/application-c.properties

@@ -45,4 +45,10 @@ spring.thymeleaf.cache=false
45 45
 spring.thymeleaf.prefix=classpath:/templates/
46 46
 spring.thymeleaf.check-template-location=true
47 47
 spring.thymeleaf.suffix=.html
48
-spring.thymeleaf.encoding=UTF-8
48
+spring.thymeleaf.encoding=UTF-8
49
+
50
+
51
+#websocketÅäÖÃ
52
+netty-websocket.host=192.168.1.146
53
+netty-websocket.path=/
54
+netty-websocket.port=9021

+ 1 - 1
src/main/resources/application.properties

@@ -1 +1 @@
1
-spring.profiles.active=h
1
+spring.profiles.active=c