我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用tornado.websocket.WebSocketClosedError()。
def open(self): # ?????path self.file_path = self.get_argument('file_path', '') MonitorHandler.clients.append(self) thread = MyThread(target=file_monitor, args=('%s.log' % self.file_path, self)) MonitorHandler.threads.append(thread) self.stream.set_nodelay(True) try: for t in MonitorHandler.threads: if t.is_alive(): continue t.setDaemon(True) t.start() except WebSocketClosedError: client_index = MonitorHandler.clients.index(self) MonitorHandler.threads[client_index].stop() MonitorHandler.clients.remove(self) MonitorHandler.threads.remove(MonitorHandler.threads[client_index]) logger.debug("Websocket: Monitor client num: %s, thread num: %s" % (len(MonitorHandler.clients), len(MonitorHandler.threads)))
def _deliver(cls, message, upn="AUTHENTICATED", session=None): """ Writes the given *message* (string) to all users matching *upn* using the write_message() function. If *upn* is not provided or is "AUTHENTICATED", will send the *message* to all users. Alternatively a *session* ID may be specified instead of a *upn*. This is useful when more than one user shares a UPN (i.e. ANONYMOUS). """ #print 'deliver message',message logging.debug("_deliver(%s, upn=%s, session=%s)" % (message, upn, session)) for instance in cls.instances: try: # Only send to users that have authenticated user = instance.current_user except (WebSocketClosedError, AttributeError): continue if session and user and user.get('session', None) == session: instance.write_message(message) elif upn == "AUTHENTICATED": instance.write_message(message) elif user and upn == user.get('upn', None): instance.write_message(message)
def send_message(self, action, **data): """Sends the message to the connected client """ message = { "action": action, "data": data } try: self.write_message(json.dumps(message)) except WebSocketClosedError: logger.warning("WS_CLOSED", "Could Not send Message: " + json.dumps(message)) # Send Websocket Closed Error to Paired Opponent self.send_pair_message(action="pair-closed") self.close()
def run(self): try: super(MyThread, self).run() except WebSocketClosedError: pass
def send_to_the_client(self, data): try: self.write_message(data) except websocket.WebSocketClosedError as e: self.on_close()
def callback(self, channel, method, header, body): machine = method.routing_key.split(".")[0] data = json.loads(body.decode("utf-8")) data['machine'] = machine try: self.write_message(data) except websocket.WebSocketClosedError as e: self.on_close()
def ka(self): # print "%d: Keepalive: %s" % (time.time(), str(self)) try: self.write_message(self.KA) tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=2), self.ka) except websocket.WebSocketClosedError: pass
def broadcast(self, message, channel, sender): subscribers = self.get_subscribers(channel) for subscriber in subscribers: if subscriber != sender: try: subscriber.write_message(message) except WebSocketClosedError: self.remove_subscriber(channel, subscriber)