Twisted webserver and Autobahn WebSocket at the same time, same port

I have a server that listens for WebSocket connections on port 80 using Twisted and Autobahn. I want it to serve static HTML pages as well, as the client doesn't want to use WebSocket. Can both things be used at the same time using Twisted and Autobahn?

+3


source to share


2 answers


Of course, take a look here and here . You can run Twisted Web and add Autobahn-based Twisted Web to a WebSocket Web resource. You can add any number of Twisted Web resources to the resource tree.



In short, to start WebSocketServerFactory

manually by calling startFactory()

and then wrap it in a resource autobahn.twisted.resource.WebSocketResource

, which can then be registered with putChild anywhere in the Twisted Web hierarchy.

+5


source


I think you need to add haproxy to the mix. If you just want to use twisted and autobahns then I don't think you can share the port. Having said that, I have both my web ports and my web server listening on the same external port. I had to use haproxy to do the trick though ... haproxy handles the incoming connection and then propagates the connection based on the things it fetches from its environment. Every Wednesday is different. Basically, you run haproxy and then force your webservice and websocket to listen on private, different ports. In my case, I set my web socket server to 127.0.0.1:9000 and my web service to 127.0.0.1:8080. Then you create a haproxy.conf file for the haproxy configuration, in this example, for example:

global
    maxconn 100
    mode http
frontend myfrontend
    bind *:80
    acl is_websocket hdr(Upgrade) -i WebSocket
    use_backend ws if is_websocket
    default_backend mybackend
backend mybackend
     server s3 127.0.0.1:8080
backend ws
    timeout server 600s
    reqrep ^Host:\ .* \0:9000
    server ws1 127.0.0.1:9000

      



I had to remove a bunch of unrelated stuff from the haproxy.conf file, but that translates the idea. It was important for me to have only one port visible from the outside and not manage two.

haproxy is awesome! -g

0


source







All Articles