SpringBoot+Websocket实现即时通讯

·  阅读 3208
SpringBoot+Websocket实现即时通讯

1. 介绍

基于Spring Boot和websocket实现点对点在线聊天和简单的机器人自动回复功能,学生可选择在线空闲的老师咨询,无在线老师接入机器人根据学生提问的内容和问题编号从问题库中获取对应的答案来回复学生。

2.后台

2.1. 引入依赖

    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-websocket', version: '2.0.4.RELEASE'
复制代码

2.2. websocket 配置类

WebsocketConfig.java

@Configuration
public class WebSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
复制代码

2.3. 封装显示消息对象

@Data
public class MessageVo {
    @ApiModelProperty(value = "用户id")
    private Integer userId;

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "消息内容")
    private String msg;

    @ApiModelProperty(value = "在线人数")
    private int count;
}
复制代码

2.4. 封装进行对话的对象

用来存放会话的用户信息

@Data
@ApiModel(description = "websocket会话对象")
public class SocketUserInfo extends BaseEntity {


    //用户sessionId
    private String sessionId;

    //用户session
    private Session session;

    //目标用户sessionid
    private String targetSessionId;

    //用户角色
    private String userRole;

    //用户id
    private Integer userId;
    //用户名
    private String userName;
    //联系电话
    private String tel;
复制代码

2.5. websocket处理类

@Slf4j
@Component
@ServerEndpoint(value = "/groupChat/{targetSessionId}/{userId}",configurator = SpringContextUtil.class)
public class WebSocketServerController{

    //用本地线程保存session
    private static ThreadLocal<Session> sessions = new ThreadLocal<Session>();
    //保存所有连接上的用户的session sessionId作为key
    private static Map<String, SocketUserInfo> userSessionMap = new ConcurrentHashMap<>();
    //保存老师的session
    private static Map<String, SocketUserInfo> serverSessionMap = new ConcurrentHashMap<>();

    //保存在线的学生
    private static List<Map<String,Object>> studentUserMapList = new ArrayList<>();

    //保存在线空闲的老师信息
    private static List<Map<String,Object>> teacherUserMapList = new ArrayList<>();

    @Autowired
    private CacheProxy cacheProxy;

    @Autowired
    private BuiProblemAnswerServiceImpl buiProblemAnswerService;

    /**
     * 建立连接调用的方法,群成员加入
     *
     * @param session
     * @param targetSessionId 学生咨询的对象的session id
     */
    @OnOpen
    public void onOpen(Session session,
                       @PathParam("targetSessionId") String targetSessionId,
                       @PathParam("userId") Integer userId) {
        try {
            MessageVo messageVo = new MessageVo();
            //保证各个线程里的变量相对独立于其他线程内的变量
            sessions.set(session);
            //获取用户信息
            SysLoginUserServiceImpl sysLoginUserService = SpringContextUtil.getBean(SysLoginUserServiceImpl.class);
            Map<String, Object> map = sysLoginUserService.queryUserInfoById(userId);
            //老师上线
            if ("10000".equals(targetSessionId)){
                //如果是老师和管理员角色,创建一个在线聊天信息
                SocketUserInfo serverInfo = new SocketUserInfo();
                serverInfo.setUserRole("老师");
                serverInfo.setSession(session);
                serverInfo.setUserId(userId);

                serverInfo.setSessionId(session.getId());
                serverInfo.setUserName(StrUtil.clearNull(map.get("userName")));
                serverInfo.setTerminalCode(StrUtil.clearNull(map.get("tel")));

                map.put("sessionId",session.getId());

                //将老师信息存入到list中
                teacherUserMapList.add(map);
                //将在线老师信息写入缓存
                cacheProxy.set("websocket",teacherUserMapList);

                messageVo.setMsg(session.getId()+"您已经上线了");
                messageVo.setUserId(userId);
                sendMsg(session,messageVo);

                //将老师的信息保存到map中
                serverSessionMap.put(session.getId(),serverInfo);
            }

            if (!"10000".equals(targetSessionId)){//学生上线 创建一个在线学生信息
                SocketUserInfo userInfo = new SocketUserInfo();
                userInfo.setSessionId(session.getId());
                userInfo.setSession(session);
                userInfo.setUserRole("学生");
                userInfo.setUserId(userId);
                userInfo.setUserName(StrUtil.clearNull(map.get("userName")));
                userInfo.setTerminalCode(StrUtil.clearNull(map.get("tel")));

                //获取对应的老师信息
                if (!targetSessionId.equals("kefu")){
                    SocketUserInfo serverInfo = serverSessionMap.get(targetSessionId);
                    //将该老师的连接设置为当前学生
                    serverInfo.setTargetSessionId(session.getId());
                    //将当前学生的连接对象设置为该老师
                    userInfo.setTargetSessionId(targetSessionId);

                    //将当前咨询的老师从空闲的列表中删除
                    for (int i = 0; i < teacherUserMapList.size(); i++) {
                        Map<String, Object> teacherMap = teacherUserMapList.get(i);
                        if (teacherMap.get("sessionId").equals(targetSessionId)){
                            teacherUserMapList.remove(i);
                        }
                    }
                    //更新可咨询的老师列表
                    cacheProxy.set("websocket",teacherUserMapList);

                    //给学生发送一条信息
                    messageVo.setMsg("老师"+serverInfo.getUserName()+"正在为你答疑!");
                    messageVo.setCount(2);
                    messageVo.setUserId(serverInfo.getUserId());
                    sendMsg(userInfo.getSession(), messageVo);

                    //给老师发送一条信息
                    messageVo.setMsg("正在为学生"+userInfo.getSessionId()+"答疑!");
                    messageVo.setUserId(userInfo.getUserId());
                    sendMsg(serverInfo.getSession(), messageVo);
                }
                if ("kefu".equals(targetSessionId)){
                    userInfo.setTargetSessionId("kefu");
                    messageVo.setMsg("请根据右侧的列表输入要您咨询的问题编号!!!");
                    sendMsg(session,messageVo);
                }
                
                //将在线用户信息保存到map中
                userSessionMap.put(session.getId(), userInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        }

    /**
     * 收到消息调用的方法,群成员发送消息
     *
     * @param message
     * @param session
     */
    @OnMessage
    public void onMessage(String message, Session session){
        HashMap<String, String> result = new HashMap<>();
        MessageVo messageVo = new MessageVo();
        SocketUserInfo serverInfo = serverSessionMap.get(session.getId());
        if (serverInfo != null) {//老师消息
            log.info("老师"+ session.getId()+"发送消息:\""+ message +"\"给学生"+serverSessionMap.get(session.getId()).getTargetSessionId());
            result.put("msg", "老师"+session.getId()+":"+message);
            messageVo.setMsg(message);
            messageVo.setUserId(serverInfo.getUserId());
            messageVo.setCount(2);
            //要判断是否绑定到的学生如果有就将消息传递到学生
            if (null != serverInfo.getTargetSessionId()){
                //将消息发送给绑定的学生
                sendMsg(userSessionMap.get(serverSessionMap.get(session.getId()).getTargetSessionId()).getSession(), messageVo);
            }
            sendMsg(session,messageVo);
        } else {//学生消息
            System.out.println("学生"+ session.getId()+"发送消息:\""+ message +"\"给老师"+userSessionMap.get(session.getId()).getTargetSessionId());
            result.put("msg", "学生"+session.getId()+":"+message);
            messageVo.setCount(2);
            messageVo.setMsg(message);
            messageVo.setUserId(userSessionMap.get(session.getId()).getUserId());
            //学生发出一条消息
            sendMsg(session,messageVo);
            //判断是否绑定了老师,如果有就发送消息
            String targetSessionId = userSessionMap.get(session.getId()).getTargetSessionId();
            if (null !=  targetSessionId && !targetSessionId.equals("kefu")){
                //给当前学生咨询的老师发送消息内容
                messageVo.setUserId(userSessionMap.get(session.getId()).getUserId());
                sendMsg(serverSessionMap.get(userSessionMap.get(session.getId()).getTargetSessionId()).getSession(), messageVo);
            }
            if (null !=  targetSessionId && targetSessionId.equals("kefu")){
                try {
                    Long problemNumber = Long.valueOf(StrUtil.clearNull(message));
                    String answer = buiProblemAnswerService.getAnswer(problemNumber);
                    messageVo.setMsg(answer);
                    if (answer == null){
                        messageVo.setMsg("请输入正确的问题编号!!!");
                    }
                }catch (Exception e){
                    List<BuiProblemAnswer> buiProblemAnswerList = buiProblemAnswerService.getAnswerList(message);
                    if (buiProblemAnswerList.size() > 1){
                        String msg = "猜你想问:\t";
                        for (BuiProblemAnswer buiProblemAnswer : buiProblemAnswerList) {
                            msg = msg + buiProblemAnswer.getProblemTopic()+"\t";
                        }
                        messageVo.setMsg(msg);
                    }else if (buiProblemAnswerList.size() == 1){
                        messageVo.setMsg(buiProblemAnswerList.get(0).getProblemAnswer());
                    }else if (buiProblemAnswerList.size() == 0){
                        messageVo.setMsg("抱歉!没有找到您的问题!");
                    }

//                    messageVo.setMsg("猜你想问:\t"+);
//                    messageVo.setMsg("请输入正确的问题编号!!!");
                }
            }
            //回复学生消息
            messageVo.setUserId(null);
            sendMsg(session,messageVo);
        }
    }

    /**
     * 关闭连接调用的方法,群成员退出
     *
     * @param session
     * @param targetSessionId
     */
    @OnClose
    public void onClose(Session session, @PathParam("targetSessionId") String targetSessionId, @PathParam("userId") Integer userId) {
        SocketUserInfo serverInfo = serverSessionMap.get(session.getId());
        MessageVo messageVo = new MessageVo();
        if (serverInfo != null){
            //老师下线,将老师从map中移除
            for (int i = 0; i < teacherUserMapList.size(); i++) {
                Map<String, Object> map = teacherUserMapList.get(i);
                if (userId == map.get("userId")){
                    teacherUserMapList.remove(i);
                }

            }
            //更新缓存中的在线老师的数据
            cacheProxy.set("websocket",teacherUserMapList);
            serverSessionMap.remove(session.getId());
            //查看当前是否有在服务的对象,如果有就给用户发送系统错误的信息
            if (serverInfo.getTargetSessionId() != null){
                messageVo.setMsg("系统错误,老师已经下线了");
                messageVo.setUserId(serverInfo.getUserId());
                sendMsg(userSessionMap.get(serverInfo.getTargetSessionId()).getSession(),messageVo);
            }
            log.info("老师编号:" + session.getId() + "退出了连接,当前在线老师共计:" + serverSessionMap.size());
        }else if ("10000".equals(targetSessionId)){
            //学生下线 从老师中解绑,解绑后,如果还有在排队的学生,则让这个学生和当前老师绑定起来
            userSessionMap.remove(session.getId());

            //获取当前老师信息
            SocketUserInfo serverUserInfo = serverSessionMap.get(targetSessionId);
            HashMap<String, Object> teacherMap = new HashMap<>();
            teacherMap.put("userName",serverUserInfo.getUserName());
            teacherMap.put("serssionId",serverUserInfo.getSessionId());
            teacherMap.put("tel",serverUserInfo.getTerminalCode());
            teacherMap.put("userId",serverUserInfo.getUserId());

            //将当前老师添加到空闲老师列表并更新缓存
            teacherUserMapList.add(teacherMap);
            cacheProxy.set("websocket",teacherUserMapList);

            log.info("用户编号:" + session.getId() + "退出了连接,当前在线学生共计:" + userSessionMap.size());
        }

    }

    /**
     * 传输消息错误调用的方法
     *
     * @param error
     */
    @OnError
    public void OnError(Throwable error) {
        log.info("Connection error");
    }


    //查询排队用户
    private synchronized String findLineUser(){
        //判断是否有用户
        if (userSessionMap.size() > 0){
            //遍历所有用户,查找一个排队的用户
            for (SocketUserInfo UserInfo: userSessionMap.values()) {
                if (null == UserInfo.getTargetSessionId()){
                    return UserInfo.getSessionId();
                }
            }
        }
        return null;
    }

    //查询在线空闲的老师
    private synchronized String findFreeServer(){
        //判断是否有老师
        if (serverSessionMap.size() > 0){
            //遍历在线的所有老师,查找一个空闲的老师
            for (SocketUserInfo serverInfo: serverSessionMap.values()) {
                if (null == serverInfo.getTargetSessionId()){
                    return serverInfo.getSessionId();
                }
            }
        }
        return null;
    }

    //统一的发送消息方法
    private synchronized void sendMsg(Session session, MessageVo messageVo) {
        try {
            session.getBasicRemote().sendText(JSON.toJSONString(messageVo));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
复制代码

websocket中无法注入bean实例的问题: 解决:

1.png

)6NYGKBASHCB{AUT){9K0C9.png

这样就可以使用@Autowired来注入实例了

最后前端就可以使用 'ws://ip地址:端口/项目地址/groupChat/{参数1}/{参数2}' 来请求连接了

注意:如果后台做了用户认证配置了拦截器需要放行接口

2.png

分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改