tornadoapp.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:django-dialogs 作者: ElusiveSpirit 项目源码 文件源码
def on_message(self, message):
        data = json.loads(message)
        if data['type'] == 'open_dialog':
            if not Thread.objects.filter(
                id=data['thread_id'],
                participants__id=self.user_id
            ).exists():
                self.close()
                return
            self.thread_id = data['thread_id']
            return
        elif data['type'] == 'message':
            url = settings.SEND_MESSAGE_API_URL
            body = urlencode({
                "message_text": data['text'].encode("utf-8"),
                "api_key": settings.API_KEY,
                "sender_id": self.user_id,
                "thread_id": self.thread_id,
            })
        elif data['type'] == 'message_status':
            url = settings.UPDATE_MESSAGE_STATUS_API_URL
            body = urlencode({
                "api_key": settings.API_KEY,
                "sender_id": self.user_id,
                "thread_id": self.thread_id,
            })
        elif data['type'] == 'person_status':
            pub_client.publish("thread_{}_messages".format(self.thread_id), json.dumps({
                "type": "person_status",
                "thread_id": self.thread_id,
                "user_id": self.user_id,
                "username": self.username,
                "typing": data['typing'],
            }))
            return
        else:
            return

        http_client = tornado.httpclient.AsyncHTTPClient()
        request = tornado.httpclient.HTTPRequest(
            url,
            method="POST",
            body=body
        )
        http_client.fetch(request, self.handle_request)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号