2
#!/usr/bin/python27
import tornado  
import tornado.websocket
import tornado.wsgi
import motor
import json
import jsonpickle
import uuid
from py2neo import neo4j, cypher
from tornado.ioloop import IOLoop

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        db = self.settings['db'] 
Class ChatWebSocket(tornado.websocket.WebSocketHandler):
    ids={}
    frd={}
    def my_callback(result, error):
        print 'result', repr(result)
        IOLoop.instance().stop()
    def on_message(self, message):
        def get_current_user(self):
           print(self)
        to=db.today
       data=json.loads(message)
       print(data)
       s=str(data['who'])            
       if (data['st']=='me'):



        if ''+s+'' in ChatWebSocket.ids:
            del ChatWebSocket.ids[''+s+'']
            ChatWebSocket.ids[''+s+'']=self
            print("sf")
        else:
            ChatWebSocket.ids[''+s+'']=self
            print(ChatWebSocket.ids)
    elif (data['st']=='st'):
        graph_db = neo4j.GraphDatabaseService()
        query = "START a=node:node_auto_index(uid='"+data['who']+"')  MATCH a-[:frd]-q RETURN q.uid as id"
        data1, metadata = cypher.execute(graph_db, query)
        print(data1)
        print(data)
        for y in data1:
            for j in y:
                if ''+str(j)+'' in ChatWebSocket.ids:
                    ChatWebSocket.ids[''+str(j)+''].write_message({"s":"st","id":data['id'],"con":data['con'],"time":data['time'],"on":data['on']})         
                else:
                    print("shit")
    elif(data['st']=='com'):
        graph_db = neo4j.GraphDatabaseService()
        query = "START a=node:node_auto_index(uid='"+data['who']+"')  MATCH a-[:frd]-q RETURN q.uid as id"
        data1, metadata = cypher.execute(graph_db, query)
        print(data1)
        print(data)
        for y in data1:
            for j in y:
                if ''+str(j)+'' in ChatWebSocket.ids:
                    ChatWebSocket.ids[''+str(j)+''].write_message({"con":data['con'],"id":data['id'],"s":"com"})         
                else:
                    print("shit")
    elif(data['st']=='cl'):
        print("cl")         
        if ''+s+'' in ChatWebSocket.ids: 
            del ChatWebSocket.ids[''+s+'']
            print(ChatWebSocket.ids)
            print("closed")                      
    elif(data['st']=='ty'): 

        s=str(data['whom'])

        if ChatWebSocket.ids[''+s+''].write_message({"g":str(data['who']),"s":"ty"}):
            print("ok")
    else: 
        doc={'who':data['who'],'whom':data['whom'],'mes':data['mes'],'id':data['who']+data['whom']}
        to.insert(doc)
        s=str(data['whom'])

        if ChatWebSocket.ids[''+s+''].write_message({"g":str(data['who']),"mes":data['mes'],"s":"m"}):
            print("ok")

    db=motor.MotorClient().open_sync().chat tornado_app = tornado.web.Application([(r'/websocket', ChatWebSocket),
    (r'.*', tornado.web.FallbackHandler), (r'/', MainHandler)],db=db)

   tornado_app.listen(5528)
   tornado.ioloop.IOLoop.instance().start()

这就是我正在做的事情:当websocket打开时,客户端发送他的uniqueid,python接收onmessage并使用{uniqueid:websocketobject}创建一个dict当有人发送消息时它发送唯一id python获取websocket对象为websocket object = dict{ uniqueid} 然后将其发送给适当的客户端,但有时它不起作用并且说没有类型属性,即使我已经在其中保存了 websocket 对象。

4

0 回答 0