websocket在springboot中的实现

1.maven依赖:

<!--webSocket-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

2.配置类:


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

3.实现类

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.message.mapper.SysMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint(value = "/managerWebsocket/{userId}")
@Component
@Slf4j
public class ManagerWebsocket {

    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;
    //concurrent包的线程安全map,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
    private static ConcurrentHashMap<String, ManagerWebsocket> webSocketMap = new ConcurrentHashMap<String, ManagerWebsocket>();
    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session webSocketsession;
    //当前发消息的人员编号
    private String userId;

    @Autowired
    private SysMessageMapper sysMessageMapper;


    /**
     * 连接建立成功调用的方法
     *
     * @param webSocketsession 可选的参数。webSocketsession为与某个客户端的连接会话,需要通过它来给客户端发送数据
     */
    @OnOpen
    public void onOpen(@PathParam(value = "userId") String userId, Session webSocketsession, EndpointConfig config) {
        if(StringUtils.isEmpty (userId)) return;
        this.userId = userId;//接收到发送消息的人员编号
        this.webSocketsession = webSocketsession;
        webSocketMap.put(this.userId, this);//加入map中
        addOnlineCount();           //在线数加1
        sendNotReadNumToUser (userId);
        log.info ("有新连接加入:" + userId + ",当前在线人数为" + getOnlineCount());
    }


    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        if (!userId.equals("")) {
            webSocketMap.remove(userId);  //从set中删除
            subOnlineCount();           //在线数减1
            log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
        }
    }


    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息
     * @param session 可选的参数
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info ("来自客户端的消息:" + message);
        //群发消息
        /*if (1 < 2) {
            sendAll(message);
        } else {
            //给指定的人发消息
            sendToUser(message);
        }*/
    }


    /**
     * 给指定的人发送消息,聊天场景
     * @param message
     */
    /*public void sendToUser(String message) {
        String senduserId = message.split("[|]")[1];
        String sendMessage = message.split("[|]")[0];
        String now = getNowTime();
        try {
            if (webSocketMap.get(senduserId) != null) {
                webSocketMap.get(senduserId).sendMessage(now + "用户" + userId + "发来消息:" + " <br/> " + sendMessage);
            } else {
                log.info ("当前用户不在线");
            }
        } catch (IOException e) {
            log.error (e.getMessage (), e);
        }
    }*/


    /**
     * 给所有人发消息,聊天场景
     * @param message
     */
    /*private void sendAll(String message) {
        String now = getNowTime();
        String sendMessage = message.split("[|]")[0];
        //遍历HashMap
        for (String key : webSocketMap.keySet()) {
            try {
                //判断接收用户是否是当前发消息的用户
                if (!userId.equals(key)) {
                    webSocketMap.get(key).sendMessage(now + "用户" + userId + "发来消息:" + " <br/> " + sendMessage);
                    log.info ("key = " + key);
                }
            } catch (IOException e) {
                log.error (e.getMessage (), e);
            }
        }
    }*/

    /**
     * 给指定用户推送未读消息数
     * @param userId
     */
    public void sendNotReadNumToUser(String userId) {
        if (webSocketMap.get (userId) != null) {
            try {
                String notReadNum = String.valueOf (sysMessageMapper.countManagerNotRead (userId));
                webSocketMap.get(userId).sendMessage(notReadNum);
            } catch (Exception e) {
                log.error (e.getMessage (), e);
            }
        }

    }

    /**
     * 给全体在线用户推送未读消息数
     */
    public void sendNotReadNumToAll() {
        for (String user : webSocketMap.keySet()) {
            try {
                String notReadNum = String.valueOf (sysMessageMapper.countManagerNotRead (user));
                webSocketMap.get (user).sendMessage (notReadNum);
            } catch (Exception e) {
                log.error (e.getMessage (), e);
            }
        }

    }


    /**
     * 发生错误时调用
     *
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error ("发生错误");
        error.printStackTrace();
    }


    /**
     * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
     *
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message) throws IOException {
        //this.webSocketsession.getBasicRemote().sendText(message);
        this.webSocketsession.getAsyncRemote().sendText(message);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    private String getNowTime() {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        return time;
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }


    public static synchronized void addOnlineCount() {
        ManagerWebsocket.onlineCount++;
    }


    public static synchronized void subOnlineCount() {
        ManagerWebsocket.onlineCount--;
    }
}

注:注入的SysMessageMapper是项目的消息业务,与websocket无关

4.附上前端代码:
(1) 在 initSocket() 函数中我们新建 websocket 对象,通过调用这些 websocket 对象的内置函数实现数据的请求和接收:

initSocket();
function initSocket(){
  webSocket = new WebSocket('ws://'+window.location.host+'/managerWebsocket/'+loginInfo.userId);
  //webSocket 对象
  webSocket.onerror = function (event) {
      onError(event);
  };
  webSocket.onopen = function (event) {
      onOpen(event);
  };
  webSocket.onmessage = function (event) {
      onMessage(event);
  };

(2) 然后定义相应的函数,发生 http 请求,接收到数据后打印出来看一下数据格式,并进行处理

function onError(event){

}
function onOpen(event){
    webSocket.send();//看后台需要接收什么信息才能握手成功
}
function onMessage(event){
    console.log(event);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值