Closing Javascript Websites Right 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")
}

A connection has been established to the server, and a warning message is displayed to open the connection. However, immediately after this, the connection closes. The server is not ringing close, and there seem to be no other errors in the console. This happens in both chrome and firefox.

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

+4
source share
5 answers

Fixed!

All I had to do was block the handler from returning to closing the web connection

0
source

so that the Keep Websocket Opend does not allow the handler to return return false;to connection.onmessage for example:

connection.onmessage = function(e){
   var server_message = e.data;
   alert(server_message);
   return false;
}
+2

, , OP, , . , , , OP.

, , OP , : " , websocket ".

, webSocket , , webSocket. , Python script asyncio/websockets :

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)

, websockets.serve handleClient, , receiveCommandsLoop, .

: websockets.serve, Python , ( handleClient) , , .

, , receiveCommandsLoop, handleClient , webSocket .

, handleClient, , receiveCommandsLoop. , - .

+1

It can also happen when you try to send binary data via a connection to a website, but some side (client or server) tries to interpret it as text - many libraries and frameworks do this if you do not explicitly specify you need binary data .

0
source

This may also be a registration issue. Websocket will automatically close the required website authentication, but no authentication information has been provided.

0
source

Source: https://habr.com/ru/post/1538526/


All Articles