def broadcast(self, clients, msg): """ Optimized C{broadcast} implementation. Depending on type of the session, will json-encode message once and will call either C{send_message} or C{send_jsonifed}. @param clients: Clients iterable @param msg: Message to send """ json_msg = None count = 0 for c in clients: sess = c.session if not sess.is_closed: if sess.send_expects_json: if json_msg is None: json_msg = proto.json_encode(msg) sess.send_jsonified(json_msg, stats=False) else: sess.send_message(msg, stats=False) count += 1 self.stats.packSent(count)
def send_message(self, msg, stats=True): """ Send or queue outgoing message @param msg: Message to send @param stats: If set to True, will update statistics after operation completes """ self.send_jsonified(proto.json_encode(msg), stats)
def send_pack(self, message): # TODO: Just do escaping msg = '<script>\np(%s);\n</script>\r\n' % proto.json_encode(message) self.write(msg) self.flush() # Close connection based on amount of data transferred if self.should_finish(len(msg)): self._detach() self.safe_finish()
def get(self): self.preflight() self.disable_cache() self.set_header('Content-Type', 'application/json; charset=UTF-8') options = dict(websocket=self.server.websockets_enabled, cookie_needed=self.server.cookie_needed, origins=['*:*'], entropy=random.randint(0, sys.maxint)) self.write(json_encode(options))
def send_pack(self, message): # TODO: Just escape msg = '%s(%s);\r\n' % (self.callback, proto.json_encode(message)) self.set_header('Content-Type', 'application/javascript; charset=UTF-8') self.set_header('Content-Length', len(msg)) # FIXME self.set_header('Etag', 'dummy') self.write(msg) self._detach() self.safe_finish()
def _send_stats(self): data = proto.json_encode(BroadcastRouter.stats.dump()) self.sendMessage(data)