目前我正在使用python中的twisted。如何在程序退出时正确关闭websocket实例。只是“reactor.stop”不会做这个工作。 这是代码:
class MyWebSocketProtocol(WebSocketServerProtocol):
def onConnect(self, request):
self.log("Client connecting: {0}".format(request.peer))
def onOpen(self):
self.log("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if payload == "bandwidth":
content = json.dumps(NetworkMessages.data, ensure_ascii=False).encode('utf8')
self.sendMessage(content)
self.log("Sent: {}".format(content))
else:
self.log("Unknown request!")
def onClose(self, wasClean, code, reason):
self.log("WebSocket connection closed: {0}".format(reason))
def log(self, message):
DataLogger.writeActualData(WebSocketServer.NAME, [message])
class WebSocketServer(object):
def __init__(self,reactor,address,port,dt,wifi):
factory = WebSocketServerFactory(address)
factory.protocol = MyWebSocketProtocol
reactor.listenTCP(port, factory)