2017-09-20 19 views
1

你好我正在使用TornadoDjango frammwork在後端處理郵件在前端的消息。我不太喜歡我的項目,但是我的項目中有一個文件,其中的類實現了websocket.WebSocketHandlerweb.Application。這是該文件如何發送消息到web託管與Tornado

class Application(web.Application): 
    """ 
    Main Class for this application holding everything together. 
    """ 
    def __init__(self): 
     PROJECT_NAME = os.path.basename(os.getcwd()) 
     os.environ['DJANGO_SETTINGS_MODULE'] = PROJECT_NAME + 'tutorial/settings' 

     # Handlers defining the url routing. 
     handlers = [ 
      ('/room', SocketHandler), 
      ('/room/([a-zA-Z0-9|=%]*)$', SocketHandler), 
      ('/video_call', VideoCallSocketHandler), 
      ('/video_call/([a-zA-Z0-9|=%]*)$', VideoCallSocketHandler), 
     ] 

在前端部分的一部分,是一個功能:

var ws = new WebSocket('wss://domain.com:9003/video_call/' + conferenceId); 

ws.onmessage = function (ev) { 
    window.location.replace(redirectUrl); 
}; 

我相信,從我們的移動aplication該功能獲取的信息。

所以問題是,我想發送消息到這個URL或任何它從我的python視圖'wss://domain.com:9003/video_call/' + conferenceId

例如:

def some_view_function(request, **kwargs): 
    conference_id = request.GET.data['conferenceId'] 
    ... 
    if something: 
     send message to wss://domain.com:9003/video_call/' + conference_id 

我該怎麼辦呢?

回答

0

那麼由龍捲風包裹django代碼呢?

def main(): 
    os.environ['DJANGO_SETTINGS_MODULE'] = 'docs.settings' 
    application = get_wsgi_application() 
    container = tornado.wsgi.WSGIContainer(application) 

    tornado_app = tornado.web.Application(
     [('.*', tornado.web.FallbackHandler, dict(fallback=container))],   
    ) 

    server = tornado.httpserver.HTTPServer(tornado_app) 
    server.bind(8888) 
    server.start(0) 
    tornado.ioloop.IOLoop.instance().start() 

django和龍捲風之間的溝通我建議通過隊列進行溝通,如Redis。

def some_view_function(request, **kwargs): 
    conference_id = request.GET.data['conferenceId'] 
    ... 
    if something: 
     send_to_queue() 

龍捲風側隊列監聽器。

+0

如何在'send_to_queue'中定義'conference_id'。函數如何知道'WebSocket'發送消息? – Przemek