繁体   English   中英

未捕获的InvalidStateError:无法在“ WebSocket”上执行“发送”:

[英]Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket':

我正在尝试运行以下代码:

function smConnect() {
  ws = new WebSocket('ws://127.0.0.1:1805/');
  delete ws.URL;

  ws.onopen = function(response) {};
  ws.onmessage = function(response) {};
  ws.onclose = function(response) {};
  ws.onerror = function(error) {};
}

smConnect();
ws.send('message', 'hi');

但是它返回了这个错误:

未捕获的InvalidStateError:无法在“ WebSocket”上执行“发送”:仍处于CONNECTING状态。

可能是什么问题呢?

您可以这样操作,添加一些日志记录,发送可以在构造函数之外处理的信息,并且您还将SocketWrapper提取到其自己的命名空间中(好的,是的,现在在窗口中:))

您可以检查开发工具(在大多数浏览器中为F12),以查看日志/错误中发生的情况,例如:由于没有可用的套接字,此处将引发错误:)

而且您不需要为所有事件都提供值,只需为您需要的值(您的案例onopen +可能是onmessage?)

(在已定义的回调中,这将指向套接字,而不是SocketWrapper,SocketWrapper也未提供ws变量,它是私有的,但我应该这样做)

SocketWrapper上有一个send方法,当您发送到关闭的流时会引发错误,但是如果尚未打开,它将对消息进行排队直到打开,然后将队列清空到websocket(因此从某种意义上讲,您不需要设置onopen回调,只需使用send方法添加它就可以了;)

 (function(nameSpace) { function createMethod(method, options, stateCallback) { var that = this; this[method] = function() { if (stateCallback && stateCallback.apply) { stateCallback(method); } console.info(method); if (options[method] && options[method].apply) { options[method].apply(that, arguments); } }; } function SocketWrapper(options) { var ws, events = ['onopen', 'onmessage', 'onclose', 'onerror'], i, len, prop = { opened: false, closed: false, error: false }, method; if (typeof options === 'undefined' || !options) { throw 'ArgumentException: please add default constructor options'; } this.queue = []; this.onEventTrigger = function(eventName) { var i, len; if (eventName === 'onopen') { prop.opened = true; prop.closed = false; // openend send queue if (this.queue.length > 0) { for (i = this.queue.length; --i >= 0;) { this.send.apply(this, this.queue[0]); this.queue.splice(0, 1); } } } if (eventName === 'onerror') { prop.error = true; } if (eventName === 'onclosed') { prop.opened = false; prop.closed = true; } }; this.init = function() { var cb = this.onEventTrigger.bind(this); ws = new WebSocket(options.url); for (i = 0; i < events.length; i++) { method = events[i]; createMethod.apply(ws, [method, options, cb]); } }; this.send = function() { if (prop.closed) { throw 'InvalidOperation: Cannot send messages to a closed Websocket!'; } if (!prop.opened) { this.queue.push(arguments); } else { ws.send.apply(ws, arguments); } }; this.init(); return this; } window.SocketWrapper = SocketWrapper; }(window)); var socket = new window.SocketWrapper({ url: 'ws://127.0.0.1:1805', onopen: function() { this.send('message', 'hi'); }, onmessage: function() { console.log(arguments); }, onclose: function() { socket = null; }, onerror: function() { console.log('error occured, oh no!'); console.error(arguments); } }); socket.send('i am message send to soon, but since i check the state of the ws object, i will be queued and send when appropriate'); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM