5

我正在使用 WebSocket,并试图能够在整个应用程序中随时发送套接字数据。当我尝试从另一个函数中访问发送命令时,我收到:

Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.

这仅在我调用函数时发生,这就是我设置 websocket 的方式:

Main.socket = (function() {
var socket = new WebSocket("ws://server:port");

socket.onopen = function() {  
    console.log("Socket has been opened!");  
}

function send() {
    socket.send('test');
}

return {
    socket: socket,
    send: send
}
})();

我能够全局调用该函数,并且当我从函数中 console.log Main.socket 时,它也能够看到套接字。但是当我调用发送函数时,我得到了那个错误。

4

2 回答 2

4

这是等待网络套接字连接上线的替代解决方案,将您的调用替换为:

function send() {
    web_socket.send('test');
}

有了这个 :

function send(msg) {

    wait_for_socket_connection(socket, function() {
        socket.send(msg);
    });
};

function wait_for_socket_connection(socket, callback){

    setTimeout(
        function(){
            if (socket.readyState === 1) {
                if(callback !== undefined){
                    callback();
                }
                return;

            } else {

                console.log("... waiting for web socket connection to come online");

                wait_for_socket_connection(socket,callback);
            }
        }, 5);
};
于 2014-11-06T21:18:19.883 回答
2

问题是套接字还没有打开。在异步事件发生之前WebSocket.send 不能使用。 onopen

在使用setTimeout(足够长的持续时间)“应该工作”时,处理异步 JavaScript 编程的正确方法是将程序流视为一系列相关事件。

无论如何,这里是一个小例子,展示了如何使用jQuery Deferred Object(从 jQuery 1.8 开始,它没有被破坏并遵守Promises/A契约):

Main.socket = (function($) {
   var socket = new WebSocket("ws://server:port");
   // Promise will be called with one argument, the "send" function for this
   // socket.
   var readyPromise = $.Deferred();
   socket.onopen = function() {
     console.log("Socket has been opened!");
     readyPromise.resolve(socket.send)
  }
  return readyPromise;
})(jQuery);

然后,在使用这个小模块的代码中:

Main.socket.then(function (send) {
   // This will only be called after `Promise.resolve` is called in the module
   // which will be called in the `WebSocket.onopen` callback.
   send("Hello world!");
})
// This code may or may not execute before the `then` function above
// depending upon the state the Promise/Deferred Object.
// However, we can get consistent program flow by using `then`-chaining
// of promises.

当然,您不必使用 Promises - 回调可以正常工作,尽管我更喜欢 Promises 的统一合同/框架 - 您可以使用最合适的任何名称或结构。

另外,请注意,在整个页面生命周期中使用单个 WebSocket可能并不好,因为这将无法正确处理断开连接和恢复情况。

于 2013-10-25T19:21:43.617 回答