|
@@ -0,0 +1,140 @@
|
|
|
+package com.fjhx.service;
|
|
|
+
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.fjhx.entity.SendEntity;
|
|
|
+import com.fjhx.event.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+
|
|
|
+import javax.websocket.*;
|
|
|
+import javax.websocket.server.PathParam;
|
|
|
+import javax.websocket.server.ServerEndpoint;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+@ServerEndpoint("/webStock/{userId}")
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class WebSocketServer {
|
|
|
+
|
|
|
+ private static final ApplicationContext applicationContext = SpringUtil.getApplicationContext();
|
|
|
+
|
|
|
+
|
|
|
+ * concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
|
|
|
+ */
|
|
|
+ private static final ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ * 与某个客户端的连接会话,需要通过它来给客户端发送数据
|
|
|
+ */
|
|
|
+ private Session session;
|
|
|
+
|
|
|
+ * 接收userId
|
|
|
+ */
|
|
|
+ private String userId;
|
|
|
+
|
|
|
+
|
|
|
+ * 连接建立成功调用的方法
|
|
|
+ */
|
|
|
+ @OnOpen
|
|
|
+ public void onOpen(@RequestBody Session session, @PathParam("userId") String userId) {
|
|
|
+ this.session = session;
|
|
|
+ this.userId = userId;
|
|
|
+ webSocketMap.remove(userId);
|
|
|
+ webSocketMap.put(userId, this);
|
|
|
+
|
|
|
+
|
|
|
+ applicationContext.publishEvent(new WebSocketOnOpenEvent(this, userId));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 连接关闭调用的方法
|
|
|
+ */
|
|
|
+ @OnClose
|
|
|
+ public void onClose() {
|
|
|
+ webSocketMap.remove(userId);
|
|
|
+
|
|
|
+
|
|
|
+ applicationContext.publishEvent(new WebSocketOnCloseEvent(this, userId));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 收到客户端消息后调用的方法
|
|
|
+ *
|
|
|
+ * @param message 客户端发送过来的消息
|
|
|
+ */
|
|
|
+ @OnMessage
|
|
|
+ public void onMessage(String message) {
|
|
|
+
|
|
|
+ applicationContext.publishEvent(new WebSocketOnMessageEvent(this, userId, message));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @param error 异常
|
|
|
+ */
|
|
|
+ @OnError
|
|
|
+ public void onError(Throwable error) {
|
|
|
+
|
|
|
+ applicationContext.publishEvent(new WebSocketOnErrorEvent(this, userId, error));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 实现服务器主动推送
|
|
|
+ *
|
|
|
+ * @param message 消息内容
|
|
|
+ */
|
|
|
+ public void sendMessage(String message) {
|
|
|
+ SendEntity sendEntity = new SendEntity();
|
|
|
+ sendEntity.setUserId(userId);
|
|
|
+ sendEntity.setCreateTime(new Date());
|
|
|
+ sendEntity.setMessage(message);
|
|
|
+
|
|
|
+ try {
|
|
|
+ this.session.getBasicRemote().sendText(JSON.toJSONString(sendEntity));
|
|
|
+ applicationContext.publishEvent(new WebSocketSendSuccessEvent(this, sendEntity));
|
|
|
+ } catch (IOException e) {
|
|
|
+ applicationContext.publishEvent(new WebSocketSendFailEvent(this, sendEntity));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发送自定义消息
|
|
|
+ *
|
|
|
+ * @param userId 发送用户
|
|
|
+ * @param message 消息内容
|
|
|
+ */
|
|
|
+ public static void sendInfo(String userId, String message) {
|
|
|
+ WebSocketServer webSocketServer = webSocketMap.get(userId);
|
|
|
+
|
|
|
+ if (webSocketServer == null) {
|
|
|
+ applicationContext.publishEvent(new WebSocketUserOfflineEvent(userId, message));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ webSocketServer.sendMessage(message);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发送自定义消息--群发
|
|
|
+ *
|
|
|
+ * @param message 消息内容
|
|
|
+ */
|
|
|
+ public static void sendInfoGroup(String message) {
|
|
|
+ if (webSocketMap.size() > 0) {
|
|
|
+ for (String userId : WebSocketServer.webSocketMap.keySet()) {
|
|
|
+ sendInfo(userId, message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 在线人数
|
|
|
+ */
|
|
|
+ public static synchronized int getOnlineCount() {
|
|
|
+ return webSocketMap.size();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|