[英]twisted autobahn websocket being initialized twice with wss
我有一些通過Twisted實現的websocket協議,當我使用“ ws”連接時,它們可以正常工作,但是當我啟用安全的websocket時, __init__
方法被調用了兩次。 更具體地說,它被調用一次,然后連接顯然失敗,並調用connectionLost,然后再次調用__init__
,這一次連接保持打開狀態。
下面的代碼說明了這一點。 當我與wss連接時,websocket協議的__init__
中的日志行被調用了兩次,但是普通的websocket不會發生這種情況。
從datetime導入socket從twisted.internet導入反應器導入datetime
from twisted.internet.ssl import DefaultOpenSSLContextFactory
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory, listenWS
import txaio
txaio.use_twisted()
CERT_KEY = "certificate.key"
CERT_PATH = "certificate.crt"
def log(msg):
print("{}: {}".format(str(datetime.now()), msg))
class TestProtocol(WebSocketServerProtocol):
def __init__(self):
super(TestProtocol, self).__init__()
log("Test protocol init")
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
log("Connection closed: Reason is {}".format(reason))
class TestProtocolFactory(WebSocketServerFactory):
protocol = TestProtocol
def init_websocket_protocol(factory_cls, port):
try:
key, crt = CERT_KEY, CERT_PATH
context_factory = DefaultOpenSSLContextFactory(key, crt)
connection_string = "wss://localhost:{}".format(str(port))
factory = factory_cls(connection_string)
listenWS(factory, contextFactory=context_factory)
log("Port {} bound to test websocket server".format(str(port)))
except socket.error as e:
log("Server was unable to bind to a new port: ".format(str(e)))
def main():
init_websocket_protocol(TestProtocolFactory, 9000)
reactor.run()
if __name__ == '__main__':
main()
這些天推薦的API是使用端點。 另外, twisted.internet.ssl.CertificateOptions
是TLS連接的首選API。 因此,通過這些更改,上面的代碼將如下所示:
from datetime import datetime
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from twisted.internet.ssl import CertificateOptions, PrivateCertificate, Certificate, KeyPair
from twisted.internet.endpoints import SSL4ServerEndpoint
from twisted.internet.task import react
from OpenSSL import crypto
CERT_KEY = "certificate.key"
CERT_PATH = "certificate.crt"
def log(msg):
print("{}: {}".format(str(datetime.now()), msg))
class TestProtocol(WebSocketServerProtocol):
def __init__(self):
super(TestProtocol, self).__init__()
log("Test protocol init")
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
log("Connection closed: Reason is {}".format(reason))
class TestProtocolFactory(WebSocketServerFactory):
protocol = TestProtocol
def init_websocket_protocol(reactor, port):
with open(CERT_KEY) as key_file, open(CERT_PATH) as cert_file:
key = KeyPair.load(key_file.read(), crypto.FILETYPE_PEM).original
cert = Certificate.loadPEM(cert_file.read()).original
ctx = CertificateOptions(
privateKey=key,
certificate=cert,
)
return SSL4ServerEndpoint(reactor, port, ctx)
def main(reactor):
ep = init_websocket_protocol(reactor, 9000)
ep.listen(TestProtocolFactory())
reactor.run()
if __name__ == '__main__':
react(main)
當我運行此代碼並將Firefox指向它時,它會連接一次。 您正在使用的瀏覽器端代碼是什么樣的?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.