yii2 websocket在打开后立即关闭

yii2 websocket在打开后立即关闭,websocket,yii2,Websocket,Yii2,我有一个websocket应用程序正在处理yii2。这是我的服务器端(使用): 这是我的客户端: $(function() { var chat = new WebSocket('ws://localhost:8079/'); chat.onopen = function(e) { console.log(e); console.log('connected!'); }; chat.

我有一个websocket应用程序正在处理yii2。这是我的服务器端(使用):

这是我的客户端:

$(function() {
        var chat = new WebSocket('ws://localhost:8079/');

        chat.onopen = function(e) {
            console.log(e);
            console.log('connected!');
        };
        chat.onclose = function(event) {
          console.log('closed');
        };

        $('#btnSend').click(function() {
            if ($('#message').val()) {
                chat.send( JSON.stringify({'action' : 'chat', 'message' : $('#message').val()}) );
            } else {
                alert('Enter the message')
            }
        })
    });
但当我打开此页面时,我在控制台中看到:

有联系的!
已关闭
当我试图通过
chat.send
发送消息时,我发现
WebSocket已经处于关闭或关闭状态。
。 我在这里该怎么办?为什么我的websocket关闭得这么快

对于yii2上的WebSocket,最好的决定是什么

另外,编辑我的js:

var chat = new WebSocket('ws://localhost:8080/');    
    chat._original_send_func = chat.send;
    chat.send = function(data) {
       if(this.readyState == 1) {
           this._original_send_func(data);
       }         
       else {
           console.log('No')
       }

       }.bind(chat);

当我试图发送消息时,
readyState
不等于1。我不知道为什么。

根据我的经验,SocketServer在连接客户端之后,在函数
init()
中崩溃。其中一个原因是,如果您使用
php cli
启动服务器,则崩溃的原因将是行
$user=user::findById(\Yii::$app->user->identity->id)由于Console应用程序没有用户标识组件,因此会引发异常,关闭服务器

var chat = new WebSocket('ws://localhost:8080/');    
    chat._original_send_func = chat.send;
    chat.send = function(data) {
       if(this.readyState == 1) {
           this._original_send_func(data);
       }         
       else {
           console.log('No')
       }

       }.bind(chat);