-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.py
57 lines (47 loc) · 2.02 KB
/
app.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import base64
import json
import logging
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
HTTP_SERVER_PORT = 5000
@sockets.route('/media')
def echo(ws):
app.logger.info("Connection accepted")
# A lot of messages will be sent rapidly. We'll stop showing after the first one.
has_seen_media = False
message_count = 0
while not ws.closed:
message = ws.receive()
if message is None:
app.logger.info("No message received...")
continue
# Messages are a JSON encoded string
data = json.loads(message)
# Using the event type you can determine what type of message you are receiving
if data['event'] == "connected":
app.logger.info("Connected Message received: {}".format(message))
if data['event'] == "start":
app.logger.info("Start Message received: {}".format(message))
if data['event'] == "media":
if not has_seen_media:
app.logger.info("Media message: {}".format(message))
payload = data['media']['payload']
app.logger.info("Payload is: {}".format(payload))
chunk = base64.b64decode(payload)
app.logger.info("That's {} bytes".format(len(chunk)))
app.logger.info("Additional media messages from WebSocket are being suppressed....")
has_seen_media = True
if data['event'] == "stop":
app.logger.info("Stop Message received: {}".format(message))
break
message_count += 1
app.logger.info("Connection closed. Received a total of {} messages".format(message_count))
if __name__ == '__main__':
app.logger.setLevel(logging.DEBUG)
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(('', HTTP_SERVER_PORT), app, handler_class=WebSocketHandler)
print("Server listening on: http://localhost:" + str(HTTP_SERVER_PORT))
server.serve_forever()