|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import uuid |
| 4 | +from ._uwsgi import uwsgi |
| 5 | + |
| 6 | + |
| 7 | +class WebSocketClient(object): |
| 8 | + ''' |
| 9 | + Default WebSocket client has a blocking recieve method, but still exports |
| 10 | + rest of uWSGI API. |
| 11 | + ''' |
| 12 | + def __init__(self, fd, timeout=60): |
| 13 | + self.fd = fd |
| 14 | + self.timeout = timeout |
| 15 | + self.id = str(uuid.uuid1()) |
| 16 | + |
| 17 | + def receive(self): |
| 18 | + return uwsgi.websocket_recv() |
| 19 | + |
| 20 | + def recv(self): |
| 21 | + return uwsgi.websocket_recv() |
| 22 | + |
| 23 | + def recv_nb(self): |
| 24 | + return uwsgi.websocket_recv_nb() |
| 25 | + |
| 26 | + def send(self, msg): |
| 27 | + return uwsgi.websocket_send(msg) |
| 28 | + |
| 29 | + def send_binary(self, msg): |
| 30 | + return uwsgi.websocket_send_binary(msg) |
| 31 | + |
| 32 | + def send_from_sharedarea(self, id, pos): |
| 33 | + return uwsgi.websocket_send_from_sharedarea(id, pos) |
| 34 | + |
| 35 | + def send_binary_from_sharedarea(self, id, pos): |
| 36 | + return uwsgi.websocket_send_binary_from_sharedarea(id, pos) |
| 37 | + |
| 38 | + |
| 39 | +class WebSocketMiddleware(object): |
| 40 | + ''' |
| 41 | + WebSocket Middleware that handles handshake and passes route a WebSocketClient. |
| 42 | + ''' |
| 43 | + client = WebSocketClient |
| 44 | + |
| 45 | + def __init__(self, wsgi_app, websocket): |
| 46 | + self.wsgi_app = wsgi_app |
| 47 | + self.websocket = websocket |
| 48 | + |
| 49 | + def __call__(self, environ, start_response): |
| 50 | + handler = self.websocket.routes.get(environ['PATH_INFO']) |
| 51 | + |
| 52 | + if handler: |
| 53 | + uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'], environ.get('HTTP_ORIGIN', '')) |
| 54 | + handler(self.client(uwsgi.connection_fd(), self.websocket.timeout)) |
| 55 | + else: |
| 56 | + return self.wsgi_app(environ, start_response) |
| 57 | + |
| 58 | + |
| 59 | +class WebSocket(object): |
| 60 | + ''' |
| 61 | + Flask extension which makes it easy to integrate uWSGI-powered WebSockets |
| 62 | + into your applications. |
| 63 | + ''' |
| 64 | + middleware = WebSocketMiddleware |
| 65 | + |
| 66 | + def __init__(self, app=None, timeout=60): |
| 67 | + if app: |
| 68 | + self.init_app(app) |
| 69 | + self.timeout = timeout |
| 70 | + self.routes = {} |
| 71 | + |
| 72 | + def run(self, app=None, debug=False, host='localhost', port=5000, **kwargs): |
| 73 | + if not app: |
| 74 | + app = self.app.name + ':app' |
| 75 | + |
| 76 | + # kwargs are treated as uwsgi arguments |
| 77 | + if kwargs.get('master') is None: |
| 78 | + kwargs['master'] = True |
| 79 | + |
| 80 | + # boolean should be treated as empty value |
| 81 | + for k,v in kwargs.items(): |
| 82 | + if v is True: |
| 83 | + kwargs[k] = '' |
| 84 | + |
| 85 | + # constructing uwsgi arguments |
| 86 | + uwsgi_args = ' '.join(['--{0} {1}'.format(k,v) for k,v in kwargs.items()]) |
| 87 | + args = 'uwsgi --http {0}:{1} --http-websockets {2} --wsgi {3}'.format(host, port, uwsgi_args, app) |
| 88 | + |
| 89 | + print args |
| 90 | + |
| 91 | + # set enviromental variable to trigger adding debug middleware |
| 92 | + if self.app.debug or debug: |
| 93 | + args = 'FLASK_UWSGI_DEBUG=true {0}'.format(args) |
| 94 | + |
| 95 | + # run uwsgi with our args |
| 96 | + sys.exit(os.system(args)) |
| 97 | + |
| 98 | + def init_app(self, app): |
| 99 | + self.app = app |
| 100 | + |
| 101 | + if os.environ.get('FLASK_UWSGI_DEBUG'): |
| 102 | + from werkzeug.debug import DebuggedApplication |
| 103 | + app.wsgi_app = DebuggedApplication(app.wsgi_app, True) |
| 104 | + app.debug = True |
| 105 | + |
| 106 | + app.wsgi_app = self.middleware(app.wsgi_app, self) |
| 107 | + app.run = lambda **kwargs: self.run(**kwargs) |
| 108 | + |
| 109 | + def route(self, rule): |
| 110 | + def decorator(f): |
| 111 | + self.routes[rule] = f |
| 112 | + return f |
| 113 | + return decorator |
0 commit comments