webScoket即时聊天,用户不在线时消息暂存,上线立马收到

这个代码是用webScoket写的即时聊天通讯的,功能可群发单发,可对不在线用户发送消息时用户一上线立马就能收到消息,也可以查看未读数量,这个代码写了有一段时间了,忽然想起来就想着发上来记一下和交流一下

欢迎大家指教,如果有什么不对的地方欢迎指出,共同进步

也可以去直接下载代码,不过会需要用5积分下载

webScoket.rar-Java代码类资源-CSDN下此资源为webScoket做的即时聊天,支持单发群发,支持接收对象不在线时一上线就能收到消息也可以更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/qq_39897814/11257171

2019/11/25日更新:

1.补充了前台js调用的代码

这里是所须依赖

<dependency>  
   <groupId>javax</groupId>  
   <artifactId>javaee-api</artifactId>  
   <version>7.0</version>  
</dependency> 

以下是代码:

这里是主要的

package com.caesar.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.stereotype.Controller;

import com.Utils.WebSocketMapUtil;


/**
 * 即时通讯
 * @author 史**
 *
 * 2018年9月17日
 */
@Controller
@ServerEndpoint(value = "/websocket") 
public class MyWebSocketController {
	private volatile static List<Session> sessions = Collections.synchronizedList(new ArrayList());
    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session session;
    //设置为静态的 公用一个消息map ConcurrentMap为线程安全的map  HashMap不安全
    private static ConcurrentMap<String, Map<String, List<Object>>> messageMap=new ConcurrentHashMap<>();

    /**
     * /**
     * 连接建立成功调用的方法
     * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
     * @param userId 用户id
     * @throws Exception
     */
    @OnOpen
    public void onOpen(Session session) throws Exception{
        System.out.println("开始");
        this.session = session;
       // sessions.add((Session) this);
	  	 String key="";//当前用户id
	  	String objectUserId="";//对象id
	  	 if("".equals(session.getQueryString()) || session.getQueryString()==null) {
	  		
	  		key= "";
	  	 }else {
	  		 String keString=session.getQueryString();
	  		 if(keString.length()>1) {
	  			 key=keString.split(",")[0];
	 	  		 objectUserId=keString.split(",")[1];
	  		 }else {
	  			key=session.getQueryString();
	  		 }
	  		
	  	 }
	      WebSocketMapUtil.put(key,this);
	      if(messageMap.get(key)!=null) {
	    	  //说明在用户没有登录的时候有人给用户发送消息
	    	  //该用户所有未收的消息
	    	  Map<String, List<Object>> lists=messageMap.get(key);	
	    	  //对象用户发送的离线消息
	    	  List<Object> list= lists.get(objectUserId);
	    	  if(list!=null) {
	    		  for(int i=0;i<list.size();i++) {
	  	    		//封装消息类型   消息内容+"["+发送消息的人+";"+接收消息的人","+0
	  				  String message=list.get(i)+"["+objectUserId+";"+key+","+0;
	  				  onMessage(message);
	  	    	  }   
	    	  }
	    	 
	    	  // map中key(键)的迭代器对象
	    	  //用户接收完消息后删除 避免下次继续发送
	    	  Iterator iterator = lists.keySet().iterator();
	    	  while (iterator.hasNext()) {// 循环取键值进行判断
	    	  String keys = (String) iterator.next();//键
	    	  if (objectUserId.equals(keys)) {
		    	  iterator.remove(); // 移除map中以a字符开头的键对应的键值对
		    	 // messageMap.remove(key);
	    	  }
	    	  }
	    	  
	      }
	     
    }
 
    /**
     * 连接关闭调用的方法
     * @throws Exception 
     */
    @OnClose
    public void onClose() throws Exception{
        //从map中删除
    	System.out.println(session.getQueryString().split(",")[0]);
        WebSocketMapUtil.remove(session.getQueryString().split(",")[0]);
        System.out.println("关闭");
    }
 
    /**
     * 收到客户端消息后调用的方法  单发
     * @param message 客户端发送过来的消息
     * @param session 可选的参数
     * @param tit  0 单发 1  群发
     * @throws IOException 
     */
    @OnMessage
    public void onMessage(String message){
    		String tit=message.substring(message.lastIndexOf(",")+1, message.length());
    	   System.out.println("收到");
    	   String userId=message.substring(message.lastIndexOf(";")+1,message.lastIndexOf(","));//接收消息的用户
    	   System.out.println("发给:"+userId);
    	   String sendUserId=message.substring(message.lastIndexOf("[")+1,message.lastIndexOf(";"));//发送消息的用户
    	   System.err.println("发消息的用户:"+sendUserId);
    	   message=message.substring(0, message.lastIndexOf("["));//发送的消息
    	   System.err.println("客户端发来的信息:"+message);
        try {
        	MyWebSocketController myWebSocket= ((MyWebSocketController) WebSocketMapUtil.get(userId));
            if(myWebSocket != null){
            	if("0".equals(tit)) {
            		//单发
            		myWebSocket.sendMessage(message);
            	}else if ("1".equals(tit)) {
            		//调用群发方法
          		  myWebSocket.sendMessageAll(message);
				}
              
            }else {
				//不在线
            	System.out.println("不在线");
            	if(messageMap.get(userId)==null) {
            		//用户不在线时 第一次给他发消息
            		Map<String, List<Object>> maps=new HashMap<>();//该用户的所有消息
            		List<Object> list=new ArrayList<>();//该用户发的离线消息的集合
            		list.add(message);
            		maps.put(sendUserId, list);
            		messageMap.put(userId, maps );
            	}else {
            		//不在线再次发送消息
            		//给用户的所有消息
            		Map<String,List<Object>> listObject=messageMap.get(userId);
            		List<Object> objects=new ArrayList<>();
            			if(listObject.get(sendUserId)!=null) {//这个用户给收消息的这个用户发过消息
            				//此用户给该用户发送过离线消息(此用户给该用户发过的所有消息)
            				objects=listObject.get(sendUserId);
            				objects.add(message);//加上这次发送的消息
            				//maps.put(sendUserId, objects);
            				//替换原来的map
            				listObject.put(sendUserId, objects);         				
            			}else {//这个用户没给该用户发送过离线消息
                			objects.add(message);
                			listObject.put(sendUserId, objects);
            			}
            		//Map<String, List<Object>> map=new HashMap<>();
//            		for(Map<String, List<Object>> map : listObject) {//遍历该用户的所有未发送的集合
//            			if(map.get(sendUserId).size()>0) {//这个用户给他发送过离线消息
//            				objects=map.get(sendUserId);
//            				objects.add(message);
//            			}
//            		}
            	
            		//maps.put(sendUserId, message);
            		messageMap.put(userId, listObject );
            		
            	}
			}
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 发生错误时调用
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error){
    	   System.out.println("错误");
        error.printStackTrace();
        
    }
 
 
    /**
     * 发送消息方法。
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message) throws IOException{
    	  // System.out.println("发消息");
        this.session.getBasicRemote().sendText(message);
    }
 
    /**
     * 群发消息方法。
     * @param message
     * @throws IOException
     */
    public void sendMessageAll(String message) throws IOException{
    	   System.out.println("群发");
        for(MyWebSocketController myWebSocket : WebSocketMapUtil.getValues()){
            myWebSocket.sendMessage(message);
        }
    }
    
    
    /**
     * 获取该用户未读的消息数量
     * @param userId 当前用户id
     * @param objectUserId  对象id
     * @return
     */
    public int getMessageCount(String userId,String objectUserId) {
    	//获取该用户所有未收的消息
    	Map<String, List<Object>> listMap=messageMap.get(userId);
    	if(listMap != null) {
    		List<Object> list=listMap.get(objectUserId);
    		if(list!=null) {
    			return listMap.get(objectUserId).size();
    		}else {
    			return 0;
    		}
        	
    	}else {
    		return 0;
    	}
    	
    }




}

这里是需要用到的一个工具类 

package com.Utils;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.caesar.controller.MyWebSocketController;




/**
 * 工具类  用来存放删除获取用户
 * @author 史**
 *
 * 2018年9月17日
 */
public class WebSocketMapUtil {
    public static ConcurrentMap<String, MyWebSocketController> webSocketMap = new ConcurrentHashMap<String, MyWebSocketController>();
    public static void put(String key, MyWebSocketController myWebSocket){
        webSocketMap.put(key, myWebSocket);
    }
 
    public static MyWebSocketController get(String key){
         return webSocketMap.get(key);
    }
 
    public static void remove(String key){
         webSocketMap.remove(key);
    }
 
    public static Collection<MyWebSocketController> getValues(){
        return webSocketMap.values();
    }

}

这里因有一些朋友需要前台代码所以补充一下前台调用的代码(因为这个当初是通过小程序调用的 所以补充的是小程序的前台调用代码,html的代码调用也类似)

// pages/message/chat/chat.js
//获取应用实例
const app = getApp();
/*路径 */
var apiURL = require('../../../apiURL');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiveUserId:'',//接收消息的用户id
    chatRecord:[],//聊天记录
    objectuser:[],//聊天对象信息
    userInfo:[],//用户信息
    chatListId:'',//聊天室id
    status:'',//聊天室转态
    value:'',
    height:'',//设备高度
    scrollTop:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    // _this.getChatRecord();
    //将json字符串转为json对象
    //对象信息
    var objectuser=JSON.parse(options.objectuser);
    //聊天室id
    var chatListId = options.chatListId;
    if(chatListId == ''){//聊天室id为空说明不是从聊天室列表过来的 所以查询和该用户是否有聊天室
      _this.isChatList(options.objectuserid, app.globalData.userId);
    }else{
      //查聊天室的聊天记录
      _this.getChatRecord(chatListId, app.globalData.userId);
      _this.setData({
        chatListId: chatListId,
      })
    }
    //查用户信息
    _this.getUserInfo(app.globalData.userId);
    _this.data.receiveUserId = options.objectuserid;

    _this.setData({
      objectuser: objectuser,
    })
    if (!app.globalData.communication) {
      //通讯没有连接  向后台发起连接
      wx.connectSocket({//传入对象id    当前用户id和对象用户id
        url: apiURL.wxLinkMessage.linkMessages + app.globalData.userId + "," + options.objectuserid,
        header: {
          'content-type': 'application/json'
        },

        method: "GET"
      })
    }
   
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
    //连接成功
    wx.onSocketOpen(function () {
      console.log('连接成功');
      app.globalData.communication = true
    })
    //连接失败
    wx.onSocketError(function (res) {
      app.globalData.communication = false
      console.log('WebSocket连接打开失败,请检查!')
    })

    //接收
    wx.onSocketMessage(function (res) {
      console.log("接收服务器发过来的消息")
      console.log(res)
    })
    wx.onSocketClose(function (res) {
      console.log('WebSocket 已关闭!')
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   // console.log("++++++++++++++++++++++++++++++")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
     app.globalData.communication=false;
      //关闭当前连接
      wx.closeSocket()
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  sendMessage: function (e) {
    var _this=this;
    // console.log(e.detail.value.message)
    var message = e.detail.value.message
    if (_this.Trim(message) == null || _this.Trim(message) == ''){
      return wx.showToast({
        title: '信息不能为空',
        icon: 'none',
        duration: 2000
      })
    }
      //封装消息类型   消息内容 + "[" + 发送消息的人 + ";" + 接收消息的人","+0
      //0是单发1是群发
    var cotnt = message + "[" + app.globalData.userId + ";" + _this.data.receiveUserId + ',' + 0;
    //保存聊天记录
    _this.save(cotnt);
    wx.sendSocketMessage({
      // message + "[" + "1c3546026fe24ff08d28115d58063263" + ";" + '944b50fd0eb64c4c816a4f3bf3203842' + ',' + 0,
      data: cotnt,
    })
    _this.setData({
      value:''
    })
    if (_this.data.status == 0 || _this.data.status==''){
      //修改聊天室状态
      // console.log("修改")
      _this.update()
    }
  },

  /**
 * 去空格
 */
  Trim: function (str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
  },

  /**
   * 查询两个人的聊天记录
   */
  getChatRecord: function (chatListId,sendUserId){
   // console.log(sendUserId)
    var _this = this;
    wx.request({
      url: apiURL.wxChatRecord.getChatRecord,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'

      },
      data: { "chatListId": chatListId, "sendUserId": sendUserId },
      success: function (data) {
          console.log(data)
        var chatList = data.data.data;
        var len = 10000 * chatList.length;
        if (data.data.code == 200) {
          return _this.setData({
            chatRecord: chatList,
            scrollTop: len//定位在最下方 显示最后一条信息
          })
        }
        wx.showToast({
          title: '系统错误',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },



  // getUserInfo: function () {
  //   var that = this
  //   wx.getSetting({
  //     success(res) {
  //       if (!res.authSetting['scope.userInfo']) {
  //         wx.authorize({
  //           scope: 'scope.userInfo',
  //           success() {
  //             that.UserLogin();
  //           }
  //         })
  //       }
  //       else {
  //         that.UserLogin();
  //       }
  //     }
  //   })
  // },
/**
 * 查用户信息
 */
  getUserInfo:function(userId){
    var _this = this;
    //先从缓存拿取  缓存里没有再从 数据库拿取
    //console.log("查询用户信息走缓存")
    var userInfo = wx.getStorageSync(userId);
    // console.log(userInfo)
    if (userInfo) {
      //成功获取到数据绑定到页面
      _this.setData({
        userInfo: userInfo
      })
    } else {
      // console.log("缓存查不到用户信息,向后台发起请求")
      //查询不到向后台发起请求
      //查询用户信息
      wx.request({
        url: apiURL.wxUser.getBaseInfo,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded' // post请求时需改成这个才行 否则后台接收不到参数
        },
        data: { "id": userId},
        dataType: "json",
        success: function (data) {
          // console.log(data)
          //获取失败弹窗
          if (data.data.code != 200) {
            wx.showToast({
              title: '系统错误',
              icon: 'none',
              duration: 2000
            })

          } else {
            // console.log("查询成功")
            // console.log(data.data)
            //成功获取数据 放到缓存 
            wx.setStorageSync(app.globalData.userId, data.data.result)
            //同时将数据绑定
            _this.setData({
              userInfo: data.data.result
            })

          }

        }
      })
    }
  },
/**
 * 根据双方id查聊天室是否存在
 */
  isChatList: function (currentUserId, objectUserId){
    var _this = this;
    wx.request({
      url: apiURL.wxChatList.isGetChatList,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'

      },
      data: { "currentUserId": currentUserId, "objectUserId": objectUserId },
      success: function (data) {
        // console.log(data)
        //查聊天室的聊天记录
        _this.getChatRecord(data.data.data.id, app.globalData.userId);
        _this.setData({
          chatListId: data.data.data.id,
          status: data.data.data.status,
          
        })
      }
         
    })
  },
  /**
   * 保存聊天记录
   */
  save: function (cotnt){
    var _this=this;
    wx.request({
      url: apiURL.wxChatRecord.save,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { "message": cotnt },
      success: function (res) {
        // console.log(res.data.data)
        var chatRecord=  _this.data.chatRecord;
        chatRecord.push(res.data.data)
        // console.log(chatRecord)
        var len = 1000 * chatRecord.length;
        _this.setData({
          users: res.data,
          userId: app.globalData.userId,
          chatRecord:chatRecord,
          scrollTop: len,//使新发出的信息显示在最下面
        })


      }
    })
  },

  /**
   * 修改聊天室转态
   */
  update: function (){
    var _this=this;
    wx.request({
      url: apiURL.wxChatList.update,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { "id": _this.data.chatListId, "status": 1},
      success: function (res) {
        // console.log(res.data)
        if(res.data.code==200){
          _this.setData({
            status: 1
          })
        }
       


      }
    })
  },

  /**
   * 跳到对象用户详情
   */
  sendOtherUser(e){
    console.log(e)
    var _this=this;
    wx.navigateTo({
      url: '../../other/personDetails/personDetails?userId=' + _this.data.receiveUserId,
    })
  },
  /**
   * 查看我的详情
   */
  sendUser:function(){
    wx.navigateTo({
      url: '../../my/my?userId=' + app.globalData.userId,
    })
  }
  
})

  • 28
    点赞
  • 167
    收藏
    觉得还不错? 一键收藏
  • 28
    评论
好的,我可以为你介绍一下使用RabbitMQ和Spring Boot的WebSocket实现推送的方法。 首先,我们需要引入以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 接着,我们需要配置RabbitMQ: ```yml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` 然后,我们需要创建一个RabbitMQ的配置类: ```java @Configuration public class RabbitMQConfig { public static final String QUEUE_NAME = "user-online-queue"; public static final String EXCHANGE_NAME = "user-online-exchange"; public static final String ROUTING_KEY = "user-online-routing-key"; @Bean public Queue queue() { return new Queue(QUEUE_NAME, true); } @Bean public DirectExchange exchange() { return new DirectExchange(EXCHANGE_NAME); } @Bean public Binding binding(Queue queue, DirectExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY); } } ``` 这里创建了一个队列、一个交换机和一个绑定关系。 接着,我们需要创建一个WebSocket处理器: ```java @Component public class WebSocketHandler extends TextWebSocketHandler { private final SimpMessagingTemplate messagingTemplate; public WebSocketHandler(SimpMessagingTemplate messagingTemplate) { this.messagingTemplate = messagingTemplate; } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { // 处理客户端发送的消息 } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { // 处理客户端断开连接的事件 } @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 处理客户端连接成功的事件 } public void sendMessageToAll(String message) { messagingTemplate.convertAndSend("/topic/notification", message); } public void sendUserListToAll(List<String> users) { messagingTemplate.convertAndSend("/topic/userList", users); } } ``` 这里使用了Spring Boot的SimpMessagingTemplate来发送WebSocket消息。 最后,我们需要在控制器中使用RabbitMQ来通知WebSocket处理器: ```java @RestController public class UserController { private final WebSocketHandler webSocketHandler; private final RabbitTemplate rabbitTemplate; public UserController(WebSocketHandler webSocketHandler, RabbitTemplate rabbitTemplate) { this.webSocketHandler = webSocketHandler; this.rabbitTemplate = rabbitTemplate; } @PostMapping("/user/login") public void login(@RequestParam String username) { // 处理用户登录的逻辑 // ... // 发送用户在线通知 rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, username); // 推送当前在线用户列表 List<String> userList = // 获取当前在线用户列表 webSocketHandler.sendUserListToAll(userList); } @PostMapping("/user/logout") public void logout(@RequestParam String username) { // 处理用户退出的逻辑 // ... // 推送当前在线用户列表 List<String> userList = // 获取当前在线用户列表 webSocketHandler.sendUserListToAll(userList); } @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME) public void handleUserOnline(String username) { // 推送用户在线通知 webSocketHandler.sendMessageToAll(username + " 上线了"); } } ``` 这里使用了RabbitListener注解来监听RabbitMQ队列,当有新的消息,会调用handleUserOnline方法来通知WebSocket处理器。 以上就是利用RabbitMQ发送消息通知,使用Spring Boot的WebSocket实现推送在线用户列表和实现推送通知信息的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值