Skip to content

Instantly share code, notes, and snippets.

@looneym
Created April 29, 2017 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save looneym/13d71bf03ca04f12eb6cafcef3273765 to your computer and use it in GitHub Desktop.
Save looneym/13d71bf03ca04f12eb6cafcef3273765 to your computer and use it in GitHub Desktop.
import sys
from twisted.web.static import File
from twisted.python import log
from twisted.web.server import Site
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketServerFactory, \
WebSocketServerProtocol
from autobahn.twisted.resource import WebSocketResource
class SomeServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("some request connected {}".format(request))
def onMessage(self, payload, isBinary):
print(payload)
self.sendMessage("message received")
if __name__ == "__main__":
log.startLogging(sys.stdout)
# static file server seving index.html as root
root = File(".")
factory = WebSocketServerFactory(u"ws://127.0.0.1:8080")
factory.protocol = SomeServerProtocol
resource = WebSocketResource(factory)
# websockets resource on "/ws" path
root.putChild(u"ws", resource)
site = Site(root)
reactor.listenTCP(8080, site)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment