def start_server(): print("Running %s from %s" % (agent, path)) geventwebsocket.WebSocketServer( ("0.0.0.0", 9091), app, debug=True, keyfile=cert_dir + 'www.key', certfile=cert_dir + 'www.pem').serve_forever()
def get_web_server(self, listen_addr, debug=False, **ssl_args): """Setup WebSocketServer on listen_addr (host, port).""" return geventwebsocket.WebSocketServer( listen_addr, self.resource, debug=debug, **{key: val for key, val in ssl_args.items() if val is not None})
def listen(self, host: str, port: int): server = geventws.WebSocketServer((host, port), socketio.WSGIApp(self)) print(f'Server listening @ {host} on port {port}') try: server.serve_forever() except KeyboardInterrupt: print('Server shutting down') server.close() exit(0)
def runtime_application_task(): """ This greenlet runs the websocket server that responds remote commands that inspect/manipulate the Runtime. """ r = geventwebsocket.Resource( OrderedDict([('/', create_websocket_application(runtime))])) s = geventwebsocket.WebSocketServer(('', args.port), r) s.serve_forever()
def websocket_application_task(host, port): """ This greenlet runs the websocket server that responds to remote commands that inspect/manipulate the Runtime. """ address = 'ws://{}:{:d}'.format(host, port) print('Runtime listening at {}'.format(address)) r = geventwebsocket.Resource( OrderedDict([('/', WebSocketRuntimeApplication)])) server = geventwebsocket.WebSocketServer(('', port), r) server.serve_forever()
def runtime_application_task(): """ This greenlet runs the websocket server that responds to remote commands that inspect/manipulate the Runtime. """ print('Runtime listening at {}'.format(address)) WebSocketRuntimeApplication.runtimes[port] = runtime try: r = geventwebsocket.Resource( OrderedDict([('/', WebSocketRuntimeApplication)])) s = geventwebsocket.WebSocketServer(('', port), r) s.serve_forever() finally: WebSocketRuntimeApplication.runtimes.pop(port)
if publisher_rfd in rl: # Dispatch received message to all subscribed clients. try: data = publisher.recv() except ValueError: continue for client in clients[:]: client.send_queue(data) if __name__ == '__main__': publisher = nnpy.Socket(nnpy.AF_SP, nnpy.SUB) publisher.connect('ipc:///tmp/koruza-publish.ipc') publisher.setsockopt(nnpy.SUB, nnpy.SUB_SUBSCRIBE, '') command_bus = nnpy.Socket(nnpy.AF_SP, nnpy.REQ) command_bus.connect('ipc:///tmp/koruza-command.ipc') # Spawn the nanomsg to websocket router. gevent.spawn(router, publisher) # Start the web socket server. try: server = geventwebsocket.WebSocketServer(('', 80), app) server.serve_forever() except socket.error: server = geventwebsocket.WebSocketServer(('', 8080), app) server.serve_forever()
def serve(listen, debug=False, verbosity=1, debug_port=0, **ssl_args): """Spawn greenlets for handling websockets and PostgreSQL calls.""" import signal from django.apps import apps from django.db import connection, close_old_connections from django.utils.module_loading import import_string from dddp.postgres import PostgresGreenlet from dddp.websocket import DDPWebSocketApplication import gevent import geventwebsocket # shutdown existing connections close_old_connections() # setup PostgresGreenlet to multiplex DB calls pgworker = PostgresGreenlet(connection, debug=debug) DDPWebSocketApplication.pgworker = pgworker # use settings.WSGI_APPLICATION or fallback to default Django WSGI app from django.conf import settings if hasattr(settings, 'WSGI_APPLICATION'): wsgi_name = settings.WSGI_APPLICATION wsgi_app = import_string(wsgi_name) else: from django.core.wsgi import get_wsgi_application wsgi_app = get_wsgi_application() wsgi_name = str(wsgi_app.__class__) resource = geventwebsocket.Resource( collections.OrderedDict([ (r'/websocket', DDPWebSocketApplication), (r'^/sockjs/\d+/\w+/websocket$', DDPWebSocketApplication), (r'^/sockjs/\d+/\w+/xhr$', ddpp_sockjs_xhr), (r'^/sockjs/info$', ddpp_sockjs_info), (r'^/(?!(websocket|sockjs)/)', wsgi_app), ]), ) # setup WebSocketServer to dispatch web requests servers = [ geventwebsocket.WebSocketServer( (host, port), resource, debug=debug, **{key: val for key, val in ssl_args.items() if val is not None}) for host, port in listen ] def killall(*args, **kwargs): """Kill all green threads.""" pgworker.stop() for server in servers: server.stop() # die gracefully with SIGINT or SIGQUIT gevent.signal(signal.SIGINT, killall) gevent.signal(signal.SIGQUIT, killall) print('=> Discovering DDP endpoints...') api = apps.get_app_config('dddp').api api.pgworker = pgworker DDPWebSocketApplication.api = api print( '\n'.join(' %s' % api_path for api_path in sorted(api.api_path_map())), ) # start greenlets if debug_port: from gevent.backdoor import BackdoorServer servers.append( BackdoorServer( ('127.0.0.1', debug_port), banner='Django DDP', locals={ 'servers': servers, 'pgworker': pgworker, 'killall': killall, 'api': api, 'resource': resource, 'settings': settings, 'wsgi_app': wsgi_app, 'wsgi_name': wsgi_name, }, )) pgworker.start() print('=> Started PostgresGreenlet.') threads = [gevent.spawn(server.serve_forever) for server in servers] print('=> Started DDPWebSocketApplication.') print('=> Started your app (%s).' % wsgi_name) print('') for host, port in listen: print('=> App running at: http://%s:%d/' % (host, port)) gevent.joinall(threads) pgworker.stop() gevent.joinall([pgworker])