2016-12-05 7 views
0

私はセロリー(解析ページ)で実行するいくつかの定期的なタスクを持っています。 また、私は竜巻とwebsocketを設立しました。セロリから竜巻にデータを送信するwebsocket

定期的なタスクから竜巻にデータを渡し、このデータをwebsocketに書き込み、このデータをHTMLページで使用したいとします。

どうすればいいですか?

私はモジュールからトルネードwebsocketをセロリのタスクでインポートしようとしましたが、それはうまくいかなかったのです。

私はクライアント側からメッセージを受け取った場合、データを返す方法しか知りません。ここで私はそれに対処する方法です:

import tornado.httpserver 
import tornado.websocket 
import tornado.ioloop 
import tornado.web 
import socket 
''' 
This is a simple Websocket Echo server that uses the Tornado websocket handler. 
Please run `pip install tornado` with python of version 2.7.9 or greater to install tornado. 
This program will echo back the reverse of whatever it recieves. 
Messages are output to the terminal for debuggin purposes. 
''' 
class handler(): 
    wss = [] 



class WSHandler(tornado.websocket.WebSocketHandler): 
    def open(self): 
     print ('new connection') 
     if self not in handler.wss: 
      handler.wss.append(self) 

    def on_message(self, message): 
     print ('message received: ' + message) 
     wssend('Ihaaaa') 

    def on_close(self): 
     print ('connection closed') 
     if self in handler.wss: 
      handler.wss.remove(self) 

    def check_origin(self, origin): 
     return True 


def wssend(message): 
    print(handler.wss) 
    for ws in handler.wss: 
     if not ws.ws_connection.stream.socket: 
      print ("Web socket does not exist anymore!!!") 
      handler.wss.remove(ws) 
     else: 
      print('I am trying!') 
      ws.write_message(message) 
      print('tried') 

application = tornado.web.Application([ 
    (r'/ws', WSHandler), 
]) 


if __name__ == "__main__": 
    http_server = tornado.httpserver.HTTPServer(application) 
    http_server.listen(8888) 
    myIP = socket.gethostbyname(socket.gethostname()) 
    print ('*** Websocket Server Started at %s***' % myIP) 
    main_loop = tornado.ioloop.IOLoop.instance() 
    main_loop.start() 

答えて

0

オプションは、竜巻でハンドルを作成し、このハンドルにセロリのタスクの結果を掲示することです。 その後、このデータをwebsocketに渡す機会があります。

関連する問題