Ws4py.websocket: Closing a connection with client latency

I created a secure WebSocket server using the ws4py.websocket package.

The javascript client connects to this website and sends it JSON messages.

Now, when my JS client closes the connection ( function disconnect), it takes about 30 seconds before the closedserver function is called

Here is the code:

Server

class ControlWebSocket(WebSocket):

    def opened(self):
        print("Client connected")

    def closed(self, code, reason=None):
        print("Connection closed [%d]" % (code))

    def received_message(self, m):
        #some business logic using 'm'
server = make_server('', 8999, server_class=WSGIServer, handler_class=WebSocketWSGIRequestHandler, app=WebSocketWSGIApplication(handler_cls=ControlWebSocket))
server.socket = ssl.wrap_socket (server.socket, certfile='./cert.pem', keyfile='key.pem', server_side=True)
server.initialize_websockets_manager()
server.serve_forever()

Client

ws = new WebSocket("wss://192.168.42.1:8999/");

ws.onopen = function() {
    console.log("Connected to WebSocket server");
};

ws.onmessage = function(e) {
    //some business logic using e
};

ws.onclose = function() {
    console.log("Disconnected from WebSocketServer");
};

function disconnect() {
    console.log("Disconnecting from WebSocketServer");
    ws.close();
}

Any tips on why it takes so long to close the connection? Are there any ways to quickly disconnect?

+4
source share
1 answer

This is a problem with your client side. If you look in the module ws. Upstairs you

const closeTimeout = 30 * 1000; // Allow 30 seconds to terminate the connection cleanly.

And then you have the code below

  if (!this._finalized) {
    if (this._closeFrameReceived) this._socket.end();

    //
    // Ensure that the connection is cleaned up even when the closing
    // handshake fails.
    //
    this._closeTimer = setTimeout(this._finalize, closeTimeout, true);
  }

, 30 . const, configurable . , , ws.finalize() ws.close()

const WebSocket = require('ws');
let ws = new WebSocket("wss://127.0.0.1:8999/", {
    rejectUnauthorized: false
});

ws.onopen = function() {
    console.log("Connected to WebSocket server");
};

ws.onmessage = function(e) {
    //some business logic using e
};

ws.onclose = function() {
    console.log("Disconnected from WebSocketServer");
};

function disconnect() {
    console.log("Disconnecting from WebSocketServer");
    ws.close();
    ws.finalize();
}

setTimeout(disconnect, 100)

Edit-1

, ws4py , rfc6455.

NodeJS, \x88\x80, , . ws4py/streaming.py , . , . 30- .

, python, . NodeJS WebSocket.server, .

NodeJS? ,

https://github.com/websockets/ws/blob/master/examples/ssl.js

'use strict';

const https = require('https');
const fs = require('fs');

const WebSocket = require('..');

const server = https.createServer({
  cert: fs.readFileSync('../test/fixtures/certificate.pem'),
  key: fs.readFileSync('../test/fixtures/key.pem')
});

const wss = new WebSocket.Server({ server });

wss.on('connection', function connection (ws) {
  ws.on('message', function message (msg) {
    console.log(msg);
  });
});

server.listen(function listening () {
  //
  // If the `rejectUnauthorized` option is not `false`, the server certificate
  // is verified against a list of well-known CAs. An 'error' event is emitted
  // if verification fails.
  //
  // The certificate used in this example is self-signed so `rejectUnauthorized`
  // is set to `false`.
  //
  const ws = new WebSocket(`wss://localhost:${server.address().port}`, {
    rejectUnauthorized: false
  });

  ws.on('open', function open () {
    ws.send('All glory to WebSockets!');
  });
});
+2

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


All Articles