我使用 ws4py.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()
客户
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();
}
关于为什么关闭连接这么长时间的任何线索?有什么方法可以更快地终止连接吗?