def _join(self): """ The initial connect message to the room. The client sends this after the websocket handshake has been established. """ pat = '^[a-zA-Z0-9_]*$' if not string_util.is_valid_string(self.nick, pattern=pat): raise InvalidNickNameError('nick name may only contain a-zA-Z0-9_') else: rtc_version = TinychatApi.rtc_version(self.room) if rtc_version is None: rtc_version = config.FALLBACK_RTC_VERSION log.debug('failed to parse rtc version, using fallback: %s' % rtc_version) payload = { 'tc': 'join', 'req': self._req, 'useragent': 'tinychat-client-webrtc-undefined_win32-' + rtc_version, 'token': self._connect_args['token'], 'room': self.room, 'nick': self.nick } self.send(payload)
def on_join(self, user): # P """ Received when a user joins the room. :param user: The user joining as User object. :type user: Users.User """ if user.account: tc_info = TinychatApi.user_info(user.account) if tc_info is not None: self.users.add_tc_info(user.handle, tc_info) if user.is_owner: self.console.write( 'Owner joined: %s:%s:%s' % (user.nick, user.handle, user.account), Color.B_BLUE) elif user.is_mod: self.console.write( 'Moderator joined: %s:%s:%s' % (user.nick, user.handle, user.account), Color.B_RED) elif user.account: self.console.write( 'User joined: %s:%s:%s' % (user.nick, user.handle, user.account), Color.B_GREEN) else: self.console.write( 'Guest joined: %s:%s' % (user.nick, user.handle), Color.B_YELLOW)
def connect(self): """ Connect to the websocket server. """ if not string_util.is_valid_string(self.room): raise InvalidRoomNameError( 'room name may only contain letters(a-z) and numbers(0-9).') else: tc_header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:66.0) Gecko/20100101 Firefox/66.0', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate, br', 'Sec-WebSocket-Protocol': 'tc', 'Sec-WebSocket-Extensions': 'permessage-deflate' } websocket.enableTrace(config.DEBUG_MODE) self._connect_args = TinychatApi.connect_token(self.room) if self._connect_args is not None: self._ws = websocket.WebSocketApp( self._connect_args['endpoint'], header=tc_header, on_open=self.on_open, on_message=self.on_message, on_error=self.on_error, on_pong=self.on_pong) self._ws.run_forever(origin='https://tinychat.com', ping_interval=20, ping_timeout=5) else: log.info('missing connect args %s' % self._connect_args)
def _add_tc_info(self): log.debug('adding tinychat info for user: %s' % self._user) tc_info = TinychatApi.user_info(self._user.account) if tc_info is not None: self._bot.users.add_tc_info(self._user.handle, tc_info)