diff --git a/README.md b/README.md index f1137ef..7ca5cb0 100644 --- a/README.md +++ b/README.md @@ -36,13 +36,49 @@ Usage Example ws://localhost:62456/ws > RANDINT32 < 585865374 + > RANDUNIFORM < 0.70137183786 + > RANDNORMAL < -1.6120135370 + > RANDBYTES 16 < �����9L).�!:���@Nh������d����_q� + > SUBSCRIBEINT32 + < 1361330636 + < -604581511 + < 1510923919 + < ... + > UNSUBSCRIBE + < UNSUBSCRIBED + + > SUBSCRIBEUNIFORM + < 0.54623951886 + < 0.67567578799 + < 0.09746421443 + < ... + > UNSUBSCRIBE + < UNSUBSCRIBED + + > SUBSCRIBENORMAL + < -1.6120135370 + < 0.02943381135 + < -0.9883458007 + < ... + > UNSUBSCRIBE + < UNSUBSCRIBED + + > SUBSCRIBEBYTES 8 + < �����9L) + < .�!:���@ + < Nh������ + < d����_q� + < ... + > UNSUBSCRIBE + < UNSUBSCRIBED + License ------- diff --git a/quanttp/__main__.py b/quanttp/__main__.py index 000cb36..a9b41e1 100644 --- a/quanttp/__main__.py +++ b/quanttp/__main__.py @@ -18,6 +18,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import threading + from flask import Flask, request, Response from flask_sockets import Sockets from gevent import pywsgi @@ -59,22 +61,51 @@ def api_randbytes(): @sockets.route('/ws') def ws(websocket): + subscribed = [False] while not websocket.closed: - try: - message = websocket.receive().strip().lower() - split_message = message.split(' ') - if split_message[0] == 'randint32': - websocket.send(str(qng_wrapper.randint32())) - elif split_message[0] == 'randuniform': - websocket.send(str(qng_wrapper.randuniform())) - elif split_message[0] == 'randnormal': - websocket.send(str(qng_wrapper.randnormal())) - elif split_message[0] == 'randbytes': - websocket.send(qng_wrapper.randbytes(int(split_message[1]))) - except ValueError as e: - websocket.close(code=1003, message=str(e)) - except Exception as e: - websocket.close(code=1011, message=str(e)) + threading.Thread(target=handle_ws_message, args=(websocket.receive(), websocket, subscribed)).start() + + +def handle_ws_message(message, websocket, subscribed): + try: + split_message = message.strip().lower().split() + if split_message[0] == 'randint32': + websocket.send(str(qng_wrapper.randint32())) + elif split_message[0] == 'randuniform': + websocket.send(str(qng_wrapper.randuniform())) + elif split_message[0] == 'randnormal': + websocket.send(str(qng_wrapper.randnormal())) + elif split_message[0] == 'randbytes': + length = int(split_message[1]) + websocket.send(qng_wrapper.randbytes(length)) + elif split_message[0] == 'subscribeint32': + if not subscribed[0]: + subscribed[0] = True + while subscribed[0] and not websocket.closed: + websocket.send(str(qng_wrapper.randint32())) + elif split_message[0] == 'subscribeuniform': + if not subscribed[0]: + subscribed[0] = True + while subscribed[0] and not websocket.closed: + websocket.send(str(qng_wrapper.randuniform())) + elif split_message[0] == 'subscribenormal': + if not subscribed[0]: + subscribed[0] = True + while subscribed[0] and not websocket.closed: + websocket.send(str(qng_wrapper.randnormal())) + elif split_message[0] == 'subscribebytes': + chunk = int(split_message[1]) + if not subscribed[0]: + subscribed[0] = True + while subscribed[0] and not websocket.closed: + websocket.send(qng_wrapper.randbytes(chunk)) + elif split_message[0] == 'unsubscribe': + subscribed[0] = False + websocket.send('UNSUBSCRIBED') + except (ValueError, BlockingIOError): + pass + except Exception as e: + websocket.close(code=1011, message=str(e)) @app.errorhandler(Exception)