#!/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が開くとき、クライアントは、pythonがonmessageを受信する彼のuniqueidを送信し、{uniqueid:websocketobject}でdictを作成します。誰かがメッセージを送信すると、一意のIDが送信されます。 uniqueid} その後、適切なクライアントに送信しますが、websocket オブジェクトをその中に保存したとしても、機能せず、タイプ属性がないと言うことがあります。