123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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);
- WebSocketSession sessionk = userMap.get(projectId);
- if(sessionk == null || !session.isOpen()){
- userMap.put(projectId, 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);
- System.out.println("websocket发生 消息");
- } 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;
- }
- }
|