Websocket-InvalidStateError:尚未建立连接 [英] Websocket - InvalidStateError: The connection has not been established yet

查看:332
本文介绍了Websocket-InvalidStateError:尚未建立连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将Angular2前端与Spring的websocket后端连接.

I'm not able to connect Angular2 front-end with Spring's websocket back-end.

Spring配置xml文件:

Spring configuration xml file:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:websocket="http://www.springframework.org/schema/websocket"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">

    <!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
    <context:component-scan base-package="com.hestalis"/>

    <!-- Enable @Controller annotation support -->
    <mvc:annotation-driven/>

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

</beans>

Angular2组件:

Angular2 component:

import * as SockJS from 'sockjs-client';
import {Stomp} from 'stompjs';
import {Component} from '@angular/core';
import {Observable} from 'rxjs/Rx';

@Component({
    selector: 'hs-game',
    styleUrls: ['resources/css/game.css'],
    templateUrl: 'app/partials/game.html',
    //providers: [ GameService ]
})
export class GameComponent {
    stompClient: any;
    activityId: any;
    text: any;
    messages = [];
    messageIds = [];

    seconds = 0;
    minutes = 0;
    hours = 0;

    constructor() {
    }

    ngOnInit() {
        this.connect();
        this.sendMessage("Player is connected");

        let timer = Observable.timer(0, 1000);
        timer.subscribe(t=>this.setTimer());
    }

    setTimer() {
        this.seconds++;
        if (this.seconds >= 60) {
            this.seconds = 0;
            this.minutes++;
            if (this.minutes >= 60) {
                this.minutes = 0;
                this.hours++;
            }
        } 
    }

    sendMessage(message) {
        var id = Math.floor(Math.random() * 1000000);
        this.stompClient.send('/app/chat', {}, JSON.stringify({ message: message, id: id }));
        this.messageIds.push(id);
    }

    connect() {
        var that = this;
        var socket = new SockJS('/chat');
        this.stompClient = Stomp.over(socket);
        this.stompClient.connect({}, function (frame) {
            console.log('Connected: ' + frame);
            that.stompClient.subscribe('/topic/message/', function (greeting) {
                that.messages.push(JSON.parse(greeting.body).content);
            });
        }, function (err) {
            console.log('err', err);
        });
    }

    startListener() {

    }
}

我有InvalidStateError:尚未建立连接,但浏览器中出现异常:

And I have InvalidStateError: The connection has not been established yet exception in the browser:

如何正确配置Stomp和SockJS端点?我的应用程序部署在"/"下.

How to properly configure my Stomp and SockJS endpoint? My application is deployed under: '/'.

推荐答案

前段时间我找到了解决此问题的方法,我也将其发布在这里.

I found a solution to this problem some time ago, I'll post it here also.

问题在于:npm install sockjs-client下载sockjs-node而不是客户端.我希望他们会尽快修复它.

The issue is that: npm install sockjs-client downloads sockjs-node instead of client. I hope they will fix it soon.

如果您遇到相同的问题,则应下载此文件http://cdn.jsdelivr.net/sockjs/1/sockjs.min.js并将其放在项目中,并在system-config.ts中指向它.

If you're facing the same issue you should download this file http://cdn.jsdelivr.net/sockjs/1/sockjs.min.js put it in your project and point to it in system-config.ts.

这篇关于Websocket-InvalidStateError:尚未建立连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆