aboutsummaryrefslogtreecommitdiffstats
path: root/ws_app.py
blob: cab43ac088c857ed0654b8575cc8510d82839f0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# coding=utf-8
import os

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)


app = WebSocketServer(
    ('', os.environ.get("WS_PORT", 8080)),
    Resource({
        "/ws/echo": EchoApplication  # Dicts are ordered in Python 3.6
    })
)

if __name__ == "__main__":
    app.serve_forever()