wildfly 8.2.1 + spring mvc + spring-websocket(简单代理)+ sockjs + stomp
wildfly 8.2.1 + spring mvc + spring-websocket (simple-broker) + sockjs + stomp
我有一个 websocket 的问题,特别是 SimpMessagingTemplate 和提供 spring(版本 4.3.4.RELEASE)的简单代理。
我在 class:
中发送给用户消息
@Autowired
SimpMessagingTemplate simpMessagingTemplate;
public void onMessage(final TextMessage message, Session session) throws JMSException {
....
this.simpMessagingTemplate.convertAndSendToUser(
userInfo.getIp(), "/queue/resp", message.getText());
}
这是我的 websocket 配置:
<websocket:message-broker application-destination-prefix="/app" user-destination-prefix="/user">
<websocket:stomp-endpoint path="/workplace">
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
</websocket:handshake-interceptors>
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic, /queue, /user"/>
</websocket:message-broker>
这是我的web.xml内容:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
我的控制器:
@控制器
public class UserController 扩展了 TextWebSocketHandler {
@MessageMapping("/req")
@SendToUser(destinations = "/queue/resp")
public String greeting(String greeting)
throws Exception {
System.out.println("greeeeeeeeeeeeeeeeeeeeeeeeeeeeeting: " + greeting);
return "{ \"date\": "+ new Date().getTime() + ", \"message\": " + greeting + "}";
}
@MessageExceptionHandler
@SendToUser("/queue/errors")
public String handleException(Exception ex) {
ex.printStackTrace();
System.out.println(ex);
return ex.getMessage();
}
}
还有我在客户端的js代码:
<script>
var service = {}, socket = {
client: null,
stomp: null
};
service.userIpAddress = 'someIpAddress';
service.RECONNECT_TIMEOUT = 25000;
service.SOCKET_URL = 'http://localhost:8080/examinee_workplace/rest/workplace/';
service.send = function(message) {
console.info('try send message = ', message);
var id = Math.floor(Math.random() * 1000000);
socket.stomp.send("/app/req", {
priority: 9
}, JSON.stringify({
message: message,
id: id
}));
};
var startListener = function() {
console.info('subscribe');
socket.stomp.subscribe('/user/' + service.userIpAddress + '/queue/resp', function(resp) {
console.warn('resp = ', resp);
});
socket.stomp.subscribe('/user/' + service.userIpAddress + '/queue/errors', function(resp) {
var body = JSON.parse(resp.body);
console.warn('error body = ', body);
});
};
var reconnect = function() {
console.info('reconnect. this.RECONNECT_TIMEOUT = ', this.RECONNECT_TIMEOUT);
setTimeout(service.initialize,1000);
};
service.initialize = function() {
socket.client = new SockJS(service.SOCKET_URL);
socket.stomp = Stomp.over(socket.client);
socket.stomp.connect({}, startListener);
socket.stomp.onclose = reconnect;
};
service.initialize();
最后是我的问题 - 我在客户端上收到消息,但不是全部。某些消息未传送到客户端(卡巴斯基防病毒软件已关闭)。
你知道为什么吗?
我只是用 JavaConfig 替换了 Spring XML 配置。
我在这里找到了答案:Stomp over socket using sockjs can't connect with Spring 4 WebSocket
我有一个 websocket 的问题,特别是 SimpMessagingTemplate 和提供 spring(版本 4.3.4.RELEASE)的简单代理。 我在 class:
中发送给用户消息@Autowired
SimpMessagingTemplate simpMessagingTemplate;
public void onMessage(final TextMessage message, Session session) throws JMSException {
....
this.simpMessagingTemplate.convertAndSendToUser(
userInfo.getIp(), "/queue/resp", message.getText());
}
这是我的 websocket 配置:
<websocket:message-broker application-destination-prefix="/app" user-destination-prefix="/user">
<websocket:stomp-endpoint path="/workplace">
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
</websocket:handshake-interceptors>
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic, /queue, /user"/>
</websocket:message-broker>
这是我的web.xml内容:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
我的控制器:
@控制器 public class UserController 扩展了 TextWebSocketHandler {
@MessageMapping("/req")
@SendToUser(destinations = "/queue/resp")
public String greeting(String greeting)
throws Exception {
System.out.println("greeeeeeeeeeeeeeeeeeeeeeeeeeeeeting: " + greeting);
return "{ \"date\": "+ new Date().getTime() + ", \"message\": " + greeting + "}";
}
@MessageExceptionHandler
@SendToUser("/queue/errors")
public String handleException(Exception ex) {
ex.printStackTrace();
System.out.println(ex);
return ex.getMessage();
}
}
还有我在客户端的js代码:
<script>
var service = {}, socket = {
client: null,
stomp: null
};
service.userIpAddress = 'someIpAddress';
service.RECONNECT_TIMEOUT = 25000;
service.SOCKET_URL = 'http://localhost:8080/examinee_workplace/rest/workplace/';
service.send = function(message) {
console.info('try send message = ', message);
var id = Math.floor(Math.random() * 1000000);
socket.stomp.send("/app/req", {
priority: 9
}, JSON.stringify({
message: message,
id: id
}));
};
var startListener = function() {
console.info('subscribe');
socket.stomp.subscribe('/user/' + service.userIpAddress + '/queue/resp', function(resp) {
console.warn('resp = ', resp);
});
socket.stomp.subscribe('/user/' + service.userIpAddress + '/queue/errors', function(resp) {
var body = JSON.parse(resp.body);
console.warn('error body = ', body);
});
};
var reconnect = function() {
console.info('reconnect. this.RECONNECT_TIMEOUT = ', this.RECONNECT_TIMEOUT);
setTimeout(service.initialize,1000);
};
service.initialize = function() {
socket.client = new SockJS(service.SOCKET_URL);
socket.stomp = Stomp.over(socket.client);
socket.stomp.connect({}, startListener);
socket.stomp.onclose = reconnect;
};
service.initialize();
最后是我的问题 - 我在客户端上收到消息,但不是全部。某些消息未传送到客户端(卡巴斯基防病毒软件已关闭)。 你知道为什么吗?
我只是用 JavaConfig 替换了 Spring XML 配置。 我在这里找到了答案:Stomp over socket using sockjs can't connect with Spring 4 WebSocket