forked from YashMakan/flask_webrtc_youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
97 lines (73 loc) · 2.94 KB
/
server.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
from flask import Flask, render_template, request, session
from flask_socketio import SocketIO, emit, join_room
import platform
app = Flask(__name__)
app.config['SECRET_KEY'] = "wubba lubba dub dub"
socketio = SocketIO(app)
users_in_room = {}
rooms_sid = {}
names_sid = {}
@app.route("/join", methods=["GET"])
def join():
display_name = request.args.get('display_name')
mute_audio = request.args.get('mute_audio') # 1 or 0
mute_video = request.args.get('mute_video') # 1 or 0
room_id = request.args.get('room_id')
session[room_id] = {"name": display_name,
"mute_audio": mute_audio, "mute_video": mute_video}
return render_template("join.html", room_id=room_id, display_name=session[room_id]["name"], mute_audio=session[room_id]["mute_audio"], mute_video=session[room_id]["mute_video"])
@socketio.on("connect")
def on_connect():
sid = request.sid
print("New socket connected ", sid)
@socketio.on("join-room")
def on_join_room(data):
sid = request.sid
room_id = data["room_id"]
display_name = session[room_id]["name"]
# register sid to the room
join_room(room_id)
rooms_sid[sid] = room_id
names_sid[sid] = display_name
# broadcast to others in the room
print("[{}] New member joined: {}<{}>".format(room_id, display_name, sid))
emit("user-connect", {"sid": sid, "name": display_name},
broadcast=True, include_self=False, room=room_id)
# add to user list maintained on server
if room_id not in users_in_room:
users_in_room[room_id] = [sid]
emit("user-list", {"my_id": sid}) # send own id only
else:
usrlist = {u_id: names_sid[u_id]
for u_id in users_in_room[room_id]}
# send list of existing users to the new member
emit("user-list", {"list": usrlist, "my_id": sid})
# add new member to user list maintained on server
users_in_room[room_id].append(sid)
print("\nusers: ", users_in_room, "\n")
@socketio.on("disconnect")
def on_disconnect():
sid = request.sid
room_id = rooms_sid[sid]
display_name = names_sid[sid]
print("[{}] Member left: {}<{}>".format(room_id, display_name, sid))
emit("user-disconnect", {"sid": sid},
broadcast=True, include_self=False, room=room_id)
users_in_room[room_id].remove(sid)
if len(users_in_room[room_id]) == 0:
users_in_room.pop(room_id)
rooms_sid.pop(sid)
names_sid.pop(sid)
print("\nusers: ", users_in_room, "\n")
@socketio.on("data")
def on_data(data):
sender_sid = data['sender_id']
target_sid = data['target_id']
if sender_sid != request.sid:
print("[Not supposed to happen!] request.sid and sender_id don't match!!!")
if data["type"] != "new-ice-candidate":
print('{} message from {} to {}'.format(
data["type"], sender_sid, target_sid))
socketio.emit('data', data, room=target_sid)
if any(platform.win32_ver()):
socketio.run(app, debug=True)