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的多终端接收消息

验证用户88接收消息:


验证用户88接收消息

总结

websocekt接口在mvc模式中被service层调用需要在@ServerEndpoint中添加configurator属性,而实际情况需要根据业务需求来进行设计。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容