WebSocketBt.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //package com.gyee.alarm.websocket;
  2. //
  3. //import org.apache.commons.lang.StringUtils;
  4. //import org.apache.logging.log4j.LogManager;
  5. //import org.apache.logging.log4j.Logger;
  6. //import org.springframework.stereotype.Component;
  7. //
  8. //import javax.websocket.*;
  9. //import javax.websocket.server.PathParam;
  10. //import javax.websocket.server.ServerEndpoint;
  11. //import java.io.IOException;
  12. //import java.util.List;
  13. //import java.util.concurrent.ConcurrentHashMap;
  14. //
  15. ///**
  16. // * 功能描述:
  17. // * WebSocketServer服务端
  18. // * @Date: 2022-12-01 09:41:32
  19. // * @since: 1.0.0
  20. // */
  21. //// @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端。注解的值将被用于监听用户连接的终端访问URL地址
  22. //// encoders = WebSocketCustomEncoding.class 是为了使用ws自己的推送Object消息对象(sendObject())时进行解码,通过Encoder 自定义规则(转换为JSON字符串)
  23. //@ServerEndpoint(value = "/websocketBt/{userId}",encoders = WebSocketCustomEncoding.class)
  24. //@Component
  25. //public class WebSocketBt {
  26. // private final static Logger logger = LogManager.getLogger(WebSocketBt.class);
  27. //
  28. // /**
  29. // * 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的
  30. // */
  31. //
  32. // private static int onlineCount = 0;
  33. //
  34. // /**
  35. // * concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象
  36. // */
  37. // public static ConcurrentHashMap<String, WebSocketBt> webSocketBtMap = new ConcurrentHashMap<>();
  38. //
  39. // /***
  40. // * 功能描述:
  41. // * concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象的参数体
  42. // */
  43. // public static ConcurrentHashMap<String, PushParams> webSocketParamsBtMap = new ConcurrentHashMap<>();
  44. //
  45. // public static ConcurrentHashMap<String, List<String>> btParamsMap = new ConcurrentHashMap<>();
  46. // /**
  47. // * 与某个客户端的连接会话,需要通过它来给客户端发送数据
  48. // */
  49. //
  50. // private Session session;
  51. // private String userId;
  52. //
  53. //
  54. // /**
  55. // * 连接建立成功调用的方法
  56. // * onOpen 和 onClose 方法分别被@OnOpen和@OnClose 所注解。他们定义了当一个新用户连接和断开的时候所调用的方法。
  57. // */
  58. // @OnOpen
  59. // public void onOpen(Session session, @PathParam("userId") String userId) {
  60. // this.session = session;
  61. // this.userId = userId;
  62. // //加入map
  63. // webSocketBtMap.put(userId, this);
  64. // addOnlineCount(); //在线数加1
  65. // logger.info("Bt用户{}连接成功,当前在线人数为{}", userId, getOnlineCount());
  66. // try {
  67. // sendMessage(String.valueOf(this.session.getQueryString()));
  68. // } catch (IOException e) {
  69. // logger.error("BtIO异常");
  70. // }
  71. // }
  72. //
  73. //
  74. // /**
  75. // * 连接关闭调用的方法
  76. // */
  77. // @OnClose
  78. // public void onClose() {
  79. // //从map中删除
  80. // webSocketBtMap.remove(userId);
  81. // subOnlineCount(); //在线数减1
  82. // logger.info("Bt用户{}关闭连接!当前在线人数为{}", userId, getOnlineCount());
  83. // }
  84. //
  85. // /**
  86. // * 收到客户端消息后调用的方法
  87. // * onMessage 方法被@OnMessage所注解。这个注解定义了当服务器接收到客户端发送的消息时所调用的方法。
  88. // * @param message 客户端发送过来的消息
  89. // */
  90. // @OnMessage
  91. // public void onMessage(String message, Session session) throws IOException {
  92. // logger.info("Bt来自客户端用户:{} 消息:{}",userId, message);
  93. //
  94. // sendMessageByUserId(userId,"ok");
  95. // //群发消息
  96. // /*for (String item : webSocketBtMap.keySet()) {
  97. // try {
  98. // webSocketBtMap.get(item).sendMessage(message);
  99. // } catch (IOException e) {
  100. // e.printStackTrace();
  101. // }
  102. // }*/
  103. // }
  104. //
  105. // /**
  106. // * 发生错误时调用
  107. // *
  108. // * @OnError
  109. // */
  110. // @OnError
  111. // public void onError(Session session, Throwable error) {
  112. // logger.error("Bt用户错误:" + this.userId + ",原因:" + error.getMessage());
  113. // error.printStackTrace();
  114. // }
  115. //
  116. // /**
  117. // * 向客户端发送消息
  118. // */
  119. // public void sendMessage(String message) throws IOException {
  120. // this.session.getBasicRemote().sendText(message);
  121. // //this.session.getAsyncRemote().sendText(message);
  122. // }
  123. //
  124. // /**
  125. // * 向客户端发送消息
  126. // */
  127. // public void sendMessage(Object message) throws IOException, EncodeException {
  128. // this.session.getBasicRemote().sendObject(message);
  129. // //this.session.getAsyncRemote().sendText(message);
  130. // }
  131. //
  132. // /**
  133. // * 通过userId向客户端发送消息
  134. // */
  135. // public void sendMessageByUserId(String userId, String message) throws IOException {
  136. // logger.info("Bt服务端发送消息到{},消息:{}",userId,message);
  137. //
  138. // if(StringUtils.isNotBlank(userId)&&webSocketBtMap.containsKey(userId)){
  139. // webSocketBtMap.get(userId).sendMessage(message);
  140. // }else{
  141. // logger.error("Bt用户{}不在线",userId);
  142. // }
  143. //
  144. // }
  145. //
  146. // /**
  147. // * 通过userId向客户端发送消息
  148. // */
  149. // public synchronized void sendMessageByUserId(String userId, Object message) throws IOException, EncodeException {
  150. // logger.info("Bt服务端发送消息到{},消息:{}",userId,message);
  151. // if(StringUtils.isNotBlank(userId)&&webSocketBtMap.containsKey(userId)){
  152. // webSocketBtMap.get(userId).sendMessage(message);
  153. // }else{
  154. // logger.error("Bt用户{}不在线",userId);
  155. // }
  156. // }
  157. //
  158. // /**
  159. // * 通过userId更新缓存的参数
  160. // */
  161. // public void changeParamsByUserId(String userId, PushParams pushParams) throws IOException, EncodeException {
  162. // logger.info("wsBt用户{}请求参数更新,参数:{}",userId,pushParams.toString());
  163. // webSocketParamsBtMap.put(userId,pushParams);
  164. // }
  165. //
  166. // /**
  167. // * 群发自定义消息
  168. // */
  169. // public static void sendInfo(String message) throws IOException {
  170. // for (String item : webSocketBtMap.keySet()) {
  171. // try {
  172. // webSocketBtMap.get(item).sendMessage(message);
  173. // } catch (IOException e) {
  174. // continue;
  175. // }
  176. // }
  177. // }
  178. //
  179. // public static synchronized int getOnlineCount() {
  180. // return onlineCount;
  181. // }
  182. //
  183. // public static synchronized void addOnlineCount() {
  184. // WebSocketBt.onlineCount++;
  185. // }
  186. //
  187. // public static synchronized void subOnlineCount() {
  188. // WebSocketBt.onlineCount--;
  189. // }
  190. //
  191. //}