ice*_*ter 35 javascript exception-handling websocket
当我的页面加载时,我尝试向send服务器发送一条消息以启动连接,但它无法正常工作.此脚本块位于我的文件顶部附近:
var connection = new WrapperWS();
connection.ident();
// var autoIdent = window.addEventListener('load', connection.ident(), false);
Run Code Online (Sandbox Code Playgroud)
大多数时候,我看到标题中的错误:
未捕获的InvalidStateError:无法在'WebSocket'上执行'send':仍处于CONNECTING状态
所以我尝试catch了异常,正如你在下面看到的那样,但现在它似乎InvalidStateError没有定义,并产生了一个ReferenceError.
这是我的websocket连接的包装器对象:
// Define WrapperWS
function WrapperWS() {
if ("WebSocket" in window) {
var ws = new WebSocket("ws://server:8000/");
var self = this;
ws.onopen = function () {
console.log("Opening a connection...");
window.identified = false;
};
ws.onclose = function (evt) {
console.log("I'm sorry. Bye!");
};
ws.onmessage = function (evt) {
// handle messages here
};
ws.onerror = function (evt) {
console.log("ERR: " + evt.data);
};
this.write = function () {
if (!window.identified) {
connection.ident();
console.debug("Wasn't identified earlier. It is now.");
}
ws.send(theText.value);
};
this.ident = function () {
var session = "Test";
try {
ws.send(session);
} catch (error) {
if (error instanceof InvalidStateError) {
// possibly still 'CONNECTING'
if (ws.readyState !== 1) {
var waitSend = setInterval(ws.send(session), 1000);
}
}
}
window.identified = true;
theText.value = "Hello!";
say.click();
theText.disabled = false;
};
};
}
Run Code Online (Sandbox Code Playgroud)
我在Ubuntu上使用Chromium进行测试.
小智 25
您可以通过等待readyState为1的代理函数发送消息.
this.send = function (message, callback) {
this.waitForConnection(function () {
ws.send(message);
if (typeof callback !== 'undefined') {
callback();
}
}, 1000);
};
this.waitForConnection = function (callback, interval) {
if (ws.readyState === 1) {
callback();
} else {
var that = this;
// optional: implement backoff for interval here
setTimeout(function () {
that.waitForConnection(callback, interval);
}, interval);
}
};
Run Code Online (Sandbox Code Playgroud)
然后使用this.send代替ws.send,并将之后应该运行的代码放在回调中:
this.ident = function () {
var session = "Test";
this.send(session, function () {
window.identified = true;
theText.value = "Hello!";
say.click();
theText.disabled = false;
});
};
Run Code Online (Sandbox Code Playgroud)
对于更简化的内容,您可以查看承诺.
引发此错误是因为您在建立WebSocket 连接之前发送消息。
您可以通过简单地执行以下操作来解决它:
conn.onopen = () => conn.send("Message");
Run Code Online (Sandbox Code Playgroud)
这个onopen函数在发送消息之前等待您建立 websocket 连接。
如果您使用一个 websocket 客户端对象并从随机应用程序位置进行连接,则该对象可以处于连接模式(并发访问)。
如果您只想通过一个 websoket 进行交换,则创建带有Promise 的类并将其保留在属性中
class Ws {
get newClientPromise() {
return new Promise((resolve, reject) => {
let wsClient = new WebSocket("ws://demos.kaazing.com/echo");
console.log(wsClient)
wsClient.onopen = () => {
console.log("connected");
resolve(wsClient);
};
wsClient.onerror = error => reject(error);
})
}
get clientPromise() {
if (!this.promise) {
this.promise = this.newClientPromise
}
return this.promise;
}
}
Run Code Online (Sandbox Code Playgroud)
创建单例
window.wsSingleton = new Ws()
Run Code Online (Sandbox Code Playgroud)
在应用程序的任何地方使用 clientPromise 属性
window.wsSingleton.clientPromise
.then( wsClient =>{wsClient.send('data'); console.log('sended')})
.catch( error => alert(error) )
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/adqu7q58/11/
| 归档时间: |
|
| 查看次数: |
42869 次 |
| 最近记录: |