【问题标题】:How to solve "failed: Error during WebSocket handshake: Unexpected response code: 200" in spring websockets?如何解决spring websockets中的“失败:WebSocket握手期间出错:意外响应代码:200”?
【发布时间】:2017-08-21 03:35:39
【问题描述】:

我正在尝试订阅 Spring Web 套接字中的特定频道,但是在使用 SockJs 指向 URL 时,我收到以下错误 WebSocket connection to 'ws://localhost:8080/Spring/rest/user/chat/045/jmfz3b3j/websocket' failed: Error during WebSocket handshake: Unexpected response code: 200 请帮助我避免这种情况。 这是我的订阅客户端代码。

索引.jsp

var stompClient =null;
      function subscribe(){ 
var socket = new SockJS('/Spring/rest/user/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
     console.log('Connected: ' + frame);
     stompClient.subscribe('/topic/messages', function(test) {
     	 alert("in call back function");});
 });
 }

【问题讨论】:

  • 看起来服务器端点与客户端不匹配。贴出服务端代码sn-p供审核。
  • @Kane 请检查以下sn-p。

标签: spring-mvc stomp spring-websocket sockjs spring-messaging


【解决方案1】:

@Kane 这是我在spring-servlet.xml 中的websocket 配置

<websocket:message-broker application-destination-prefix="/app" >
    <websocket:stomp-endpoint path="/chat" allowed-origins="*">
        <websocket:sockjs />
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>

这是我的controller 代码

@Controller
@RequestMapping("user")
public class OptnCptController{
    @MessageMapping("topic/messages")
    public String getMsg(String s)
    {
        return s;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    相关资源
    最近更新 更多