如何在websocket中通过python Tornado发送json标头? [英] how to send json header via python tornado in a websocket?

查看:28
本文介绍了如何在websocket中通过python Tornado发送json标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!C:/Python27/python.exe -u 
import tornado
import tornado.websocket
import tornado.wsgi
import tornado.web
import tornado.httpserver
import tornado.tcpserver
import json

from py2neo import neo4j, cypher
graph_db = neo4j.GraphDatabaseService()
class request(tornado.web.RequestHandler):
    def  sr():
        self.set_header('Content-type','application/json')
class ChatWebSocket(tornado.websocket.WebSocketHandler):
    clients = []
    def open(self):
        ChatWebSocket.clients.append(self)
    def on_message(self, message):
         query = "START a=node:node_auto_index(uid='twitter') CREATE a-[:ch]-(t{con:'"+message+"'}) RETURN a"
     data, metadata = cypher.execute(graph_db, query)
         for client in ChatWebSocket.clients:
         print(client)
         t=json.loads(message)
         client.write_message('Content-type:application/json\n') 
         client.write_message(json.dumps({'a':t['b']}))
         print(t['b'])
    def on_close(self):
         ChatWebSocket.clients.remove(self)
    tornado_app = tornado.web.Application([
    (r'/websocket', ChatWebSocket),
    (r'.*', tornado.web.FallbackHandler)
    ])
 tornado_app.listen(5094)
tornado.ioloop.IOLoop.instance().start()

这是我的代码示例,我在获取其实例 b 的消息中接受 json并将 json 再次发送到客户端.但客户端将标头视为普通字符串.

here is my code sample i am accepting json in the message getting its instance b and send json again to the client.but client is treating header as normal string.

推荐答案

Websockets 不需要标头.只需将您的 json 作为字符串发送即可.

Websockets does not require headers. Just send your json as string.

这篇关于如何在websocket中通过python Tornado发送json标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆