-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
121 lines (95 loc) · 3.71 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import json
import os
from faker import Factory
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
from flask_redis import FlaskRedis
from flask_socketio import SocketIO, emit
from twilio.base.exceptions import TwilioRestException
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import VideoGrant
from twilio.rest import Client
app = Flask(__name__, static_folder="static/ui/build")
fake = Factory.create()
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")
app.config['REDIS_URL'] = os.getenv('REDIS_URL', 'redis://{}:6379'.format(os.environ.get('REDIS_HOST', 'redis')))
redis_client = FlaskRedis(app)
# Substitute your Twilio AccountSid and ApiKey details
account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY']
api_secret = os.environ['TWILIO_API_SECRET']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
master_token = AccessToken(account_sid, api_key, api_secret)
master_client = Client(account_sid, auth_token)
# Serve React App
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
if path != "" and os.path.exists(app.static_folder + '/' + path):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, 'index.html')
@app.route('/token-room', methods=['POST'])
def token_room():
identity = request.get_json()['identity']
room_name = request.get_json()['room']
token = AccessToken(account_sid, api_key, api_secret)
client = Client(account_sid, auth_token)
try:
room = client.video.rooms.create(
enable_turn=True,
type='peer-to-peer',
unique_name=room_name
)
except TwilioRestException as e:
room = client.video.rooms.list(unique_name=room_name, limit=1)[0]
if not room:
raise AttributeError("Room Could not be created!")
token.identity = identity
grant = VideoGrant(room=room.unique_name)
token.add_grant(grant)
token_s = str(token.to_jwt().decode("utf-8"))
return jsonify({'identity': identity, 'token': token_s})
@app.route('/delete-room', methods=['POST'])
def delete_room():
room_name = request.get_json()['room']
client = Client(account_sid, auth_token)
room = client.video.rooms(room_name).update(status='completed')
return jsonify({
'msg': "{} was deleted!".format(room.unique_name)
})
@app.route('/delete-all-rooms', methods=['POST'])
def delete_all_rooms():
rooms = master_client.video.rooms.list(status='in-progress')
for record in rooms:
master_client.video.rooms(record.sid).update(status='completed')
return jsonify({
'msg': "All Rooms were deleted!"
})
@socketio.on('register')
def test_message(message):
room_name = message['data']['roomName']
user = message['data']['username']
rooms_users_dict = json.loads(redis_client.get('rooms_users') or "{}")
users_room = rooms_users_dict.get(room_name, [])
users_room.append(user)
rooms_users_dict[room_name] = list(set(users_room))
redis_client.set('rooms_users', json.dumps(rooms_users_dict))
print(rooms_users_dict)
emit('user-registered', {'users': rooms_users_dict[room_name]}, broadcast=True)
@socketio.on('call')
def test_message(message):
print("incoming calls")
emit('calls', {'data': message['data']}, broadcast=True)
@socketio.on('my broadcast event')
def test_message(message):
emit('my response', {'data': message['data']}, broadcast=True)
@socketio.on('connect', )
def test_connect():
pass
@socketio.on('disconnect')
def test_disconnect():
pass
if __name__ == '__main__':
socketio.run(debug=True, port=5000, app=app, host="0.0.0.0")