Springboot简易聊天室

前言:小刘整合并使用SpringBoot+socket.io+netty 实现及时通讯系统

难点:1.socket.io的消息推送

           2.集群下netty网络通讯

           3.私聊群聊的逻辑实现

           5.高并发的性能提高

简图:

视频:

核心代码:

    socket.on('SINGLE_MSG', function(data) {
        // console.log("收到服务端发送的消息")
        console.log(userId+"服务器发来私聊消息"+JSON.stringify(data))
        //私聊
        let p="<p>"+JSON.stringify(data)+"</p>";
        $("#cfL").append(p)
    });

后端私聊和群发 

 /**群发
     * 广播消息
     */
    public static void sendBroadMsg(List<RespMsg>respMsgList) {

        log.info("=sendBroadMsg==websocket发起响应===");
        respMsgList.stream().forEach(t->{
            log.info(JSONObject.toJSONString(t));
            if (socketIOClientMap.containsKey(t.getReceiveId())){
                log.info("====sendBroadMsg===websocket---receiveId========"+t.getReceiveId());
                SocketIOClient client = socketIOClientMap.get(t.getReceiveId());
                if (client.isChannelOpen()){
                    client.sendEvent(GROUP_MSG, t, System.currentTimeMillis());
                }
            }
        });
    }
    /**
     * 私发
     * @param
     */
    public static void sendSingleMsg(RespMsg resp){
        SocketIOClient acceptClient = socketIOClientMap.get(resp.getReceiveId());
        if (acceptClient!=null&&acceptClient.isChannelOpen()){
            log.info(resp.getSendId()+"发送消息sendSingleMsg给receiveId"+resp.getReceiveId());
            acceptClient.sendEvent(SINGLE_MSG,resp, System.currentTimeMillis());
        }else {
            log.info("sendSingleMsg本系统接收方不在线!");
        }

        SocketIOClient sendClient = socketIOClientMap.get(resp.getSendId());
        if (sendClient!=null&&sendClient.isChannelOpen()){
            log.info(resp.getSendId()+"发送消息给自身"+resp.getSendId());
            sendClient.sendEvent(SINGLE_MSG, resp, System.currentTimeMillis());
        }else {
            log.info("本系统发送方不在线!");
        }
    }

无偿免费分享源码以及技术和面试文档,更多优秀精致的源码技术栈分享请关注微信公众号:gh_817962068649 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值