Python 了解高速公路和扭曲的集成

Python 了解高速公路和扭曲的集成,python,websocket,twisted,autobahn,Python,Websocket,Twisted,Autobahn,我试图理解这里给出的例子: 我构建了这个脚本,该脚本应该处理多个发布/订阅websocket连接,并为传入的控制消息打开一个tcp端口(8123)。当8123端口上出现消息时,应用程序应向所有连接的订户广播在8123端口接收到的消息。如何使NotificationProtocol或NotificationFactory与websocket对话并使websocket服务器广播消息 另一件我不明白的事情是url。客户端javascript连接到url http://:8080/ws。“ws”来自哪里

我试图理解这里给出的例子:

我构建了这个脚本,该脚本应该处理多个发布/订阅websocket连接,并为传入的控制消息打开一个tcp端口(8123)。当8123端口上出现消息时,应用程序应向所有连接的订户广播在8123端口接收到的消息。如何使NotificationProtocol或NotificationFactory与websocket对话并使websocket服务器广播消息

另一件我不明白的事情是url。客户端javascript连接到url http://:8080/ws。“ws”来自哪里

还有人能解释一下RouterFactory、RouterSessionFactory和此位的用途吗:

from autobahn.wamp import types
session_factory.add( WsNotificationComponent(types.ComponentConfig(realm = "realm1" )))
我的代码如下:

import sys, time
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, Factory
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession
from autobahn.twisted.util import sleep


class NotificationProtocol(Protocol):
    def __init__(self, factory):
        self.factory = factory

    def dataReceived(self, data):
        print "received new data"

class NotificationFactory(Factory):
    protocol = NotificationProtocol

class WsNotificationComponent(ApplicationSession):
   @inlineCallbacks
   def onJoin(self, details):
      counter = 0
      while True:
         self.publish("com.myapp.topic1", "test %d" % counter )
         counter += 1
         yield sleep(1)



## we use an Autobahn utility to install the "best" available Twisted reactor
   ##
from autobahn.twisted.choosereactor import install_reactor
reactor = install_reactor()

## create a WAMP router factory
##
from autobahn.wamp.router import RouterFactory
router_factory = RouterFactory()

## create a WAMP router session factory
##
from autobahn.twisted.wamp import RouterSessionFactory
session_factory = RouterSessionFactory(router_factory)

from autobahn.wamp import types
session_factory.add( WsNotificationComponent(types.ComponentConfig(realm = "realm1" )))

from autobahn.twisted.websocket import WampWebSocketServerFactory
transport_factory = WampWebSocketServerFactory(session_factory)
transport_factory.setProtocolOptions(failByDrop = False)


from twisted.internet.endpoints import serverFromString
## start the server from an endpoint
##
server = serverFromString(reactor, "tcp:8080")
server.listen(transport_factory)

notificationFactory = NotificationFactory()
reactor.listenTCP(8123, notificationFactory)

reactor.run()
“如何使
NotificationProtocol
NotificationFactory
与websocket对话并使websocket服务器广播消息:”

请查看我在SO上的另一个答案:。跳转到示例代码并对websocket逻辑进行建模,如“IO”逻辑,您将有一个很好的匹配(您可能还希望看到来自twisted core团队的关于的后续答案)

“ws”来自哪里?”

WebSocket是通过重新设置http连接的任务来实现的,而http连接的本质是在请求上必须有一个特定的路径。“ws”路径通常会映射到autobahn为您构建的一个特殊http处理程序,以处理WebSocket(或者至少这是您的javascript所期望的…)。假设设置正确,您实际上可以将web浏览器指向该url,它应该会打印回有关websocket握手的错误(
我使用的是预期的websocket头,但我使用的是websocket而不是autobahn)

另外,“websockets必须有一个特定的路径”的一个很酷的副作用是,您实际上可以在同一个处理程序/listen/port上混合websockets和普通http内容,当您尝试在同一个SSL端口上运行它们时,这非常方便,因为您试图避免在代码结尾使用代理前端的要求