Created
April 29, 2017 01:33
-
-
Save looneym/13d71bf03ca04f12eb6cafcef3273765 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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