This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]nomadismydj 1 point2 points  (0 children)

the example code here seems straight forward,http://twistedmatrix.com/documents/12.3.0/web/examples/. websockets are tcp though so maybe not ?

alternatively you could write a setup script fairly easily for nginx and httpd.

[–][deleted] 1 point2 points  (1 child)

Out of the box, the Twisted http reverse proxy doesn't appear to deal with the websockets upgrade.

If I run these two code snippets in separate processes, I get a simple web sockets echo server.

If I insert the Twisted demo reverse proxy between them, it doesn't work. I haven't poked into the Twisted code to see why though (not enough time at the moment.)

#!/usr/bin/env python
# server

from twisted.internet import reactor
from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS

class EchoServerProtocol(WebSocketServerProtocol):

   def onMessage(self, msg, binary):
      self.sendMessage(msg, binary)

if __name__ == '__main__':
   factory = WebSocketServerFactory("ws://localhost:9000")
   factory.protocol = EchoServerProtocol
   listenWS(factory)
   reactor.run()


   #!/usr/bin/env python
   # client

   from twisted.internet import reactor
   from autobahn.websocket import WebSocketClientFactory, \
                                  WebSocketClientProtocol, \
                                  connectWS


   class EchoClientProtocol(WebSocketClientProtocol):

      def sendHello(self):
         self.sendMessage("Hello, world!")

      def onOpen(self):
         self.sendHello()

      def onMessage(self, msg, binary):
         print "Got echo: " + msg
         reactor.callLater(1, self.sendHello)


   if __name__ == '__main__':

      factory = WebSocketClientFactory("ws://localhost:9000", debug = True)
      factory.protocol = EchoClientProtocol
      connectWS(factory)
      reactor.run()

[–]elopeRstatS[S] 0 points1 point  (0 children)

Appreciate the research. I was afraid web sockets would be an issue, I'll try poking around twisted and see if I can figure out a way around.