ssm框架中集成websocket实现服务端主动向客户端发送消息

2018年07月22日 其他资源 1条评论 阅读1949次

找了很多配置文档及实例说明,也还是没能成功,最终在csdn博客中发现了基于stomp的消息推送的文章,
下面整理自csdn博客,https://blog.csdn.net/u013627689/article/details/73611945


1. 加入spring的webSocket包,必须是4.0以上的包

加入jar的依赖:
spring-messaging-{spring.version}.jar 
spring-websocket-{spring.version}.jar 


2. 通信要异步处理,所以在 web.xml 里的filter 和servlet 都要加上异步的支持,默认是同步的。


<async-supported>true</async-supported>


3. 在spring-mvc配置文件中,声明bean


<bean
 class="org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean">
 <property name="maxTextMessageBufferSize" value="8192" />
 <property name="maxBinaryMessageBufferSize" value="8192" />
 <property name="maxSessionIdleTimeout" value="900000" />
 <property name="asyncSendTimeout" value="5000" />
</bean>
<!-- 实时消息推送,在spring容器初始化此bean的时候就启动了监听线程, -->
<bean id="webSocketMessageUtil" class="net.jileniao.ssm.websocket.WebSocketMessageUtil" />



4. net.jileniao.ssm.websocket.WebSocketMessageUtil的代码


package net.jileniao.ssm.websocket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.util.StringUtils;
/**
 * 这里启动一个线程不停的监控队列是否有消息需要进行发送,如果有就发送出去。
 *
 */
public class WebSocketMessageUtil implements Runnable {
    static Logger log = Logger.getLogger(WebSocketMessageUtil.class);
    private static SimpMessagingTemplate messageingTemplate;
    private static BlockingQueue<WebSocketMessage> wsQueue = new LinkedBlockingDeque<>();
    public WebSocketMessageUtil() {
        new Thread(this).start();
    }
    @Autowired
    public void setTemplate(SimpMessagingTemplate t) {
        WebSocketMessageUtil.messageingTemplate = t;
    }
    public static void addMessage(WebSocketMessage msg) {
        try {
            wsQueue.put(msg);
        } catch (InterruptedException e) {
            log.error("添加实时消息异常");
        }
    }
    public static void sendMessage(WebSocketMessage msg) {
        if (StringUtils.isEmpty(msg.getUserId())) {
            messageingTemplate.convertAndSend(msg.getDistination(), msg);
        } else {
            messageingTemplate.convertAndSendToUser(msg.getUserId(), msg.getDistination(), msg);
        }
    }
    @Override
    public void run() {
        log.info(">>>>>>>推送消息线程启动,正在监听消息。");
        while (true) {
            try {
                WebSocketMessage msg = wsQueue.take();
                if (msg != null) {
                    WebSocketMessageUtil.sendMessage(msg);
                }
            } catch (Exception ex) {
            }
        }
    }
}


5.使用注解配置定义的webSocket的配置类


package net.jileniao.ssm.websocket;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@EnableWebSocketMessageBroker
@Controller
public class WebSocketMessageAction extends AbstractWebSocketMessageBrokerConfigurer {
    /**
     * 将"/webSocket"路径注册为STOMP端点,这个路径与发送和接收消息的目的路径有所不同,
     * 这是一个端点,客户端在订阅或发布消息到目的地址前,要连接该端点,
     * 即用户发送请求url="/applicationName/webSocket"与STOMP server进行连接。之后再转发到订阅url;
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册websocket,客户端用ws://host:port/项目名/webSocket 访问
        registry.addEndpoint("/webSocket").setHandshakeHandler(new StompMessageHandshakeHandler())
                .addInterceptors(new WebSocketHandshakeInterceptor()).withSockJS();// 表示支持以SockJS方式连接服务器
    }
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 这句话表示在topic和user这两个域上服务端可以向客户端发消息
        registry.enableSimpleBroker("/topic", "/user");
        // 这句话表示客户端向服务器端发送时的主题上面需要加"/ws"作为前缀
        registry.setApplicationDestinationPrefixes("/ws");
        // 这句话表示服务端给客户端指定用户发送一对一的主题,前缀是"/user"
        registry.setUserDestinationPrefix("/user");
    }
}


6.其中StompMessageHandshakeHandler类代码如下


package net.jileniao.ssm.websocket;
import java.security.Principal;
import java.util.Map;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
/**
 * 获取客户端连接前对客户端的session、cookie等信息进行握手处理, <br />
 * 也就是可以在这里可以进行一些用户认证? 这是我个人的理解。这里没有做任何处理
 *
 */
public class StompMessageHandshakeHandler extends DefaultHandshakeHandler {
    @Override
    protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler,
            Map<String, Object> attributes) {
        return super.determineUser(request, wsHandler, attributes);
    }
}


7.其中WebSocketHandshakeInterceptor的程序代码如下


package net.jileniao.ssm.websocket;
import java.util.Map;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
/**
 * 握手前后的拦截,这里没有处理,默认
 *
 */
public class WebSocketHandshakeInterceptor extends HttpSessionHandshakeInterceptor {
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
            Exception ex) {
        super.afterHandshake(request, response, wsHandler, ex);
    }
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
            Map<String, Object> attributes) throws Exception {
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }
}


8.其中WebSocketHandshakeHandler的程序代码如下


package net.jileniao.ssm.websocket;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
/**
 * WebSocket 握手信息
 * 
 * @ClassName: WebSocketHandshakeHandler.java
 * @Description: WebSocket 握手信息
 */
public class WebSocketHandshakeHandler extends TextWebSocketHandler {
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
    }
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        super.afterConnectionEstablished(session);
    }
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        super.handleMessage(session, message);
    }
}


9.剩下就是工具辅助类了,首先是WebSocketMessage,自己封装的消息体


package net.jileniao.ssm.websocket;
/**
 * 自定义封装包含发送信息的实体类
 *
 */
public class WebSocketMessage {
    /**
     * 发送广播消息,发送地址:/topic/* ,*为任意名字,如取名monitor,则客户端对应订阅地址为:/topic/monitor
     * 发送私人消息,发送地址:/*,*为任意名字,这里取名为message,客户端对应订阅地址:/user/{自定义客户端标识ID}/message
     */
    private String distination;
    private Object data;// 实际发送的数据对象
    private String userId;// 如果不为空,则表示发送给个人而不是广播
    public String getDistination() {
        return distination;
    }
    public void setDistination(String distination) {
        this.distination = distination;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
}


10. 测试发送消息

有了上面的配置资源及代码之后,就可以测试在服务器上给客户端发送消息,发送消息使用下面的代码


WebSocketMessage message = new WebSocketMessage();
message.setDistination("/topic/jileniao/testWebSocket/new");
message.setData("A message from the server....");
WebSocketMessageUtil.addMessage(message);


11.客户端要接收,下面是客户端的页面程序。


<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>WebSocket测试</title>
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
 </head>
 <body>
  <div>
   <div>
    <div>
     <button id="connect" onclick="connect();">Connect</button>
     <button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
    </div>
    <div id="conversationDiv">
     <label>What is your name?</label><input type="text" id="name" />
     <button id="sendName" onclick="sendName();">Send all</button>
     <p id="response"></p>
    </div>
   </div>
  </div>
  <script src="https://cdn.bootcss.com/sockjs-client/1.1.4/sockjs.min.js"></script>
  <script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.min.js"></script>
  <script type="text/javascript">
   var stompClient = null;
   function setConnected(connected) {
    document.getElementById('connect').disabled = connected;
    document.getElementById('disconnect').disabled = !connected;
    document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
    document.getElementById('response').innerHTML = '';
   }
   function connect() {
    // 地址:工程名/webSocket
    // 连接服务端的端点,连接以后才可以订阅广播消息和个人消息 
    var socket = new SockJS('/ssm/webSocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function(frame) {
     console.log('Connected: ' + frame);
     // 订阅广播消息
     stompClient.subscribe('/topic/jileniao/testWebSocket/new', function(playload) {
      console.log(JSON.stringify(playload));
     });
     // 订阅个人信息
     stompClient.subscribe('/user/1/testUser', function(playload) {
      console.log(JSON.stringify(playload));
     });
    });
   }
   function disconnect() {
    if(stompClient != null) {
     stompClient.disconnect();
     console.log("Disconnected");
    }
   }
   //发送到服务的消息
   function sendName() {
    var name = document.getElementById('name').value;
    stompClient.send("../ws/webSocket/testWithServer", {
     'name': 'Jileniao',
     'syn': 'Zhang'
    }, JSON.stringify({
     'message': name
    }));
   }
  </script>
 </body>
</html>




OK。再次感谢csdn原博主。






分享本文至:

WRITTEN BY

avatar
本文标签:ssmwebsocketstomp
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

1 条评论

  1. avatar 头条新闻

    文章不错非常喜欢

欢迎留言




用户登录

sitemap