Vis*_*ale 2 java spring spring-mvc websocket spring-websocket
我正在尝试在我现有的Spring MVC应用程序中实现聊天.我指的是来自github的这两个链接 -
但是当我尝试通过websocket连接时,看起来初始的HttpHandhsake失败了.我已经在这里上传了我的源代码 - 服务器代码.这是关于源代码的简要介绍.配置类是 -
只有ChatController处理WebSocket和webapp/WEB-INF/pages/home.jsp包含客户端js.I我在Sock JS上使用STOMP.
因此,请查看代码并建议我进行聊天工作的更改.目前,当我在网络选项卡上看到它看起来像初始HttpHandshake失败,因为交换机协议请求的延迟非常高(它永远不会返回).
从网络和控制台面板编辑 ScreenShots -


你的链接代码相当大,所以很难找到所有事情发生的地方......但是当我得到使用Spring的WebSocket内容时,我必须处理一些事情:
(作为旁注,我最初遇到了类似于你的问题,初始握手会因各种错误信息而失败)
这些是我正在使用的特定spring依赖项.从你的pom看起来你好在这里,但我将它们包含在列表中以供参考(这些是Gradle依赖声明).
compile "org.springframework:spring-websocket:4.0.1.RELEASE"
compile "org.springframework:spring-messaging:4.0.1.RELEASE"
Run Code Online (Sandbox Code Playgroud)
web.xml应该将servlet规范版本声明为3.0,而不是2.3.看起来你正在使用2.3.
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
Run Code Online (Sandbox Code Playgroud)
不确定这是否是一个严格的要求,但我不得不切换到我的工作.这是文档参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html.我还必须将"async-supported"元素添加到所有servlet定义和过滤器定义中:
<async-supported>true</async-supported>
Run Code Online (Sandbox Code Playgroud)
此外,您需要使用兼容的servlet容器.我不得不升级到Tomcat 7.0.50(我认为最低版本是7.0.47).Spring文档列出了其他兼容的容器.
这是我的Spring XML配置:
<websocket:message-broker application-destination-prefix="/rt">
<websocket:stomp-endpoint path="/endpoint" >
<websocket:sockjs />
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic" />
</websocket:message-broker>
Run Code Online (Sandbox Code Playgroud)
所以,我在端点"端点"上创建了一个主题."application-destination-prefix"实际上并没有被使用AFAICT,因为我只是从服务器推送到客户端.
然后,并不是说它与你的"无法连接"问题有关,而是我在这里向主题发送消息的方式:
SimpMessagingTemplate simpMessagingtemplate;
@Autowired
public void setSimpMessagingTemplate(SimpMessagingTemplate template){
simpMessagingtemplate = template;
}
@Override
public void sendMessage(UserDetails userDetails, String message, DateTime time) {
// Send notifications based on subscriptions
for(String destUser : ...list of users...){
this.simpMessagingtemplate.convertAndSendToUser(
destUser,
"/topic/notifications",
new MessagePayload(... Your stuff here...));
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用AngularJS,所以这些东西都在消息服务中.
var socket = new SockJS('https://localhost:8443/WildFire/endpoint');
var stompClient = Stomp.over(socket);
stompClient.connect('empty', 'empty', function(frame){
$log.info("Connected");
stompClient.subscribe('/user/topic/notifications', function(message){
addMessage(JSON.parse(message.body));
});
});
Run Code Online (Sandbox Code Playgroud)
我需要引用websocket的完整URL,因为SockJS没有使用到端点的相对路径.在我做出改变之前,它造成了奇怪的错误.我正在进行用户特定的主题订阅,因此订阅声明中的"/ user/...".
此外,我必须为初始stompClient连接提供虚拟凭证(例如'empty/empty'),否则我会收到关于缺少凭据的错误.我认为使用空字符串('','')也可以.
在您的安全设置中,您的"/ chat"网址属于一揽子'permitAll'条件.这不应该导致问题,但是您可以保护websocket端点与任何其他URL相同,因为它始于作为Spring处理的基本servlet请求.
我的安全配置处理websocket端点需要经过身份验证的会话和HTTPS,它可以与websockets和消息传递系统一起使用.因为websocket连接作为servlet请求启动,所以使用在初始登录期间建立的JSESSIONID对其进行身份验证.
无论如何,我不确定究竟是什么导致了您的具体问题,但这是我为了让我的应用程序正常运行所经历的过程.看起来你正在做一些与我正在做的事情非常相似的事情,所以希望这会帮助你弄清楚出了什么问题.
| 归档时间: |
|
| 查看次数: |
5352 次 |
| 最近记录: |