/*
* Name: WebSocketConfig.java
* Title:
* Description:
* @copyright 2004-2018 Sichuan Tianyi Network Service Co.,Ltd All Rights Reserved。
* @version v1.0.0 2016-4-25 14:19:09
*/
package com.tievd.cloud.vbp.message.config;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;
/**
*
* @author sitianqiang@tieserv.com
*/
@Configuration
@EnableWebSocketMessageBroker
@Order(Ordered.HIGHEST_PRECEDENCE + 99)
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
private Log LOG = LogFactory.getLog(WebSocketConfig.class);
/**
* 定义服务器端接改地址
*
* @param registry
*/
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
//服务端点接收客户端的连接
registry.addEndpoint("/notify").setAllowedOrigins("*").withSockJS().setClientLibraryUrl("../js/1.1.4/sockjs.min.js");
}
/**
* 定义服务器端发送地址
*
* @param config
*/
@Override
**public void configureMessageBroker(MessageBrokerRegistry config) {
//客户端给服务端发消息的地址的前缀
config.setApplicationDestinationPrefixes("/app");
//客户端接收服务端消息的地址的前缀信息
config.enableSimpleBroker("/topic", "/queue");
}**
}
在以上服务端配置的情况下,js也有相应的写法:
连接websocket:很明显对应者registerStompEndpoints方法里的配置notify
var socket = new SockJS("http://master:8989/msg/notify/?access_token=" + token);
订阅:对应于enableSimpleBroker方法
stompClient.subscribe('/user/queue/face_compare', function(greeting){
客户端向服务端发送消息:对应与setApplicationDestinationPrefixes方法
stompClient.send("/app/hello", {atytopic:"greetings"}, JSON.stringify({ 'name': name }));