在WebSocket开放活动中警报
我有这样的代码,警报(“打开”)显示正常。
var webSocket = new WebSocket("ws://localhost:8080/WebSocketServerExample/websocketendpoint");
webSocket.onopen = function(message){ alert("open");};
But when I add one more alert after webSocket creation then the same alert("open") doesn't appearvar webSocket = new WebSocket("ws://localhost:8080/WebSocketServerExample/websocketendpoint");
alert("after websoscket creation");
webSocket.onopen = function(message){ alert("open");};
和线程有关系吗?
更新: 如果我们用 console.log 替换 onopen 事件中的警报,那么控制台中将再次出现任何内容。这意味着 onopen 中的所有代码都不会运行,因此我们假设该事件没有被触发。为什么?
I have this code that alert("open") is displayed ok.
var webSocket = new WebSocket("ws://localhost:8080/WebSocketServerExample/websocketendpoint");
webSocket.onopen = function(message){ alert("open");};
But when I add one more alert after webSocket creation then the same alert("open") doesn't appear
var webSocket = new WebSocket("ws://localhost:8080/WebSocketServerExample/websocketendpoint");
alert("after websoscket creation");
webSocket.onopen = function(message){ alert("open");};
Has it to do with threads?
UPDATE:
If we replace the alert inside onopen event with console.log then again nothing appears in console. This means that all the code inside onopen doesn't run so this leads us to assume that the event is not triggered. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行顺序为:
var websocket = new WebSocket(...);
alert("websoscket 创建之后");
webSocket.onopen();< /code>
alert("open");
即,
onopen
回调在您的第一个alert
之后执行。这意味着,第一个警报打开,而第二个警报启动,并且在浏览器中,当已经有一个警报打开时,第二个警报将被阻止。
The sequence of execution is:
var websocket = new WebSocket(...);
alert("after websoscket creation");
webSocket.onopen();
alert("open");
i.e., the
onopen
callback is executed after your firstalert
.That means, that the first alert is open while the second is launched, and in the browser, a second alert is blocked when there is already one open.
我认为我们错过了Onopen活动。
有两个链接与此有关。
javascript websockets-javascript websockets-控制初始连接
对执行代码执行的很好解释: https://bytefish.medium.com/the-execution-order-order-of-as-sasynchronous-functions-in-the-event-loop-ff641dae4f09
I think that we missed out the onopen event.
There is 2 links that has to do with this.
javascript websockets - control initial connection/when does onOpen get bound
Also a very good explanation of code execution:https://bytefish.medium.com/the-execution-order-of-asynchronous-functions-in-the-event-loop-ff641dae4f09