简体   繁体   中英

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

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. 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.

Another thing that i do not understand is the url. The client javascript connects to the url http://:8080/ws . Where does the "ws" come from ?

Also can someone explain the purpose of RouterFactory, RouterSessionFactory and this bit:

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 ":

Check out one of my other answers on SO: Persistent connection in 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)

" Where does the "ws" come from ? "

Websockets are implemented by retasking http connections, which by their nature have to have a specific path on the request. 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...). 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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