当前位置:   article > 正文

WebSocket的使用_websocket使用

websocket使用

一、WebSocket概述

        一般我们前后端交互都是使用http,http是前端发起一个请求,后端回复。它的特点就是前端发起请求后端返回数据,一次交互就没了。而我们在聊天的时候,A给B发送了一个消息,我们后台接收到了消息然后主动推送给B去显示。
websocket是一个双向通信的协议,前后端都可以主动的去发送消息。websocket本质上也是一个请求连接,所以我们也可以像http请求一样传递连接参数。当我们连接成功后,后端会产生一个session,我们可以通过这个session去发消息(它里面提供了一个发送消息的方法)

配置类:

  1. @Configuration
  2. public class WebSocketConfig {
  3. @Bean
  4. public ServerEndpointExporter serverEndpointExporter() {
  5. return new ServerEndpointExporter();
  6. }
  7. }

二、前端代码

  1. initWebSocket: function (userId,sessionId) {
  2. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  3. this.websock = new WebSocket("ws://127.0.0.1:1997/websocket/"+userId+"/"+sessionId);
  4. this.websock.onopen = this.websocketonopen;
  5. this.websock.onerror = this.websocketonerror;
  6. this.websock.onmessage = this.websocketonmessage;
  7. this.websock.onclose = this.websocketclose;
  8. },
  9. websocketonopen: function () {
  10. console.log("WebSocket连接成功");
  11. },
  12. websocketonerror: function (e) {
  13. console.log("WebSocket连接发生错误",e);
  14. },
  15. websocketonmessage: function (e) {
  16. console.log("获取到后端传递来的数据",e.data);
  17. },
  18. websocketclose: function (e) {
  19. console.log("WebSocket连接关闭",e);
  20. },
  1. //开始会话
  2. startSession(sessionId){
  3. console.log(sessionId);
  4. if(this.websock != undefined){
  5. this.websock.close()
  6. }
  7. this.curSessionId = sessionId
  8. this.initWebSocket(this.curUserId, sessionId)
  9. this.msgList(sessionId)
  10. },
  1. created() { // 页面创建生命周期函数
  2. },
  3. updated(){
  4. // 解决每次发消息到最后面
  5. var elmnt = document.getElementById("msg_end");
  6. elmnt.scrollTop = elmnt.scrollHeight;
  7. },
  8. destroyed: function () { // 离开页面生命周期函数
  9. this.websocketclose();
  10. },

1、initWebSocket

        我们调用initWebSocket方法就可以和后端建立连接,userIdsessionId就是我们传递的参数。

2、websocketonopen

        websocket连接成功后,就会调用websocketonopen方法。

3、websocketonerror

        websocket连接失败后,就会调用websocketonerror方法

4、websocketonmessage

        当接收到后端发送消息的时候,就会调用 websocketonmessage方法,e.data就是传递的消息内容。

5、websocketclose

        当websocket连接关闭的时候,就会调用这个websocketclose方法。

关闭连接的原因有很多,后端异常、网络异常等一些原因,所以我们可以在这里考虑如何做中断后重连

三、后端代码

  1. @OnOpen
  2. public void onOpen(Session session,@PathParam(value="userId")Integer userId, @PathParam(value="sessionId")String sessionId) {
  3. this.session = session;
  4. CurPool.webSockets.put(userId,this);
  5. List<Object> list = new ArrayList<>();
  6. list.add(sessionId);
  7. list.add(session);
  8. CurPool.sessionPool.put(userId , list);
  9. System.out.println("【websocket消息】有新的连接,总数为:"+CurPool.webSockets.size());
  10. }
  11. @OnClose
  12. public void onClose() {
  13. // 断开连接删除用户删除session
  14. Integer userId = Integer.parseInt(this.session.getRequestParameterMap().get("userId").get(0));
  15. CurPool.sessionPool.remove(userId);
  16. CurPool.webSockets.remove(userId);
  17. if (userMapper == null){
  18. this.userMapper = (UserMapper)SpringContextUtil.getBean("userMapper");
  19. }
  20. User user = userMapper.selectByPrimaryKey(userId);
  21. CurPool.curUserPool.remove(user.getName());
  22. System.out.println("【websocket消息】连接断开,总数为:"+CurPool.webSockets.size());
  23. }
  24. @OnMessage
  25. public void onMessage(String message) {
  26. String sessionId = this.session.getRequestParameterMap().get("sessionId").get(0);
  27. if (sessionId == null){
  28. System.out.println("sessionId 错误");
  29. }
  30. // 在这里无法注入Mapper所以使用这种方式注入Mapper
  31. if (seesionListMapper == null){
  32. this.seesionListMapper = (SeesionListMapper)SpringContextUtil.getBean("seesionListMapper");
  33. }
  34. if (userMapper == null){
  35. this.userMapper = (UserMapper)SpringContextUtil.getBean("userMapper");
  36. }
  37. if (msgInfoMapper == null){
  38. this.msgInfoMapper = (MsgInfoMapper)SpringContextUtil.getBean("msgInfoMapper");
  39. }
  40. SessionList sessionList = seesionListMapper.selectByPrimaryKey(Integer.parseInt(sessionId));
  41. User user = userMapper.selectByPrimaryKey(sessionList.getUserId());
  42. MsgInfo msgInfo = new MsgInfo();
  43. msgInfo.setContent(message);
  44. msgInfo.setCreateTime(new Date());
  45. msgInfo.setFromUserId(sessionList.getUserId());
  46. msgInfo.setFromUserName(user.getName());
  47. msgInfo.setToUserId(sessionList.getToUserId());
  48. msgInfo.setToUserName(sessionList.getListName());
  49. msgInfo.setUnReadFlag(0);
  50. // 消息持久化
  51. msgInfoMapper.insert(msgInfo);
  52. // 判断用户是否存在,不存在就结束
  53. List<Object> list = CurPool.sessionPool.get(sessionList.getToUserId());
  54. if (list == null || list.isEmpty()){
  55. // 用户不存在,更新未读数
  56. seesionListMapper.addUnReadCount(sessionList.getToUserId(),sessionList.getUserId());
  57. }else{
  58. // 用户存在,判断会话是否存在
  59. String id = seesionListMapper.selectIdByUser(sessionList.getToUserId(), sessionList.getUserId())+"";
  60. String o = list.get(0) + "";
  61. if (id.equals(o)){
  62. // 会话存在直接发送消息
  63. sendTextMessage(sessionList.getToUserId(),JsonUtils.objectToJson(msgInfo));
  64. }else {
  65. // 判断会话列表是否存在
  66. if (id == null || "".equals(id) || "null".equals(id)){
  67. // 新增会话列表
  68. SessionList tmpSessionList = new SessionList();
  69. tmpSessionList.setUserId(sessionList.getToUserId());
  70. tmpSessionList.setToUserId(sessionList.getUserId());
  71. tmpSessionList.setListName(user.getName());
  72. tmpSessionList.setUnReadCount(1);
  73. seesionListMapper.insert(tmpSessionList);
  74. }else {
  75. // 更新未读消息数量
  76. seesionListMapper.addUnReadCount(sessionList.getToUserId(),sessionList.getUserId());
  77. }
  78. // 会话不存在发送列表消息
  79. List<SessionList> sessionLists = seesionListMapper.selectByUserId(sessionList.getToUserId());
  80. sendTextMessage(sessionList.getToUserId() ,JsonUtils.objectToJson(sessionLists));
  81. }
  82. }
  83. System.out.println("【websocket消息】收到客户端消息:"+message);
  84. }

1、onOpen

        前端调用连接的时候,就会调用这个方法。我们可以拿到当前连接的session和传递的参数。

我们在这里去保存这个session,后面发消息需要用到

   2、onClose

        当连接中断的时候,会调用这个方法。

我们可以在这里去清除这个session

3、onMessage

        当前端向我们发消息(this.websock.send(content))的时候,就进入了这个方法

我们可以通过this.session拿到当前发消息的session(具体的代码可以看下源码)

4、sendTextMessage

        发送文本消息,我们通过 session.getBasicRemote().sendText(message) 这个方法主动的向前端推送消息。前端的websocketonmessage方法就可以接收到我们的消息了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/242027
推荐阅读
相关标签
  

闽ICP备14008679号