WebStock会话
创始人
2024-11-15 03:33:04
0

其实使用消息队列也可以实现会话,直接前端监听指定的队列,使用rabbitmq的分组还可以实现不同群聊的效果。

1、依赖搭建:

      4.0.0      org.example     WebStock     1.0-SNAPSHOT              org.springframework.boot         spring-boot-starter-parent         2.7.8                                         org.springframework.boot             spring-boot-starter                               org.springframework.boot             spring-boot-starter-web                               org.springframework.boot             spring-boot-starter-websocket                               org.projectlombok             lombok                            8         8         UTF-8        

SpringBoot都已集成完毕了,不用使用原生的WebStock。

2、配置运行

如果是工作中,要单独起一个服务来操作这个比较好,反正是微服务,多一个少一个没啥的

WebStock连接配置、

package com.quxiao.config;  import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.socket.BinaryMessage; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.AbstractWebSocketHandler;  import java.time.LocalDateTime;  /**  * ws消息处理类  */ @Component @Slf4j public class MyWsHandler extends AbstractWebSocketHandler {     @Autowired     WsService wsService;      @Override     public void afterConnectionEstablished(WebSocketSession session) throws Exception {         log.info("建立ws连接");         WsSessionManager.add(session.getId(), session);     }      @Override     protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {         log.info("发送文本消息");         // 获得客户端传来的消息         String payload = message.getPayload();         //这里也是一样,要取出前端传来的参数,判断发给谁.这里我就是发给了连接客户端自己.         log.info("server 接收到消息 " + payload);         wsService.sendMsg(session, "server 发送给的消息 " + payload + ",发送时间:" + LocalDateTime.now().toString());         String url = session.getUri().toString();         //使用?拼接参数,后端取出判断发给谁         System.out.println("获取到的参数:" + url.substring(url.indexOf('?') + 1));     }      @Override     protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {         log.info("发送二进制消息");     }      @Override     public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {         log.error("异常处理");         WsSessionManager.removeAndClose(session.getId());     }      @Override     public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {         log.info("关闭ws连接");         WsSessionManager.removeAndClose(session.getId());     } } 
package com.quxiao.config;  import lombok.extern.slf4j.Slf4j; import org.springframework.web.socket.WebSocketSession;  import java.io.IOException; import java.util.concurrent.ConcurrentHashMap;  @Slf4j public class WsSessionManager {     /**      * 保存连接 session 的地方      */     public  static ConcurrentHashMap SESSION_POOL = new ConcurrentHashMap<>();      /**      * 添加 session      *      * @param key      */     public static void add(String key, WebSocketSession session) {         // 添加 session         SESSION_POOL.put(key, session);     }      /**      * 删除 session,会返回删除的 session      *      * @param key      * @return      */     public static WebSocketSession remove(String key) {         // 删除 session         return SESSION_POOL.remove(key);     }      /**      * 删除并同步关闭连接      *      * @param key      */     public static void removeAndClose(String key) {         WebSocketSession session = remove(key);         if (session != null) {             try {                 // 关闭连接                 session.close();             } catch (IOException e) {                 // todo: 关闭出现异常处理                 e.printStackTrace();             }         }     }      /**      * 获得 session      *      * @param key      * @return      */     public static WebSocketSession get(String key) {         // 获得 session         return SESSION_POOL.get(key);     } } 
package com.quxiao.config;  import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;  @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer {      @Autowired     private MyWsHandler myWsHandler;      @Override     public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {         registry                 .addHandler(myWsHandler, "myWs")                 //允许跨域                 .setAllowedOrigins("*");     } } 

这里有几个后续逻辑:

(1)、连接时,通过前端的参数或者对其按登陆人信息绑定连接消息。

(2)、收到一个消息,发送给谁就得需要前端传来参数:例如某个群的id,然后通过群绑定人员,因为(1)中通过人员id为key,value时连接信息。

直接遍历这个群下面的所有人员id,获取已经连接的信息,发送给他们。这里还要搞一个表存储群消息log。这样其他群员连接时,可以获取到以往的消息。

发送消息工具类

package com.quxiao.config;  import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession;  import java.io.IOException;  /**  * ws操作相关服务  */ @Service @Slf4j public class WsServiceUtil {      /**      * 发送消息      * @param session      * @param text      * @return      * @throws IOException      */     public void sendMsg(WebSocketSession session, String text) throws IOException {         session.sendMessage(new TextMessage(text));     }      /**      * 广播消息      * @param text      * @return      * @throws IOException      */     public void broadcastMsg(String text) throws IOException {         for (WebSocketSession session: WsSessionManager.SESSION_POOL.values()) {             session.sendMessage(new TextMessage(text));         }     }  } 

   这里广播我就是遍历了储存连接消息的map容器。

package com.quxiao.controller;  import com.quxiao.config.WsServiceUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile;  import javax.servlet.http.HttpServletRequest;  @RestController @RequestMapping("/put") public class MessageHandler {     @Autowired     WsServiceUtil wsServiceUtil;      @PostMapping("/t1/{test}")     public void t1(@PathVariable("test") String text) {         try {             wsServiceUtil.broadcastMsg(Thread.currentThread().getName() + ": " + text);         } catch (Exception e) {             throw new RuntimeException(e);         }     }  } 

3、 前端测试页面:

       My WebSocket       

4、总结

所以最重要的是这个消息发给谁

        后端要做的就是需要根据不同的群、人员标识符去发送消息,

前端需要做的就是

        传入不同的标识符,如果是私聊,就得传人员id,如果是群聊,就需要传入群id。

相关内容

热门资讯

秒懂普及”新七喜大厅房卡购买“... 秒懂普及”新七喜大厅房卡购买“牛牛房卡是怎么购买的 微信牛牛房卡客服微信号微信游戏中心打开微信,添加...
秒懂教程!我买拼三张房卡链接,... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:71319951许多玩家在游戏中会购买房卡来享...
实测分享”皇豪互娱房卡充值“金... 实测分享”皇豪互娱房卡充值“金花牛牛房卡充值游戏中心打开微信,添加客服【113857776】,进入游...
正版授权“可以一起创房的牛牛,... 九尾大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
分享经验”神盾大新获取房卡教程... 分享经验”神盾大新获取房卡教程“卡农大厅房卡充值游戏中心打开微信,添加客服【113857776】,进...
秒懂教程!微信牛牛房卡如何购买... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:66336574许多玩家在游戏中会购买房卡来享受...
1分秒分析”海贝之城哪里买低价... 来教大家如何使用哪里有详细房卡介绍房卡充值 添加房卡批售商:微【113857775】复制到微信搜索、...
ia实测“在哪里买炸金花房卡便... 天王大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来...
实测教程”鲨鱼众娱怎么买房卡“... 实测教程”鲨鱼众娱怎么买房卡“王者大厅房间卡怎么购买游戏中心打开微信,添加客服【113857776】...
秒懂教程!微信买链接牛牛房卡,... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:56001354许多玩家在游戏中会购买房卡来享受...
秒懂教程!微信金花房间卡在哪买... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:71319951许多玩家在游戏中会购买房卡来享受...
ia实测“微信斗牛房卡专卖店联... 金牛座金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
1分秒分析”百万牛房卡客服“拼... 房卡客服是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:113857776许多玩家在游戏中会购买房卡...
一分钟实测分享”海贝之城房卡获... 一分钟实测分享”海贝之城房卡获取方式“金花房卡充值游戏中心打开微信,添加客服【113857776】,...
秒懂教程!拼三张房卡哪里买,新... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:66336574许多玩家在游戏中会购买房卡来享...
一分钟了解“哪里购买斗牛牛链接... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
秒懂普及”新竹大厅在哪里买房卡... 第二也可以在游戏内商城:在游戏界面中找到 “微信金花,斗牛链接房卡”“商城”选项,选择房卡的购买选项...
玩家攻略”新大海房卡获取方式“... 玩家攻略”新大海房卡获取方式“详细房卡使用教程 微信牛牛房卡客服微信号微信游戏中心打开微信,添加客服...
秒懂教程!微信牛牛房卡怎样开,... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:56001354许多玩家在游戏中会购买房卡来享受...
实测教程”贝塔大厅是如何购买的... 第二也可以在游戏内商城:在游戏界面中找到 “微信金花,斗牛链接房卡”“商城”选项,选择房卡的购买选项...