Javascript websockets在打开后立即关闭 [英] Javascript websockets closing immediately after opening

查看:65
本文介绍了Javascript websockets在打开后立即关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

connection = new WebSocket("ws://localhost:1050/join?username=test")

connection.onopen = function(){
   alert('Connection open!');
}


connection.onmessage = function(e){
   var server_message = e.data;
   alert(server_message);
}

connection.onclose = function() {
    alert("websocket closing")
}

已建立与服务器的连接,并显示连接打开的警报!但是,此后立即关闭连接.服务器没有调用关闭,并且控制台中似乎没有其他错误.chrome和firefox中都在发生这种情况.

The connection to the server is established and an alert is displayed for Connection open! However immediately afterwards the connection closes. The server does not call close and there seem to be no other errors in the console. This is happening in both chrome and firefox.

我在网络上看了一堆不同的类似示例,但无济于事.

I looked at a bunch of different similar examples on the web but to no avail.

推荐答案

已修复!

我要做的就是在websocket连接关闭之前阻止处理程序返回

All I had to do was block the handler from returning before the websocket connection closes

这篇关于Javascript websockets在打开后立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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