def __init__(self, queue_class=Queue.Queue, wsgi_handler=None): self._queue_class = queue_class self.messages = queue_class() def static_wsgi_app(environ, start_response): start_response("200 OK", [("Content-Type", "text/html")]) return 'Ready for WebSocket connection in /ws' self.handle = Resource({ '/': static_wsgi_app if wsgi_handler is None else wsgi_handler, '/ws': WSApplicationFactory(self.messages, queue_class) })
def run(self): static_path = os.path.join(self.path, 'static') # XXX naming routes = [('^/static/', self.make_static_application('/static/', static_path)), ('^/$', self.serve_index), ('^/ws$', WebSocketAPI)] data = { 'raiden': self.raiden, 'port': self.port, 'events': self.events } resource = Resource(routes, extra=data) host_port = ('', self.port) server = WebSocketServer( host_port, resource, debug=True, ) server.serve_forever()
RPCTestClass()) self.wamp.register_pubsub("http://localhost:8000/somechannel") print "opened" def on_message(self, message): print "message: ", message super(WampApplication, self).on_message(message) def on_close(self): print "closed" def add(self, var, has): has.update({'bloep': var}) return has def build_protocol(self): self.wamp = WampProtocol(self) return self.wamp @classmethod def protocol(cls): return WampProtocol.PROTOCOL_NAME if __name__ == "__main__": resource = Resource({'/': WampApplication}) server = WebSocketServer(("", 8000), resource, debug=True) server.serve_forever()
def on_open(self): wamp = self.protocol wamp.register_procedure("http://localhost:8000/calc#add", self.add) wamp.register_object("http://localhost:8000/test#", RPCTestClass()) wamp.register_pubsub("http://localhost:8000/somechannel") print "opened" def on_message(self, message): print "message: ", message super(WampApplication, self).on_message(message) def on_close(self, reason): print "closed" def add(self, var1, var2): return var1 + var2 def static_wsgi_app(environ, start_response): start_response("200 OK", [("Content-Type", "text/html")]) return open("wamp_example.html").readlines() if __name__ == "__main__": resource = Resource({'/page': static_wsgi_app, '/': WampApplication}) server = WebSocketServer(("", 8000), resource, debug=True) server.serve_forever()
def on_close(self, *args, **kwargs): broker.unsubscribe('room1', self) def on_message(self, message, *args, **kwargs): if not message: return data = json.loads(message) data['user'] = self.userid.hex broker.publish('room1', data) def on_broadcast(self, data): self.ws.send(json.dumps(data)) def index(environ, start_response): start_response('200 OK', [('Content-type','text/html')]) html = open('index.html', 'rb').read() return [html] application = Resource([ ('^/chat', Chat), ('^/', index) ]) if __name__ == '__main__': #print("script mode") WSGIServer('{}:{}'.format('0.0.0.0', 8000), application, handler_class=WebSocketHandler).serve_forever()
def on_open(self): wamp = self.protocol wamp.register_procedure("http://localhost:8000/calc#add", self.add) wamp.register_object("http://localhost:8000/db#", KeyValue()) print("opened") def on_message(self, message): print("message: ", message) super(WampApplication, self).on_message(message) def on_close(self, reason): print("closed") def add(self, x, y): return int(x) + int(y) def static_wsgi_app(environ, start_response): start_response("200 OK", [("Content-Type", "text/html")]) return open("wamp_example.html").readlines() if __name__ == "__main__": resource = Resource({ '^/wamp_example$': WampApplication, '^/$': static_wsgi_app }) server = WebSocketServer(("", 8000), resource, debug=True) server.serve_forever()
def runserver(): server = WebSocketServer(('0.0.0.0', 5000), Resource([('^/chat', ChatApplication), ('^/.*', DebuggedApplication(app))]), debug=True) server.serve_forever()
protocol_class = WampProtocol def on_open(self): wamp = self.protocol wamp.register_procedure("http://localhost:8000/calc#add", self.add) wamp.register_object("http://localhost:8000/db#", KeyValue()) print "opened" def on_message(self, message): print "message: ", message super(WampApplication, self).on_message(message) def on_close(self, reason): print "closed" def add(self, x, y): return int(x) + int(y) def static_wsgi_app(environ, start_response): start_response("200 OK", [("Content-Type", "text/html")]) return open("wamp_example.html").readlines() if __name__ == "__main__": resource = Resource([('^/wamp_example$', WampApplication), ('^/$', static_wsgi_app)]) server = WebSocketServer(("", 8000), resource, debug=True) server.serve_forever()
class Chat(WebSocketApplication): def on_open(self, *args, **kwargs): client_web_sockets[id(self)] = self print("** [%s] client connected **" % id(self)) def on_close(self, *args, **kwargs): client_web_sockets.pop(id(self)) print("**[%s] client closed **" % id(self)) def on_message(self, message, *args, **kwargs): try: for i in client_web_sockets.values(): if i == self: continue i.ws.send(message) except Exception as e: print(e) def on_broadcast(self, data): pass application = Resource([ ('^/', Chat), ]) if __name__ == '__main__': print("************** WebSocket server started **************\n") WSGIServer('0.0.0.0:80', application, handler_class=WebSocketHandler).serve_forever()
listener_thread.start() def on_message(self, message, *args, **kwargs): if not message: return data = json.loads(message) data["user"] = self.userid.hex body = json.dumps(data) # logger.info('On published %s' % body) publish(body, "room1") def on_broadcast(self, ch, method, properties, body): # logger.info('On broadcast %s' % body) data = json.loads(str(body, "utf-8")) self.ws.send(json.dumps(data)) def index(environ, start_response): start_response("200 OK", [("Content-type", "text/html")]) html = open("index.html", "rb").read() return [html] application = Resource([("^/chat", Chat), ("^/", index)]) if __name__ == "__main__": WSGIServer("{}:{}".format("0.0.0.0", 8000), application, handler_class=WebSocketHandler).serve_forever()