简体   繁体   English

了解高速公路和扭曲的融合

[英]Understanding Autobahn and Twisted integration

I am trying to understand the examples given here: https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/basic/pubsub/basic 我正在尝试理解此处给出的示例: https : //github.com/tavendo/AutobahnPython/tree/master/examples/twisted/wamp/basic/pubsub/basic

I built this script which is supposed to handle multiple pub/sub websocket connections and also open a tcp port ( 8123 ) for incoming control messages. 我构建了该脚本,该脚本应该处理多个pub / sub websocket连接,并且还为传入的控制消息打开了一个tcp端口(8123)。 When a message comes on the 8123 port, the application should broadcast to all the connected subscribers the message received on port 8123. How do i make NotificationProtocol or NotificationFactory talk to the websocket and make the websocket server broadcast a message. 当消息在8123端口上出现时,应用程序应向所有连接的订户广播在端口8123上接收到的消息。如何使NotificationProtocol或NotificationFactory与Websocket对话并使Websocket服务器广播消息。

Another thing that i do not understand is the url. 我不明白的另一件事是网址。 The client javascript connects to the url http://:8080/ws . 客户端javascript连接到URL http://:8080 / ws。 Where does the "ws" come from ? “ ws”来自哪里?

Also can someone explain the purpose of RouterFactory, RouterSessionFactory and this bit: 也有人可以解释一下RouterFactory,RouterSessionFactory的用途以及以下内容:

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

my code is below: 我的代码如下:

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

" How do i make NotificationProtocol or NotificationFactory talk to the websocket and make the websocket server broadcast a message ": 如何使NotificationProtocolNotificationFactory与websocket对话并使Websocket服务器广播消息 ”:

Check out one of my other answers on SO: Persistent connection in twisted . 在SO上查看我的其他答案之一: Twisted中的持久连接 Jump down to the example code and model your websocket logic like the "IO" logic and you'll have a good fit (You might also want to see the follow-on answer about the newer endpoint calls from one of the twisted core-team too) 跳至示例代码,并对Websocket逻辑(如“ IO”逻辑)进行建模,就很合适(您可能还想查看有关来自扭曲核心团队之一的较新终结点调用的后续答案。太)

" Where does the "ws" come from ? " ws”来自哪里?

Websockets are implemented by retasking http connections, which by their nature have to have a specific path on the request. Websocket通过重新分配http连接的任务来实现,http连接本质上必须在请求上具有特定的路径。 That "ws" path typically would map to a special http handler that autobahn is building for you to process websockets (or at least that's what your javascript is expecting...). 该“ ws”路径通常会映射到autobahn为您构建的特殊http处理程序,以处理websocket(或者至少是您的javascript期望的……)。 Assuming thing are setup right you can actually point your web-browswer at that url and it should print back an error about the websocket handshake ( Expected WebSocket Headers in my case, but I'm using cyclones websockets not autobahn). 假设设置正确,您实际上可以将Web浏览器指向该URL,它应该会打印回有关websocket握手的错误(在我的情况下是Expected WebSocket Headers ,但我使用的是气旋 websockets而不是高速公路)。

PS one of the cool side-effects from "websockets must have a specific path" is that you can actually mix websockets and normal http content on the same handler/listen/port, this gets really handy when your trying to run them all on the same SSL port because your trying to avoid the requirement of a proxy front-ending your code. PS:“ websockets必须具有特定路径”的一个很酷的副作用是,您实际上可以在同一处理程序/侦听/端口上混合使用websockets和正常的http内容,当您尝试在所有这些上运行它们时,这真的很方便相同的SSL端口,因为您试图避免要求在代码前端添加代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM