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

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

问题描述

两个相关的问题可能更根源于我缺乏浏览器如何/是否预解析 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() { ... };

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

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?

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

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?

我意识到目前这只是一个草案,可能取决于实现,我可能错过了一些非常明显的内容,但是我在互联网搜索/浏览 w3c 规范草案时看不到任何特别相关的内容,因此任何帮助非常感谢我对 websockets/javascript 内部工作原理的理解!

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 是单线程的,这意味着在当前执行范围完成并且网络执行有机会运行之前无法建立网络连接.执行范围可以是当前函数(下例中的 connect 函数).所以,如果你很晚才使用 setTimeout 绑定到 onopen 事件,你可能会错过它.在此示例中,您可能会错过该事件:

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:

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

代码:

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();

如果您运行该示例,您很可能会看到永远不会输出onopen called"日志行.这是因为我们错过了活动.

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.

但是,如果您将 new WebSocket(...) 和对 onopen 事件的绑定保持在同一执行范围内,那么您就不可能错过活动.

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.

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

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天全站免登陆