-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvidChatApp.py
155 lines (141 loc) · 8.21 KB
/
vidChatApp.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import logging
import threading
import time
import uuid
import json
import tornado.web
import tornado.websocket
logger = logging.getLogger(__name__)
# ===============================================================================
# MainHandler- Default request handler
# ===============================================================================
class MainHandler(tornado.web.RequestHandler):
"""Generic request handler that moves all http requests to https.
"""
def prepare(self):
pass
# if self.request.protocol == 'http':
# self.redirect('https://' + self.request.host, permanent=True)
# ===============================================================================
# HomePage- Default homepage for hello
# ===============================================================================
class HomePage(MainHandler):
def get(self):
self.render("sources/index.html")
# ===============================================================================
# ChatRoomSocket- Stream Web cam and connect
# ===============================================================================
class ChatRoomSocket(tornado.websocket.WebSocketHandler):
_ROOMCONNECTIONS = {}
def open(self):
self._name = None
self._id = None
logger.info(self._ROOMCONNECTIONS)
def on_message(self, message):
data = json.loads(message)
if data.get('joinChat'):
if data.get('joinChat') in self._ROOMCONNECTIONS:
self._ROOMCONNECTIONS[data.get('joinChat')].append(self)
else:
self._ROOMCONNECTIONS[data.get('joinChat')] = [self]
if not self._name:
self._name = data.get('personName')
if not self._id:
self._id = uuid.uuid4().hex[:8]
logger.info("{} Joined the {} chatroom".format(self._name,
data.get('joinChat')))
count = len(self._ROOMCONNECTIONS[data.get('joinChat')])
people = [
c._name for c in self._ROOMCONNECTIONS[data.get('joinChat')]]
# peers = [
# c._id for c in self._ROOMCONNECTIONS[data.get('joinChat')]]
for connection in self._ROOMCONNECTIONS[data.get('joinChat')]:
connection.write_message({'count': count,
'people': people,
'lastPeer': self._id,
'id': connection._id,
'messageType': 'init'})
else:
# this is imperative without this we do not know which room to control
if data.get('chatID'):
if data.get("messageType") == "text":
logger.info("{} messaged the {} chatroom".format(self._name,
data.get('chatID')))
count = len(self._ROOMCONNECTIONS[data.get('chatID')])
for connection in self._ROOMCONNECTIONS[data.get('chatID')]:
connection.write_message({'count': count,
'messagePersonName': self._name,
'messageType':
data.get("messageType"),
'message':
data.get('message'),
'image':
data.get('image')
})
if data.get("messageType") == "pause":
for connection in self._ROOMCONNECTIONS[data.get('chatID')]:
if connection != self:
connection.write_message(data)
if data.get("messageType") == "offer":
for connection in self._ROOMCONNECTIONS[data.get('chatID')]:
if connection._id == data.get("peerId"):
logger.info("{} offer to {}".format(
self._name, connection._name))
connection.write_message({'messageType': "offer",
'offer': data.get('offer'),
'peerId': self._id})
if data.get("messageType") == "response":
for connection in self._ROOMCONNECTIONS[data.get('chatID')]:
if connection._id == data.get("peerId"):
logger.info("{} response to {}".format(
self._name, connection._name))
connection.write_message({'messageType': "response",
'answer': data.get('answer'),
'peerId': self._id})
if data.get("messageType") == "negotiate":
for connection in self._ROOMCONNECTIONS[data.get('chatID')]:
if connection._id == data.get("peerId"):
logger.info("{} negotiate {} {}".format(
self._name, "offer to" if data.get("offer") else "answer to", connection._name))
if data.get("offer"):
connection.write_message({'messageType': "negotiate",
'offer': data.get('offer'),
'peerId': self._id})
if data.get("answer"):
connection.write_message({'messageType': "negotiate",
'answer': data.get('answer'),
'peerId': self._id})
if data.get("messageType") == "ice":
for connection in self._ROOMCONNECTIONS[data.get('chatID')]:
if connection._id == data.get("peerId"):
logger.info("{} ice with {}".format(
self._name, connection._name))
connection.write_message({'messageType': "ice",
'iceCandidate': data.get('iceCandidate'),
'peerId': self._id})
if data.get("messageType") == "requestMedia":
for connection in self._ROOMCONNECTIONS[data.get('chatID')]:
if connection._id == data.get("peerId"):
logger.info("{} media request with {}".format(
self._name, connection._name))
connection.write_message({'messageType': "requestMedia",
'peerId': self._id})
def on_close(self):
for chatID, connectionBlock in self._ROOMCONNECTIONS.items():
if self in connectionBlock:
self._ROOMCONNECTIONS[chatID].remove(self)
logger.info("{} left {}".format(
self._name, chatID))
count = len(self._ROOMCONNECTIONS[chatID])
people = [c._name for c in self._ROOMCONNECTIONS[chatID]]
for connection in self._ROOMCONNECTIONS[chatID]:
connection.write_message({'peerId': self._id,
'messageType': 'remove',
'people': people,
'count': count})
# ===============================================================================
# Chat- Main page to connect chat rooms
# ===============================================================================
class Chat(MainHandler):
def get(self):
self.render("sources/chatRoom2.html")