//webSocket相关配置 //链接地址 public static String WEBSOCKETPATHPERFIX = "/ws-push"; public static String WEBSOCKETPATH = "/endpointWisely"; //消息代理路径 public static String WEBSOCKETBROADCASTPATH = "/topic"; //前端发送给服务端请求地址 public static final String FORETOSERVERPATH = "/welcome"; //服务端生产地址,客户端订阅此地址以接收服务端生产的消息 public static final String PRODUCERPATH = "/topic/getResponse"; //点对点消息推送地址前缀 public static final String P2PPUSHBASEPATH = "/user"; //点对点消息推送地址后缀,最后的地址为/user/用户识别码/msg public static final String P2PPUSHPATH = "/msg";
接收前端消息实体
public class WiselyMessage { private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
后台发送消息实体
private String responseMessage;
public WiselyResponse(String responseMessage){ this.responseMessage = responseMessage; }
public String getResponseMessage() { return responseMessage; }
public void setResponseMessage(String responseMessage) { this.responseMessage = responseMessage; }
配置websocket
@Configuration // @EnableWebSocketMessageBroker注解用于开启使用STOMP协议来传输基于代理(MessageBroker)的消息,这时候控制器(controller) // 开始支持@MessageMapping,就像是使用@requestMapping一样。 @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { //注册一个Stomp的节点(endpoint),并指定使用SockJS协议。 stompEndpointRegistry.addEndpoint(Constant.WEBSOCKETPATH).withSockJS(); }