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