WebSocketServer.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //package com.ozs.websocket;
  2. //
  3. //import lombok.extern.slf4j.Slf4j;
  4. //import org.springframework.stereotype.Component;
  5. //
  6. //import javax.websocket.*;
  7. //import javax.websocket.server.PathParam;
  8. //import javax.websocket.server.ServerEndpoint;
  9. //import java.util.HashMap;
  10. //import java.util.List;
  11. //import java.util.Map;
  12. //import java.util.concurrent.ConcurrentHashMap;
  13. //import java.util.concurrent.CopyOnWriteArraySet;
  14. //
  15. ///**
  16. // * @Author : sunhh
  17. // * @create 2023/3/7 16:15
  18. // */
  19. //@Slf4j
  20. //@Component
  21. //@ServerEndpoint("/websocket/{userId}") // 接口路径 ws://localhost:8087/webSocket/userId;
  22. //public class WebSocketServer {
  23. //
  24. // //与某个客户端的连接会话,需要通过它来给客户端发送数据
  25. // private Session session;
  26. // /**
  27. // * 用户ID
  28. // */
  29. // private String userId;
  30. //
  31. // //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
  32. // //虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。
  33. // // 注:底下WebSocket是当前类名
  34. // private static CopyOnWriteArraySet<WebSocketServer> webSockets =new CopyOnWriteArraySet<>();
  35. // // 用来存在线连接用户信息
  36. // private static ConcurrentHashMap<String,Session> sessionPool = new ConcurrentHashMap<String,Session>();
  37. //
  38. // /**
  39. // * 链接成功调用的方法
  40. // */
  41. // @OnOpen
  42. // public void onOpen(Session session, @PathParam(value="userId")String userId) {
  43. // try {
  44. // this.session = session;
  45. // this.userId = userId;
  46. // webSockets.add(this);
  47. // sessionPool.put(userId, session);
  48. // log.info("【websocket消息】有新的连接,总数为:"+webSockets.size());
  49. // } catch (Exception e) {
  50. // }
  51. // }
  52. //
  53. // /**
  54. // * 链接关闭调用的方法
  55. // */
  56. // @OnClose
  57. // public void onClose() {
  58. // try {
  59. // webSockets.remove(this);
  60. // sessionPool.remove(this.userId);
  61. // log.info("【websocket消息】连接断开,总数为:"+webSockets.size());
  62. // } catch (Exception e) {
  63. // }
  64. // }
  65. // /**
  66. // * 收到客户端消息后调用的方法
  67. // *
  68. // * @param message
  69. // */
  70. // @OnMessage
  71. // public void onMessage(String message) {
  72. // log.info("【websocket消息】收到客户端消息:"+message);
  73. // }
  74. //
  75. // /** 发送错误时的处理
  76. // * @param session
  77. // * @param error
  78. // */
  79. // @OnError
  80. // public void onError(Session session, Throwable error) {
  81. //
  82. // log.error("用户错误,原因:"+error.getMessage());
  83. // error.printStackTrace();
  84. // }
  85. //
  86. //
  87. // // 此为广播消息
  88. // public void sendAllMessage(String message) {
  89. // log.info("【websocket消息】广播消息:"+message);
  90. // for(WebSocketServer webSocket : webSockets) {
  91. // try {
  92. // if(webSocket.session.isOpen()) {
  93. // webSocket.session.getAsyncRemote().sendText(message);
  94. // }
  95. // } catch (Exception e) {
  96. // e.printStackTrace();
  97. // }
  98. // }
  99. // }
  100. //
  101. // // 此为单点消息
  102. // public void sendOneMessage(String userId, String message) {
  103. // Session session = sessionPool.get(userId);
  104. // if (session != null&&session.isOpen()) {
  105. // try {
  106. // log.info("【websocket消息】 单点消息:"+message);
  107. // session.getAsyncRemote().sendText(message);
  108. // } catch (Exception e) {
  109. // e.printStackTrace();
  110. // }
  111. // }
  112. // }
  113. //
  114. // // 此为单点消息(多人)
  115. // public void sendMoreMessage(List<String> userIds, String message) {
  116. // for(String userId:userIds) {
  117. // Session session = sessionPool.get(userId);
  118. // if (session != null&&session.isOpen()) {
  119. // try {
  120. // log.info("【websocket消息】 单点消息:"+message);
  121. // session.getAsyncRemote().sendText(message);
  122. // } catch (Exception e) {
  123. // e.printStackTrace();
  124. // }
  125. // }
  126. // }
  127. //
  128. // }
  129. //}