示例#1
0
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)
示例#2
0
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()
示例#3
0
 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
示例#4
0
 def open(self, name):
     self.measure = Measure(name)
     self.subscribe(self.on_measure)