From cf999735856504fee0bfe74d9b66e764b71ae746 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Tue, 13 Feb 2018 10:45:57 +0000 Subject: Websocket echo test --- pysite/views/main/ws_test.py | 15 +++++++++++++++ requirements.txt | 2 ++ templates/ws_test.html | 24 ++++++++++++++++++++++++ ws_app.py | 27 +++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 pysite/views/main/ws_test.py create mode 100644 templates/ws_test.html create mode 100644 ws_app.py diff --git a/pysite/views/main/ws_test.py b/pysite/views/main/ws_test.py new file mode 100644 index 00000000..c28269a8 --- /dev/null +++ b/pysite/views/main/ws_test.py @@ -0,0 +1,15 @@ +# coding=utf-8 +import os + +from pysite.base_route import RouteView + + +class WSTest(RouteView): + path = "/ws_test" + name = "ws_test" + + def get(self): + return self.render( + "ws_test.html", + server_name=os.environ.get("SERVER_NAME", "localhost") + ) diff --git a/requirements.txt b/requirements.txt index ccf9ac97..4ae185cd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ rethinkdb requests gevent gevent-websocket +wsaccel +ujson diff --git a/templates/ws_test.html b/templates/ws_test.html new file mode 100644 index 00000000..fd8dd3a3 --- /dev/null +++ b/templates/ws_test.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} +{% block title %}WS Test{% endblock %} +{% block content %} +
+

Open your JS console to test

+ + +
+{% endblock %} \ No newline at end of file diff --git a/ws_app.py b/ws_app.py new file mode 100644 index 00000000..a9f60175 --- /dev/null +++ b/ws_app.py @@ -0,0 +1,27 @@ +# coding=utf-8 + +from collections import OrderedDict + +from geventwebsocket import Resource, WebSocketApplication, WebSocketServer + + +class EchoApplication(WebSocketApplication): + def on_open(self): + print("Connection opened") + + def on_message(self, message, **kwargs): + print(f"<- {message}") + self.ws.send(message) + print(f"-> {message}") + + def on_close(self, reason): + print(reason) + + +if __name__ == "__main__": + app = WebSocketServer( + ('', 8000), + Resource(OrderedDict([('/ws/echo', EchoApplication)])) + ) + + app.serve_forever() -- cgit v1.2.3