Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript websockets closing immediately after opening

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.

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

like image 444
Aniruddh Chaturvedi Avatar asked Apr 29 '14 06:04

Aniruddh Chaturvedi


2 Answers

to Keep Websocket Opened prevent handler from returning by return false; in connection.onmessage like this :

connection.onmessage = function(e){
   var server_message = e.data;
   alert(server_message);
   return false;
}
like image 122
Hisham Karam Avatar answered Nov 15 '22 14:11

Hisham Karam


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.

The short version is that I think OP was referring to his server-side connection handler when he said "All I had to do was block the handler from returning before the websocket connection closes".

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:

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.

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.

Thus, by the time receiveCommandsLoop was run, handleClient had returned, and the webSocket had been automatically closed.

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.

like image 28
Syriven Avatar answered Nov 15 '22 14:11

Syriven