javascript websockets - 控制初始连接/何时onOpen被绑定 [英] javascript websockets - control initial connection/when does onOpen get bound

查看:179
本文介绍了javascript websockets - 控制初始连接/何时onOpen被绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个相关的问题可能更加根植于我对如何/如果浏览器预解析javascript的知识缺乏了解:

  var ws = new WebSocket(ws://ws.my.url.com); 
ws.onOpen = function(){...};

似乎没有办法直接控制的初始化WebSocket ,除了将它包装在回调中之外,所以我假设加载javascript代码后立即创建连接并进入构造函数?



何时将 onOpen 属性附加到 ws ?是否有任何竞争条件的可能性(如果由于某种原因,在套接字的定义和 onOpen ?的定义之间有一些代码),以便 onOpen 在连接建立之前/之后是不可约束的(我知道你可以选择检查 ws.readyState )。补充一点,是WebSocket握手阻塞?



我意识到这一切都是草稿,可能依赖于执行,我可能错过了一些非常明显的事情,但我不能在我的互联网搜索/浏览w3c规范草案中看不到任何特别相关的内容,所以任何帮助我理解websockets / javascript的内部工作方式都非常感谢!

解决方案

JavaScript是单线程的,这意味着在当前的执行范围完成并且网络执行有机会运行之前,无法建立网络连接。执行范围可以是当前函数(下例中的 connect 函数)。所以,如果你在使用setTimeout时很晚就绑定它,你可能会错过 onopen 事件。在这个例子中,您可能会错过活动:

查看: http://jsbin.com/ulihup/edit#javascript,html,live



代码:

  var ws = null; 

function connect(){
ws = new WebSocket('ws://ws.pusherapp.com:80 / app / a42751cdeb5eb77a6889?client = js& version = 1.10');
setTimeout(bindEvents,1000);
setReadyState();


函数bindEvents(){
ws.onopen = function(){
log('onopen called');
setReadyState();
};


function setReadyState(){
log('ws.readyState:'+ ws.readyState);


函数日志(msg){
if(document.body){
var text = document.createTextNode(msg);
document.body.appendChild(text);
}
}

connect();

如果你运行这个例子,你可能会发现永远不会输出'onopen called'日志行。这是因为我们错过了这个活动。



但是,如果你保留新的WebSocket(...)并且在相同的执行范围内绑定到 onopen 事件,那么你就不会错过这个事件。



有关执行范围的更多信息以及这些如何排队,安排和处理,请查看John Resig关于 JavaScript中的计时器


Two related questions that may be more rooted in my lack of knowledge of how/if browsers pre-parse javascript:

var ws = new WebSocket("ws://ws.my.url.com");
ws.onOpen = function() { ... };

There appears to be no way to directly control the initialisation of a WebSocket, beyond wrapping it in a callback, so I assume the connection is created as soon as the javascript code is loaded and get to the constructor?

When does the onOpen property get attached to ws? Is there any possibility of a race condition (if for some reason you had some code in between the definition of the socket and the definition of onOpen?) so that onOpen is undecidably bound before/after the connection is established (I know you could optionally check ws.readyState). Supplementary to this, is the WebSocket handshake blocking?

I realise it's all a draft at the moment, possibly implementation dependent and I may have missed something blindingly obvious, but I couldn't see anything particular pertinent on my internet searches/skim through the draft w3c spec, so any help in my understanding of websockets/javascript's inner workings is very much appreciated!

解决方案

JavaScript is single threaded which means the network connection can't be established until the current scope of execution completes and the network execution gets a chance to run. The scope of execution could be the current function (the connect function in the example below). So, you could miss the onopen event if you bind to it very late on using a setTimeout e.g. in this example you can miss the event:

View: http://jsbin.com/ulihup/edit#javascript,html,live

Code:

var ws = null;

function connect() {
  ws = new WebSocket('ws://ws.pusherapp.com:80/app/a42751cdeb5eb77a6889?client=js&version=1.10');
  setTimeout(bindEvents, 1000);
  setReadyState();
}

function bindEvents() {
  ws.onopen = function() {
    log('onopen called');
    setReadyState();
  };
}

function setReadyState() {
  log('ws.readyState: ' + ws.readyState);
}

function log(msg) {
  if(document.body) {
    var text = document.createTextNode(msg);
    document.body.appendChild(text);
  }
}

connect();

If you run the example you may well see that the 'onopen called' log line is never output. This is because we missed the event.

However, if you keep the new WebSocket(...) and the binding to the onopen event in the same scope of execution then there's no chance you'll miss the event.

For more information on scope of execution and how these are queued, scheduled and processed take a look at John Resig's post on Timers in JavaScript.

这篇关于javascript websockets - 控制初始连接/何时onOpen被绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆