博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring WebSocket实现消息推送
阅读量:5370 次
发布时间:2019-06-15

本文共 9693 字,大约阅读时间需要 32 分钟。

第一步: 添加Spring WebSocket的依赖jar包

(注:这里使用maven方式添加 手动添加的同学请自行下载相应jar包放到lib目录)

org.springframework
spring-websocket
${spring.version}
org.springframework
spring-messaging
${spring.version}

第二步:建立一个类实现WebSocketConfigurer接口

package com.example.demo.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.socket.config.annotation.EnableWebSocket;import org.springframework.web.socket.config.annotation.WebSocketConfigurer;import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;import org.springframework.web.socket.handler.TextWebSocketHandler;@Configuration@EnableWebMvc@EnableWebSocketpublic class SpringWebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {        //springwebsocket 4.1.5版本前默认支持跨域访问,之后的版本默认不支持跨域,需要设置.setAllowedOrigins("*")        registry.addHandler(webSocketHandler(),"/websocket/socketServer.do")                .addInterceptors(new SpringWebSocketHandlerInterceptor());        registry.addHandler(webSocketHandler(), "/sockjs/socketServer.do")                .addInterceptors(new SpringWebSocketHandlerInterceptor()).withSockJS();    }    @Bean    public TextWebSocketHandler webSocketHandler(){        return new SpringWebSocketHandler();    }}

 第三步:继承WebSocketHandler对象。该对象提供了客户端连接,关闭,错误,发送等方法,重写这几个方法即可实现自定义业务逻辑

package com.example.demo.config;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.socket.CloseStatus;import org.springframework.web.socket.TextMessage;import org.springframework.web.socket.WebSocketSession;import org.springframework.web.socket.handler.TextWebSocketHandler;import java.io.IOException;import java.util.HashMap;import java.util.Map;public class SpringWebSocketHandler extends TextWebSocketHandler {    private static Logger logger = LoggerFactory.getLogger(SpringWebSocketHandler.class);    private static final Map
users; //Map来存储WebSocketSession,key用USER_ID 即在线用户列表 //用户标识 private static final String USER_ID = "WEBSOCKET_USERID"; //对应监听器从的key static { users = new HashMap
(); } public SpringWebSocketHandler() {} /** * 连接成功时候,会触发页面上onopen方法 */ public void afterConnectionEstablished(WebSocketSession session) throws Exception { System.out.println("成功建立websocket连接!"); String userId = (String) session.getHandshakeAttributes().get(USER_ID); users.put(userId,session); System.out.println("当前线上用户数量:"+users.size()); //这块会实现自己业务,比如,当用户登录后,会把离线消息推送给用户 //TextMessage returnMessage = new TextMessage("成功建立socket连接,你将收到的离线"); //session.sendMessage(returnMessage); } /** * 关闭连接时触发 */ public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { logger.debug("关闭websocket连接"); String userId= (String) session.getHandshakeAttributes().get(USER_ID); System.out.println("用户"+userId+"已退出!"); users.remove(userId); System.out.println("剩余在线用户"+users.size()); } /** * js调用websocket.send时候,会调用该方法 */ @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { super.handleTextMessage(session, message); /** * 收到消息,自定义处理机制,实现业务 */ System.out.println("服务器收到消息:"+message); if(message.getPayload().startsWith("#anyone#")){ //单发某人 sendMessageToUser((String)session.getHandshakeAttributes().get(USER_ID), new TextMessage("服务器单发:" +message.getPayload())) ; }else if(message.getPayload().startsWith("#everyone#")){ sendMessageToUsers(new TextMessage("服务器群发:" +message.getPayload())); }else{ } } public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { if(session.isOpen()){ session.close(); } logger.debug("传输出现异常,关闭websocket连接... "); String userId= (String) session.getHandshakeAttributes().get(USER_ID); users.remove(userId); } public boolean supportsPartialMessages() { return false; } /** * 给某个用户发送消息 * * @param userId * @param message */ public void sendMessageToUser(String userId, TextMessage message) { for (String id : users.keySet()) { if (id.equals(userId)) { try { if (users.get(id).isOpen()) { users.get(id).sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } break; } } } /** * 给所有在线用户发送消息 * * @param message */ public void sendMessageToUsers(TextMessage message) { for (String userId : users.keySet()) { try { if (users.get(userId).isOpen()) { users.get(userId).sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } } }}

第四步:继承HttpSessionHandshakeInterceptor对象。该对象作为页面连接websocket服务的拦截器,代码如下:

package com.example.demo.config;import org.springframework.http.server.ServerHttpRequest;import org.springframework.http.server.ServerHttpResponse;import org.springframework.http.server.ServletServerHttpRequest;import org.springframework.web.socket.WebSocketHandler;import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;import javax.servlet.http.HttpSession;import java.util.Map;public class SpringWebSocketHandlerInterceptor extends HttpSessionHandshakeInterceptor {    @Override    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,                                   Map
attributes) throws Exception { System.out.println("Before Handshake"); if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; HttpSession session = servletRequest.getServletRequest().getSession(false); if (session != null) { //使用userName区分WebSocketHandler,以便定向发送消息 String userName = (String) session.getAttribute("SESSION_USERNAME"); //一般直接保存user实体 if (userName!=null) { attributes.put("WEBSOCKET_USERID",userName); } } } return super.beforeHandshake(request, response, wsHandler, attributes); } @Override public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) { super.afterHandshake(request, response, wsHandler, ex); }}

第5步 让SpringWebSocketConfig配置类随spring容器启动 spring文件中加入如下代码:

-------------------------------------------------------------------------到这里就算完成啦 下面准备测试-------------------------------------------------------------

1.定义一个控制器用来做连接标识和发送消息

package com.example.demo.controller;import com.example.demo.config.SpringWebSocketHandler;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Controller;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.socket.TextMessage;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@Controllerpublic class WebSocketController {    @Bean//这个注解会从Spring容器拿出Bean    public SpringWebSocketHandler infoHandler() {        return new SpringWebSocketHandler();    }    @RequestMapping("/websocket/toLogin")    public ModelAndView toLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {        return new ModelAndView("login");    }    @RequestMapping("/websocket/login")    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception {        String username = request.getParameter("username");        System.out.println(username+"登录");        HttpSession session = request.getSession(false);        session.setAttribute("SESSION_USERNAME", username);        //response.sendRedirect("/quicksand/jsp/websocket.jsp");        return new ModelAndView("websocket");    }    @RequestMapping("/websocket/send")    @ResponseBody    public String send(HttpServletRequest request) {        String username = request.getParameter("username");        if (StringUtils.isEmpty(username)){            infoHandler().sendMessageToUsers(new TextMessage("给所有用户发消息"));        }else{            infoHandler().sendMessageToUser(username, new TextMessage("给"+username+"用户发消息"));        }        return null;    }}

2.建立登录页面

<%@ page language="java" contentType="text/html; charset=utf-8"         pageEncoding="utf-8"%>

Hello World!

登录名:

3.建立发消息页面

<%@ page language="java" contentType="text/html; charset=utf-8"         pageEncoding="utf-8"%>    
Insert title here请输入:

测试结果如下图:

 

转载于:https://www.cnblogs.com/huangjinyong/p/10901806.html

你可能感兴趣的文章
第四次实训作业
查看>>
转载:OpenCV2.2无法打开摄像头的解决办法
查看>>
110303_Common Permutation(公共排列)
查看>>
Login Verification CodeForces - 928A (实现)
查看>>
windows RT Xamltoolkit
查看>>
tomcat的request和response小案例
查看>>
c# 与 Mysql 的通讯方式总结
查看>>
线程问题---在iPad上会崩溃,在iPhone上不会崩溃
查看>>
解决 vim 报错:the imp module is deprecated in favour of importlib
查看>>
LeetCode Happy Number
查看>>
Oozie_示例
查看>>
tcp三次握手,四次挥手
查看>>
网易JS面试题与Javascript词法作用域说明
查看>>
PHP session 写入数据库中的方法
查看>>
HDU 3639 Bone Collector II(01背包第K优解)
查看>>
python-接口开发flask模块(三)开发登陆接口
查看>>
一个简单的定时器
查看>>
使用jackson工具类把对象或集合转为JSON格式
查看>>
使用Ajax向服务器端发送请求
查看>>
Java字节数组转按radix进制输出
查看>>