|
@@ -0,0 +1,153 @@
|
|
|
+package com.miniframe.websocket;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.socket.CloseStatus;
|
|
|
+import org.springframework.web.socket.TextMessage;
|
|
|
+import org.springframework.web.socket.WebSocketMessage;
|
|
|
+import org.springframework.web.socket.WebSocketSession;
|
|
|
+import org.springframework.web.socket.handler.TextWebSocketHandler;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 这个类处理来之浏览器(客户端)的WebSocket请求。在这个例子中,我们创建一个叫WebSocketEndPoint的类,并让它集成TextWebsocketHandler类。
|
|
|
+ * 然后重写父类方法handlerTextMessage(),每当客户端发送信息过来,都会由这个函数接收并处理。
|
|
|
+ * 当然这里还可以重写其他方法,如afterConnectionEstablished、afterConnectionClosed、handleTransportError
|
|
|
+ * 等等
|
|
|
+ *
|
|
|
+ * @author Administrator
|
|
|
+ *
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class WebsocketEndPoint extends TextWebSocketHandler {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+
|
|
|
+ private final static Map<String, WebSocketSession> userMap = new ConcurrentHashMap<String, WebSocketSession>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭websocket时调用该方法
|
|
|
+ *
|
|
|
+ * @see org.springframework.web.socket.WebSocketHandler#afterConnectionClosed(org.springframework.web.socket.WebSocketSession,
|
|
|
+ * org.springframework.web.socket.CloseStatus)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
|
|
|
+ String projectId = this.getProjectId(session);
|
|
|
+ if (!StringUtils.isEmpty(projectId)) {
|
|
|
+ userMap.remove(projectId);
|
|
|
+ System.err.println("该" + projectId + "用户已成功关闭");
|
|
|
+ } else {
|
|
|
+ System.err.println("关闭时,获取用户id为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 建立websocket连接时调用该方法
|
|
|
+ *
|
|
|
+ * org.springframework.web.socket.WebSocketHandler#afterConnectionEstablished(org.springframework.web.socket.WebSocketSession)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
|
|
+ String projectId = this.getProjectId(session);
|
|
|
+ if (!StringUtils.isEmpty(projectId)) {
|
|
|
+ userMap.put(projectId, session);
|
|
|
+ session.sendMessage(new TextMessage("建立服务端连接成功!"));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 客户端调用websocket.send时候,会调用该方法,进行数据通信
|
|
|
+ *
|
|
|
+ * org.springframework.web.socket.WebSocketHandler#handleMessage(org.springframework.web.socket.WebSocketSession,
|
|
|
+ * org.springframework.web.socket.WebSocketMessage)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
|
|
+ String msg = message.getPayload().toString();
|
|
|
+ String projectId = this.getProjectId(session);
|
|
|
+ System.err.println("该" + projectId + "用户发送的消息是:" + msg);
|
|
|
+ message = new TextMessage("服务端已经接收到消息,msg=" + msg);
|
|
|
+ session.sendMessage(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传输过程出现异常时,调用该方法
|
|
|
+ *
|
|
|
+ * org.springframework.web.socket.WebSocketHandler#handleTransportError(org.springframework.web.socket.WebSocketSession,
|
|
|
+ * java.lang.Throwable)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void handleTransportError(WebSocketSession session, Throwable e) throws Exception {
|
|
|
+ WebSocketMessage<String> message = new TextMessage("异常信息:" + e.getMessage());
|
|
|
+ session.sendMessage(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * org.springframework.web.socket.WebSocketHandler#supportsPartialMessages()
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean supportsPartialMessages() {
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * sendMessageToUser:发给指定用户
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public void sendMessageToUser(String projectId, String contents) {
|
|
|
+ WebSocketSession session = userMap.get(projectId);
|
|
|
+ if (session != null && session.isOpen()) {
|
|
|
+ try {
|
|
|
+ TextMessage message = new TextMessage(contents);
|
|
|
+ session.sendMessage(message);
|
|
|
+ } catch (IOException e) {
|
|
|
+ //e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * sendMessageToAllUsers:发给所有的用户
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public void sendMessageToAllUsers(String contents) {
|
|
|
+ Set<String> projectIds = userMap.keySet();
|
|
|
+ for (String projectId : projectIds) {
|
|
|
+ this.sendMessageToUser(projectId, contents);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * getvdioId:获取用户id
|
|
|
+ *
|
|
|
+ * @author liuchao
|
|
|
+ * @param session
|
|
|
+ * @return
|
|
|
+ * @since JDK 1.7
|
|
|
+ */
|
|
|
+ private String getProjectId(WebSocketSession session) {
|
|
|
+ try {
|
|
|
+ String projectId = (String) session.getAttributes().get("currentUser");
|
|
|
+ return projectId;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|