ws4型py.websocket网站:关闭来自客户端的连接长延迟

2024-09-27 21:28:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用ws4创建了一个安全的WebSocket服务器websocket网站包裹。在

javascript客户机连接到这个Websocket并向它发送一些JSON消息。在

现在,当我的JS客户机关闭连接(function disconnect)时,大约需要30秒才能调用服务器的closed函数

代码如下:

服务器

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()

客户

^{pr2}$

有什么线索可以解释为什么这么长时间才能关闭连接?有什么方法可以更快地终止连接吗?在


Tags: self服务器客户机serverdefcodesocketpem
1条回答
网友
1楼 · 发布于 2024-09-27 21:28:49

这是你的客户端问题。如果您查看ws模块。在你的顶端

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

然后你有下面的代码

^{pr2}$

这是为了确保连接不会在30秒前关闭,并给各方足够的时间来清理连接。这一次是const,而不是{}。但是如果你想立即终止,你可以在ws.close()后面加一个ws.finalize(),它会立即断开连接

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)

编辑-1

在深入挖掘之后,ws4py似乎有一些与{a1}不符的东西。在

当您从NodeJS执行close操作时,它将发送一个关闭帧\x88\x80,该帧指示应该关闭连接并发送一个close帧。文件ws4py/streaming.py存在一些问题,它无法正确响应此帧。它基本上是希望读取更多的数据,但在等待相同的数据时却被卡住了。这将导致连接从客户端关闭,默认超时为30秒。在

这是您正在使用的python库中的一个bug。我用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!');
  });
});

相关问题 更多 >

    热门问题