sessionList = groupMemberInfoMap.get(sid);
59 | sessionList.remove(session);
60 | log.info("Connection closed");
61 | log.info("sid: {}, sessionList size: {}", sid, sessionList.size());
62 | }
63 |
64 | // 传输消息错误调用的方法
65 | @OnError
66 | public void OnError(Throwable error) {
67 | log.info("Connection error");
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/com/javashitang/controller/ShowTopController.java:
--------------------------------------------------------------------------------
1 | package com.javashitang.controller;
2 |
3 | import org.springframework.web.bind.annotation.RequestMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 |
6 | import java.util.concurrent.TimeUnit;
7 |
8 | /**
9 | * @author lilimin
10 | * @since 2021-03-06
11 | */
12 | @RestController
13 | @RequestMapping("top")
14 | public class ShowTopController {
15 |
16 | private Object lock1 = new Object();
17 | private Object lock2 = new Object();
18 |
19 | @RequestMapping("test")
20 | public String test() {
21 | return "success";
22 | }
23 |
24 | @RequestMapping("loop")
25 | public String loop() {
26 | System.out.println("start");
27 | while (true) {}
28 | }
29 |
30 | @RequestMapping("deadlock")
31 | public String deadlock() {
32 | new Thread(() -> {
33 | synchronized (lock1) {
34 | try{
35 | TimeUnit.SECONDS.sleep(1);
36 | } catch (Exception e) {}
37 | synchronized (lock2) {
38 | System.out.println("thread1 over");
39 | }
40 | }
41 | }).start();
42 | new Thread(() -> {
43 | synchronized (lock2) {
44 | try{
45 | TimeUnit.SECONDS.sleep(1);
46 | } catch (Exception e) {}
47 | synchronized (lock1) {
48 | System.out.println("thread2 over");
49 | }
50 | }
51 | }).start();
52 | return "success";
53 | }
54 | }
55 |
56 |
--------------------------------------------------------------------------------
/src/main/java/com/javashitang/controller/ViewController.java:
--------------------------------------------------------------------------------
1 | package com.javashitang.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.GetMapping;
5 |
6 | @Controller
7 | public class ViewController {
8 |
9 | @GetMapping("index")
10 | public String index() {
11 | return "index";
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/javashitang/controller/WebSocketServerController.java:
--------------------------------------------------------------------------------
1 | package com.javashitang.controller;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | import javax.websocket.OnClose;
6 | import javax.websocket.OnError;
7 | import javax.websocket.OnMessage;
8 | import javax.websocket.OnOpen;
9 | import javax.websocket.Session;
10 | import javax.websocket.server.PathParam;
11 | import javax.websocket.server.ServerEndpoint;
12 | import java.io.IOException;
13 |
14 | @Component
15 | @ServerEndpoint("/forshow/websocket/{sid}")
16 | public class WebSocketServerController {
17 |
18 | // 收到消息调用的方法
19 | @OnMessage
20 | public void onMessage(Session session, String message) {
21 | try {
22 | session.getBasicRemote().sendText(message);
23 | } catch (IOException e) {
24 | e.printStackTrace();
25 | }
26 | }
27 |
28 | // 建立连接调用的方法
29 | @OnOpen
30 | public void onOpen(@PathParam("sid") String sid) {
31 | System.out.println("Client connected");
32 | }
33 |
34 | // 关闭连接调用的方法
35 | @OnClose
36 | public void onClose() {
37 | System.out.println("Connection closed");
38 | }
39 |
40 | // 传输消息错误调用的方法
41 | @OnError
42 | public void OnError(Throwable error) {
43 | System.out.println("Connection error");
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8090
--------------------------------------------------------------------------------
/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | %d{HH:mm:ss.SSS} |-%5level %logger{20} [%t] |%X{ip} - %msg%n
8 | %d{yyyy-MM-dd HH:mm:ss.SSS} |-%5level %logger{20} [%t] %X{ip} - %msg%n
9 | %msg%n
10 | ./logs
11 | info
12 |
13 |
14 |
15 |
16 |
17 | ${stdoutPattern}
18 |
19 |
20 |
21 |
22 | ${filePattern}
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/main/resources/templates/demo.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Testing websockets
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
57 |
58 |
--------------------------------------------------------------------------------
/src/main/resources/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Testing websockets
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
76 |
77 |
--------------------------------------------------------------------------------