This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·170 lines (140 loc) · 4.61 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
import json
import requests
import websocket
class TinychatClient(object):
def __init__(self, room, nickname, account=None, password=None):
self._room = room
self._nickname = nickname
self._account = account
self._password = password
self._ws = None
self._req = 1
self._users = {}
def connect_room(self):
"""
Connect to a room.
"""
self._req = 1
# This packet's structure MUST not be changed.
self.send_msg({
'tc': 'join',
'useragent': 'tinychat-client-webrtc-chrome_win32-2.0.9-255',
'token': self.get_token(),
'room': self._room,
'nick': self._nickname
})
def connect_socket(self):
"""
Connect to the Tinychat websocket.
"""
self._ws = websocket.WebSocket()
header = [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3040.0 Safari/537.36',
'Accept-Language: en-US,en;q=0.8',
'Accept-Encoding: gzip, deflate, sdch, br',
'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits'
]
# This connect function's arguments MUST not be changed.
self._ws.connect(
"wss://wss.tinychat.com",
header=header,
host="wss.tinychat.com",
origin="https://tinychat.com",
subprotocols=["tc"]
)
def get_token(self):
"""
Request a token from a Tinychat room.
:return: Room token.
:rtype: str
"""
print('Making token request.')
r = requests.get('https://tinychat.com/api/v1.0/room/token/{0}'.format(self._room))
result = r.json()
token = result['result']
return token
def mainloop(self):
"""
Main receive/respond loop.
"""
while True:
msg = json.loads(self._ws.next())
self.on_msg(msg)
def on_join(self, msg):
"""
Handles join messages from the server.
:param msg: The message object.
:type msg: dict
"""
user_info = msg.copy()
del user_info['tc']
user_handle = user_info.pop('handle', None)
self._users[user_handle] = user_info
print([self._users[handle]['nick'] for handle in self._users])
def on_msg(self, msg):
"""
Main message handler.
:param msg: The message object.
:type msg: dict
"""
print(msg)
msg_type = msg['tc']
if msg_type == 'join':
self.on_join(msg)
elif msg_type == 'nick':
self.on_nick(msg)
elif msg_type == 'ping':
self.on_ping()
elif msg_type == 'quit':
self.on_quit(msg)
elif msg_type == 'userlist':
self.on_userlist(msg)
def on_nick(self, msg):
"""
Handles nick messages from the server.
:param msg: The message object.
:type msg: dict
"""
user_handle = msg['handle']
new_nick = msg['nick']
self._users[user_handle]['nick'] = new_nick
print([self._users[handle]['nick'] for handle in self._users])
def on_ping(self):
self.send_msg({'tc': 'pong'})
def on_quit(self, msg):
"""
Handles quit messages from the server.
:param msg: The message object.
:type msg: dict
"""
user_handle = msg['handle']
del self._users[user_handle]
print([self._users[handle]['nick'] for handle in self._users])
def on_userlist(self, msg):
"""
Handles userlist messages from the server.
:param msg: The message object.
:type msg: dict
"""
for user in msg['users']:
user_info = user.copy()
user_handle = user_info.pop('handle', None)
self._users[user_handle] = user_info
print([self._users[handle]['nick'] for handle in self._users])
def send_msg(self, msg):
"""
Sends a json message to the Tinychat server.
:param msg: The object to send. Must be serializable to json.
:type msg: dict | object
"""
msg['req'] = self._req
self._ws.send(json.dumps(msg))
self._req += 1
if __name__ == "__main__":
room = input("What room? ")
nickname = input("What is your nickname? ")
client = TinychatClient(room, nickname)
client.connect_socket()
client.connect_room()
client.mainloop()