Sring MVC 模式下使用websocket

支持原创,原文链接

前言

九风的上一篇文章java WebSocket开发入门WebSocket介绍了websocket一些特征和提供了一个简单的demo,但上篇文章中的websocket类不能被Spring MVC中的controller层、server层调用,这篇文章提供了一个可用于MVC模式调用的版本。

程序运行环境说明

Server version : Apache Tomcat/7.0.65
JVM version: 1.7.0_75-b13

官网介绍

首先推荐大家看官网Oracle的javax.websocket介绍 ,官网对其中的ServerEndpoint的介绍是:ServerEndpoint是web socket的一个端点,用于部署在URI空间的web socket服务,该注释允许开发者去定义公共的URL或URI模板端点和其他一些websocekt运行时的重要属性,例如encoder属性用于发送消息。

上篇文章中的最简单的tomcat的websocket服务器如果需要在MVC模式中被其他类调用,需要配置Configurator属性:用于调用一些自定义的配置算法,如拦截连接握手或可用于被每个端点实例调用的任意的方法和算法;对于服务加载程序,该接口必须提供默认配置器加载平台。

spring MVC模式配置

对于spring MVC模式,ServerEndpoint的代码为:

@ServerEndpoint(value="/websocketTest/{userId}",configurator = SpringConfigurator.class)

需要在maven中导入SpringConfiguator的包:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-websocket</artifactId>
    <version>${spring.version}</version>
</dependency>

目标需求

平台有多个用户,每个用户可能会在多个终端上登录,当有消息到达时,实时将所有消息推送给所有所用登录的当前用户,就像QQ消息一样,用户在PC、pad、phone上同时登录时,将收到的消息同时推送给该用户的所有终端。

数据结构设计

每个用户对应多个终端,多个终端用Set来记录,用户使用Map来记录,所以数据存储结构为Map<userId, Set<Terminal>>,结合具体的数据类型就是如下所示的的存储结构:

//记录每个用户下多个终端的连接
private static Map<Long, Set<WebsocketDemo>> userSocket = new HashMap<>();
操作设计
  1. onOpen: 新建连接时需要判断是否是该用户第一次连接,如果是第一次连接,则对Map增加一个userId;否则将sessionid添加入已有的用户Set中。
    2.onClose:连接关闭时将该用户的Set中记录remove,如果该用户没有终端登录了,则移除Map中该用户的记录
  2. onMessage: 这个根据业务情况详细设计。
  3. 给用户的所有终端发送数据:遍历该用户的Set中的连接即可。
后台websocket连接代码
/**
 * @Class: WebsocketDemo
 * @Description:  给所用户所有终端推送消息
 * @author JFPZ
 * @date 2017年5月15日 上午21:38:08
 */
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.server.standard.SpringConfigurator;

//websocket连接URL地址和可被调用配置
@ServerEndpoint(value="/websocketDemo/{userId}",configurator = SpringConfigurator.class)
public class WebsocketDemo {
    //日志记录      
    private Logger logger = LoggerFactory.getLogger(WebsocketDemo.class);
    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;
   
    //记录每个用户下多个终端的连接
    private static Map<Long, Set<WebsocketDemo>> userSocket = new HashMap<>();
 
    //需要session来对用户发送数据, 获取连接特征userId
    private Session session;
    private Long userId;
   
    /**
     * @Title: onOpen
     * @Description: websocekt连接建立时的操作
     * @param @param userId 用户id
     * @param @param session websocket连接的session属性
     * @param @throws IOException
     */
    @OnOpen
    public void onOpen(@PathParam("userId") Long userId,Session session) throws IOException{
        this.session = session;
        this.userId = userId;
        onlineCount++;
        //根据该用户当前是否已经在别的终端登录进行添加操作
        if (userSocket.containsKey(this.userId)) {
            logger.debug("当前用户id:{}已有其他终端登录",this.userId);
            userSocket.get(this.userId).add(this); //增加该用户set中的连接实例
        }else {
            logger.debug("当前用户id:{}第一个终端登录",this.userId);
            Set<WebsocketDemo> addUserSet = new HashSet<>();
            addUserSet.add(this);
            userSocket.put(this.userId, addUserSet);
        }
        logger.debug("用户{}登录的终端个数是为{}",userId,userSocket.get(this.userId).size());
        logger.debug("当前在线用户数为:{},所有终端个数为:{}",userSocket.size(),onlineCount);
    }
   
    /**
     * @Title: onClose
     * @Description: 连接关闭的操作
     */
    @OnClose
    public void onClose(){
        //移除当前用户终端登录的websocket信息,如果该用户的所有终端都下线了,则删除该用户的记录
        if (userSocket.get(this.userId).size() == 0) {
            userSocket.remove(this.userId);
        }else{
            userSocket.get(this.userId).remove(this);
        }
        logger.debug("用户{}登录的终端个数是为{}",this.userId,userSocket.get(this.userId).size());
        logger.debug("当前在线用户数为:{},所有终端个数为:{}",userSocket.size(),onlineCount);
    }
   
    /**
     * @Title: onMessage
     * @Description: 收到消息后的操作
     * @param @param message 收到的消息
     * @param @param session 该连接的session属性
     */
    @OnMessage
    public void onMessage(String message, Session session) {    
        logger.debug("收到来自用户id为:{}的消息:{}",this.userId,message);
        if(session ==null)  logger.debug("session null");
    }
   
    /**
     * @Title: onError
     * @Description: 连接发生错误时候的操作
     * @param @param session 该连接的session
     * @param @param error 发生的错误
     */
    @OnError
    public void onError(Session session, Throwable error){
        logger.debug("用户id为:{}的连接发送错误",this.userId);
        error.printStackTrace();
    }
   
  /**
   * @Title: sendMessageToUser
   * @Description: 发送消息给用户下的所有终端
   * @param @param userId 用户id
   * @param @param message 发送的消息
   * @param @return 发送成功返回true,反则返回false
   */
    public Boolean sendMessageToUser(Long userId,String message){
        if (userSocket.containsKey(userId)) {
            logger.debug(" 给用户id为:{}的所有终端发送消息:{}",userId,message);
            for (WebsocketDemo WS : userSocket.get(userId)) {
                logger.debug("sessionId为:{}",WS.session.getId());
                try {
                    WS.session.getBasicRemote().sendText(message);
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.debug(" 给用户id为:{}发送消息失败",userId);
                    return false;
                }
            }
            return true;
        }
        logger.debug("发送错误:当前连接不包含id为:{}的用户",userId);
        return false;
    }
  
}

Service层编写

service层比较简单,不多说,直接看代码就行:

/** 
 * @Class: WebSocketMessageService
 * @Description:  使用webscoket连接向用户发送信息
 * @author JFPZ
 * @date 2017年5月15日 上午20:17:01
 */
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springside.modules.mapper.JsonMapper;
import com.hhxk.vrshop.entity.Message;
import com.hhxk.vrshop.web.webSocket.WebsocketDemo;
import com.hhxk.vrshop.web.webSocket.WebSocket;

@Service("webSocketMessageService")
public class WSMessageService {
    private Logger logger = LoggerFactory.getLogger(WSMessageService.class);
    //声明websocket连接类
    private WebsocketDemo websocketDemo = new WebsocketDemo();

    /**
     * @Title: sendToAllTerminal
     * @Description: 调用websocket类给用户下的所有终端发送消息
     * @param @param userId 用户id
     * @param @param message 消息
     * @param @return 发送成功返回true,否则返回false
     */
    public Boolean sendToAllTerminal(Long userId,String message){   
        logger.debug("向用户{}的消息:{}",userId,message);
        if(websocketDemo.sendMessageToUser(userId,message)){
            return true;
        }else{
            return false;
        }   
    }           
}

Controller层

Controller层也简单,只需提供一个http请求的入口然后调用service即可,Controller层请求地址为:http://127.0.0.1:8080/your-project-name/message/TestWS

@Controller
@RequestMapping("/message")
public class MessageController extends BaseController{
    //websocket服务层调用类
    @Autowired
    private WSMessageService wsMessageService;

  //请求入口
    @RequestMapping(value="/TestWS",method=RequestMethod.GET)
    @ResponseBody
    public String TestWS(@RequestParam(value="userId",required=true) Long userId,
        @RequestParam(value="message",required=true) String message){
        logger.debug("收到发送请求,向用户{}的消息:{}",userId,message);
        if(wsMessageService.sendToAllTerminal(userId, message)){
            return "发送成功";
        }else{
            return "发送失败";
        }   
    }
}
前端连接代码

html支持websocket,将下面代码中的websocket地址中替换即可直接使用:

<!DOCTYPE html>
<html>

    <head lang="en">
        <meta charset="UTF-8">
        <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
        <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
        <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
        <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        <title>webSocket-用户66</title>
        <script type="text/javascript">
            $(function() {
                var websocket;
                if('WebSocket' in window) {
                                        console.log("此浏览器支持websocket");
                    websocket = new WebSocket("ws://127.0.0.1:8080/your-project-name/websocketDemo/66");
                } else if('MozWebSocket' in window) {
                    alert("此浏览器只支持MozWebSocket");
                } else {
                    alert("此浏览器只支持SockJS");
                }
                websocket.onopen = function(evnt) {
                    $("#tou").html("链接服务器成功!")
                };
                websocket.onmessage = function(evnt) {
                    $("#msg").html($("#msg").html() + "<br/>" + evnt.data);
                };
                websocket.onerror = function(evnt) {};
                websocket.onclose = function(evnt) {
                    $("#tou").html("与服务器断开了链接!")
                }
                $('#send').bind('click', function() {
                    send();
                });

                function send() {
                    if(websocket != null) {
                        var message = document.getElementById('message').value;
                        websocket.send(message);
                    } else {
                        alert('未与服务器链接.');
                    }
                }
            });
        </script>
    </head>

    <body>
        <div class="page-header" id="tou">
            webSocket多终端聊天测试
        </div>
        <div class="well" id="msg"></div>
        <div class="col-lg">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="发送信息..." id="message">
                <span class="input-group-btn">
                    <button class="btn btn-default" type="button" id="send" >发送</button>
                </span>
            </div>
        </div>
    </body>

</html>

测试验证

使用用户id为66登录2个终端,用户id为88登录1个终端,服务器中可以看到登录的信息,如下图所示:

用户连接后台websocket服务器

首先验证用户66的两个终端接收是否能同时接收消息:


验证用户66的多终端接收消息


作者:九风萍舟
链接:https://www.jianshu.com/p/3398d0230e5f
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值