| | |
| | | package com.example.springboot.component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | |
| | | import javax.websocket.server.PathParam; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | |
| | | import org.springframework.context.ConfigurableApplicationContext; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | import cn.hutool.json.JSONUtil; |
| | |
| | | @ServerEndpoint(value = "/api/talk/{username}") |
| | | @Component |
| | | public class WebSocketServer { |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class); |
| | | |
| | | // @Autowired |
| | | // HomeMapper homeMapper; |
| | | |
| | | static ConfigurableApplicationContext applicationContext; |
| | | |
| | | //解决无法注入mapper问题 //使用方法 homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class); |
| | | public static void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext) { |
| | | WebSocketServer.applicationContext = configurableApplicationContext; |
| | | } |
| | | |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class); |
| | | private List<String> messages; |
| | | /** |
| | | * 记录当前在线连接数 |
| | | */ |
| | | public static final Map<String, WebSocketServer> sessionMap = new ConcurrentHashMap<>(); |
| | | |
| | | private String username; |
| | | private Session session; |
| | | String username; |
| | | Session session; |
| | | public WebSocketServer() { |
| | | this.messages = new ArrayList<>(); |
| | | } |
| | | /** |
| | | * 连接建立成功调用的方法 |
| | | */ |
| | |
| | | public void onMessage(String message, Session session, @PathParam("username") String username) { |
| | | log.info("服务端收到用户username={}的消息:{}", username, message); |
| | | JSONObject obj = JSONUtil.parseObj(message); |
| | | |
| | | String text = obj.getStr("data"); |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.set("message", "ngng"); |
| | | jsonObject.set("message", text); |
| | | this.messages.add(text); |
| | | this.sendMessage(jsonObject.toString()); //JSONUtil.toJsonStr(jsonObject) |
| | | |
| | | } |
| | | |
| | | @OnError |
| | |
| | | */ |
| | | public void sendMessage(String message) { |
| | | try { |
| | | log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message); |
| | | // log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message); |
| | | this.session.getBasicRemote().sendText(message); |
| | | } catch (Exception e) { |
| | | log.error("服务端发送消息给客户端失败", e); |
| | |
| | | public void sendAllMessage(String message) { |
| | | try { |
| | | for (WebSocketServer webSocketServer : sessionMap.values()) { |
| | | log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message); |
| | | //log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message); |
| | | webSocketServer.sendMessage(message); |
| | | } |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | } |
| | | |
| | | public List<String> getMessages() { |
| | | return messages; |
| | | |
| | | } |
| | | |
| | | public void clearMessages() { |
| | | messages.clear(); |
| | | } |
| | | } |