2016-12-05 53 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。