class MeasureSocketServer(WebSocketHandler): def open(self, name): src = Source.objects(name=name, user=self.current_user).first() self.measurement = src.name logging.info('open websocket for %s' % name) self.measure = Measure(name) self.authed = False def auth(self, message): authed_user = User.auth_via_token( message.get('user', None), message.get('token', None)) if authed_user: self.authed, self.user = True, authed_user return True @coroutine def on_message(self, message): message = json.loads(message) if not self.authed: if self.auth(message): self.write_message(json.dumps({ 'msg': 'auth_successed', 'error': 0 })) else: self.measure.publish(message) self.measure.write(message) def on_close(self): print("measurement::%s closed" % self.measurement)
class EntangleSocketServer(WebSocketHandler): def open(self, name): self.measure = Measure(name) self.subscribe(self.on_measure) def on_measure(self, data): self.write_message(data) def on_close(self): self.measure.unsubscribe()
def open(self, name): src = Source.objects(name=name, user=self.current_user).first() self.measurement = src.name logging.info('open websocket for %s' % name) self.measure = Measure(name) self.authed = False
def open(self, name): self.measure = Measure(name) self.subscribe(self.on_measure)