websocket failed:WebSocket握手期间出错:意外的响应代码:400

时间:2016-02-14 17:32:34

标签: spring amazon-web-services websocket spring-websocket sockjs

我正在使用ec2,使用socksJS的spring websocket在本地工作正常。 我已经尝试了下面的事情。 1.我没有任何阻止TCP的负载均衡器。请求直接转到ec2服务器 2.使用true IP代替ec2服务器名称。

虽然服务器在Chrome控制台中给出了以下问题,但应用程序服务器日志中没有错误

WebSocket connection to 'ws://ec2-XX-X-XXX-XXX.compute-1.amazonaws.com/ws/963/kaidmvd9/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

的Javascript

app.service('SocketService', function(httpService,$rootScope,SoundService) {
this.registerMe = function(callback){
    $rootScope.socket = new SockJS("/ws");
    $rootScope.stompClient = Stomp.over($rootScope.socket);
    $rootScope.stompClient.connect('guest', 'guest', function(frame1) {
        $rootScope.stompClient.subscribe('/user/'+$rootScope.loggedInUser.username+'/reply', function(frame2) {
            var msg = JSON.parse(frame2.body);
            console.log(msg);
        });
    }, 
    function(error) {
        console.log(error.headers.message);
    }
    );});

Spring xml

<websocket:message-broker application-destination-prefix="/app" >
    <websocket:stomp-endpoint path="/ws">
        <websocket:sockjs />
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic,/user" />
</websocket:message-broker>

1 个答案:

答案 0 :(得分:0)

通过你的T-Hub帖子来到这里,没有足够的声誉来添加评论,否则就会这样做。这是你可以尝试的;使用您的EC2的私有IP(使用ifconfig查找)来托管WebSocket端点。

<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/ws">
      <websocket:sockjs/>
    </websocket:stomp-endpoint>
     <websocket:stomp-broker-relay prefix="/topic,/user"
           relay-host="your-ec2-private-ip-addr" relay-port="80" heartbeat-send-interval="20000" heartbeat-receive-interval="20000"/>
</websocket:message-broker>

得到以下帮助: Java Spring STOMP: Set broker IP