1. package com.zykj.hbaseAdmin.socket;
    2. import org.springframework.web.socket.CloseStatus;
    3. import org.springframework.web.socket.TextMessage;
    4. import org.springframework.web.socket.WebSocketSession;
    5. import org.springframework.web.socket.handler.TextWebSocketHandler;
    6. import java.io.IOException;
    7. import java.util.ArrayList;
    8. import java.util.Date;
    9. import java.util.List;
    10. public class WebSocketEndpoint extends TextWebSocketHandler {
    11. /**
    12. * 在线客户端集合
    13. */
    14. public static List<WebSocketSession> socketSessions = new ArrayList<WebSocketSession>();
    15. /**
    16. * 在线人数
    17. */
    18. public static int onlineCount = 0;
    19. /**
    20. * 客户端的session
    21. */
    22. private WebSocketSession webSocketSession;
    23. /**
    24. * 处理 收到的消息
    25. * @param session
    26. * @param message
    27. * @throws Exception
    28. */
    29. @Override
    30. protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    31. super.handleTextMessage(session,message);
    32. TextMessage returnMessage = new TextMessage(message.getPayload() + "received at server");
    33. session.sendMessage(returnMessage);
    34. startTask();
    35. }
    36. /**
    37. * 连接成功后的操作
    38. * @param session
    39. * @throws Exception
    40. */
    41. @Override
    42. public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    43. super.afterConnectionEstablished(session);
    44. this.webSocketSession = session;
    45. // WebSocketEndpoint.webSocketEndpoints.add(this);
    46. WebSocketEndpoint.socketSessions.add(session);
    47. WebSocketEndpoint.onlineCount++;
    48. String id = session.getId();
    49. String s = "val|id=" + id; //变量类型 的消息,
    50. this.send(s);
    51. }
    52. /**
    53. * 连接关闭后到操作
    54. * @param session
    55. * @param status
    56. * @throws Exception
    57. */
    58. @Override
    59. public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
    60. super.afterConnectionClosed(session, status);
    61. // WebSocketEndpoint.webSocketEndpoints.remove(this);
    62. WebSocketEndpoint.socketSessions.remove(this.webSocketSession);
    63. WebSocketEndpoint.onlineCount--;
    64. }
    65. /**
    66. * 获取在线人数
    67. * @return
    68. */
    69. public static int getOnlineCount() {
    70. return WebSocketEndpoint.onlineCount;
    71. }
    72. /**
    73. * 群发消息
    74. * @param message
    75. */
    76. public static void sendAll(String message) {
    77. TextMessage textMessage = new TextMessage(message);
    78. for (WebSocketSession socketSession : WebSocketEndpoint.socketSessions) {
    79. try {
    80. if (socketSession.isOpen()) {
    81. socketSession.sendMessage(textMessage);
    82. }
    83. } catch (IOException e) {
    84. // e.printStackTrace();
    85. continue;
    86. }
    87. }
    88. }
    89. /**
    90. * 发送消息到指定id的客户端
    91. * @param type 消息类型 msg-普通消息,cmd-命令,val-变量
    92. * @param id 客户端id
    93. * @param data 消息内容
    94. * @throws IOException
    95. */
    96. public static void sendWithId(String type,String id,String data) throws IOException {
    97. WebSocketSession webSocketSession = WebSocketEndpoint.findSessionById(id);
    98. if (webSocketSession != null) {
    99. String message = "";
    100. if (type.equals("msg")) {
    101. message = "msg|" + data;
    102. } else if (type.equals("cmd")) {
    103. message = "cmd|" + data;
    104. } else if (type.equals("val")) {
    105. message = "val|" + data;
    106. }
    107. //完整的消息结构为: "type|data" ,例如普通消息为 "msg|现在是2017年4月6日15:48:20",命令消息 为 "cmd|close",变量消息为 "val|a=123"
    108. TextMessage textMessage = new TextMessage(message);
    109. webSocketSession.sendMessage(textMessage);
    110. }
    111. }
    112. /**
    113. * 查找客户端webSocketSession
    114. * @param id
    115. * @return
    116. */
    117. public static WebSocketSession findSessionById(String id){
    118. for (WebSocketSession webSocketSession : WebSocketEndpoint.socketSessions) {
    119. if (id.equals(webSocketSession.getId())) {
    120. return webSocketSession;
    121. }
    122. }
    123. return null;
    124. }
    125. /**
    126. * 创建 消息
    127. * @param type 消息类型 msg-普通消息,cmd-命令,val-变量
    128. * @param data 消息内容
    129. * @return
    130. */
    131. public static TextMessage createTextMessage(String type, String data) {
    132. if (type.equals("val")) {
    133. return new TextMessage(type + "|" + data);
    134. }
    135. return new TextMessage(type + "|" + new Date(System.currentTimeMillis()).toString() + " " + data);
    136. }
    137. private void send(String message) throws IOException {
    138. TextMessage textMessage = new TextMessage(message);
    139. this.webSocketSession.sendMessage(textMessage);
    140. }
    141. public void startTask() {
    142. // new Thread(new SpssTask()).start();
    143. // try {
    144. // StatsUtil.start();
    145. // System.out.println(13216546);
    146. // StatsUtil.stop();
    147. // } catch (Exception e) {
    148. // e.printStackTrace();
    149. // }
    150. }
    151. }