Javascript websockets打开后立即关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23356983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript websockets closing immediately after opening
提问by Aniruddh Chaturvedi
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")
}
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.
与服务器的连接已建立,并显示连接打开的警报!但是,此后立即关闭连接。服务器没有调用 close 并且控制台中似乎没有其他错误。这发生在 chrome 和 firefox 中。
I looked at a bunch of different similar examples on the web but to no avail.
我在网上查看了一堆不同的类似示例,但无济于事。
采纳答案by Aniruddh Chaturvedi
Fixed it!
解决它!
All I had to do was block the handler from returning before the websocket connection closes
我所要做的就是在 websocket 连接关闭之前阻止处理程序返回
回答by Syriven
I believe I've stumbled across the solution that OP found but failed miserably to explain. I don't have enough reputation to comment, otherwise I'd be responding to all of the confused comments begging for clarification on OP's response.
我相信我偶然发现了 OP 找到但未能解释清楚的解决方案。我没有足够的声誉来发表评论,否则我将回应所有要求澄清 OP 回应的混乱评论。
The short version is that I think OP was referring to his server-side connection handlerwhen he said "All I had to do was block the handler from returning before the websocket connection closes".
简短的说法是,当他说“我所要做的就是阻止处理程序在 websocket 连接关闭之前返回”时,我认为 OP 指的是他的服务器端连接处理程序。
It turns out my server was closing the webSocket automatically because I didn't understand how a certain webSocket function worked. Specifically, I was using a Python server script with asyncio/websockets and the following code:
原来我的服务器正在自动关闭 webSocket,因为我不明白某个 webSocket 函数是如何工作的。具体来说,我使用的是带有 asyncio/websockets 的 Python 服务器脚本和以下代码:
async def receiveCommandsLoop(player):
while True:
msg = await player.websocket.recv()
print(command)
async def handleClient(websocket, path):
username = await websocket.recv()
player = players[username]
...
#Start task to listen for commands from player
asyncio.get_event_loop().create_task(receiveCommandsLoop(player))
start_server = websockets.serve(handleClient, '', 8765)
The idea was that websockets.serve would use handleClient to begin the connection and do some setup, then create a new task with receiveCommandsLoop that would take over the job of communication.
这个想法是 websockets.serve 将使用 handleClient 开始连接并进行一些设置,然后使用 receiveCommandsLoop 创建一个新任务,该任务将接管通信工作。
But it turns out: when you call websockets.serve, Python expects that when your handler (in this case, handleClient) returns, you must be done with the socket, and it closes it automatically.
但事实证明:当您调用 websockets.serve 时,Python 期望当您的处理程序(在本例中为 handleClient)返回时,您必须完成套接字并自动关闭它。
Thus, by the time receiveCommandsLoop was run, handleClient had returned, and the webSocket had been automatically closed.
因此,到运行 receiveCommandsLoop 时,handleClient 已经返回,并且 webSocket 已自动关闭。
I was able to fix this by simply modifying my handleClient function to directly run the loop originally contained in receiveCommandsLoop. Hope this helps someone out there.
我能够通过简单地修改我的 handleClient 函数来直接运行最初包含在 receiveCommandsLoop 中的循环来解决这个问题。希望这可以帮助那里的人。
回答by Hisham Karam
to Keep Websocket Opened prevent handler from returning by return false;
in connection.onmessage
like this :
保持 Websocket 打开防止处理程序返回return false;
,connection.onmessage
如下所示:
connection.onmessage = function(e){
var server_message = e.data;
alert(server_message);
return false;
}
回答by Edgar N
It could also be a login problem. The websocket will automatically close the website required authentication but no authentication information was provided.
也可能是登录问题。websocket 会自动关闭需要认证的网站,但没有提供认证信息。
回答by Andrew Kravchuk
This also could be the case when you're trying to send binary data over a websocket connection, but some side (client or server) is trying to interpret it as a text - many libraries and frameworks do it unless you explicitly specify you do want binary data.
当您尝试通过 websocket 连接发送二进制数据时,也可能是这种情况,但某些端(客户端或服务器)试图将其解释为文本 - 许多库和框架都会这样做,除非您明确指定您确实想要二进制数据。