Uncaught 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');
但它返回给我这个错误:
Uncaught InvalidStateError:未能在"WebSocket"上执行"send":仍处于CONNECTING状态。
可能是什么问题?
你可以这样做,它添加了一些日志记录,发送你可以在构造函数之外处理的信息,你也可以在它自己的命名空间中抽象掉SocketWrapper(好吧,是的,它现在在窗口中:)
你可以检查开发工具(大多数浏览器中的F12),看看日志/错误中发生了什么,例如:这里它抛出了一个错误,因为没有可用的套接字:)
您不需要为所有事件提供值,只需为您需要的事件提供值(您的情况是onpen+可能是onmessage?)
(在定义的回调中,这将指向套接字,而不是SocketWrapper,SocketWraper也不提供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');
相关文章:
- 为什么我的WebSocket客户端总是在发送消息后重新加载
- websocket客户端在发送大数据时断开连接
- 为什么我的服务器从不接收WebSocket发送
- Uncaught InvalidStateError:无法执行'发送'在'WebSocket
- 通过 Websocket 发送的 JSON 对象的大小
- 如何访问使用websocket发送的烧瓶中的formData?Flask SocketIO
- Nodejs-websocket:如何在没有服务器响应的情况下连续发送客户端消息
- 如何使用websocket将多个数组发送到客户端
- 使用牛仔和MessagePack通过websocket发送二进制数据
- 通过 WebSocket 将视频文件发送到浏览器
- 将垃圾发送到服务器的 Websocket
- Websocket 客户端到服务器发送 Float32Array - 我做错了什么
- 通过MySQL数据库更改发送WebSocket更新更高效
- Websocket 不会向所有客户端发送数据
- WebSocket 发送有关连接的额外信息
- Websocket 无法发送大型数据
- 将数组从 websocket 发送到客户端
- 如何处理通过 JavaScript 通过 WebSocket 发送回 Python 服务器的 blob
- WebSocket JavaScript:发送复杂对象
- 每1秒发送WebSocket消息