2013-05-30 63 views
2

最近我一直在試圖建立我使用這個插件一個Ngnix服務器上的應用程序彗星服務器:https://github.com/wandenberg/nginx-push-stream-modulenginx的彗星長輪詢與jQuery

由於GNU/GPL的約束性,我無法使用隨插件提供的JS,所以我試圖使用jquery ajax請求自己實現它。

我的Nginx的配置是這樣的:

location /channels-stats { 
     # activate channels statistics mode for this location 
     push_stream_channels_statistics; 

     add_header 'Access-Control-Allow-Origin' '*'; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 

     # query string based channel id 
     set $push_stream_channel_id    $arg_id; 
    } 

    location ~ /pub/(.*) { 
     # activate publisher (admin) mode for this location 
     push_stream_publisher admin; 

     add_header 'Access-Control-Allow-Origin' '*'; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 

     # query string based channel id 
     set $push_stream_channel_id    $1; 
    } 

    location ~ /sub/(.*) { 
     # activate subscriber (streaming) mode for this location 
     push_stream_subscriber; 

     add_header 'Access-Control-Allow-Origin' '*'; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 


     # positional channel path 
     set $push_stream_channels_path    $1; 
    } 

和代碼段,我試圖用(螢火蟲)來測試這兩種方式通信:

//reciever 
$.ajax({ 
    url:'sub/test?callback=mycallback', 
    dataType:'json', 
    success:function(data){ 
      console.log(data); 
    } 
}); 

//sender 
$.ajax({ 
    url:'pub/test', 
    dataType:'json', 
    type:'POST', 
    data:'mycallback({"J":5,"0":"N"})' 

}); 

我想使其工作跨域,但我無法設法讓它工作,即使在同一個域上,發生什麼acctualy是:

當我使用接收代碼,它啓動連接到s並且正在無休止地加載,因此我嘗試用發送者代碼來響應長輪詢。

現在在.NET選項卡(firebug)的控制檯上我可以看到,一旦我發送POST,它會以純文本的形式在響應中收到它,但仍然保持連接而不會回撥!所以如果我反覆發送帖子,我可以看到他們正在收集的網絡響應標籤中的螢火蟲,但沒有從接收器功能給出回調!因此我無法提取數據!

現在我試着這兩個在另一個域和相同的原產地域,所以我認爲這個政策不是問題在這裏,什麼問題是,jQuery的片段沒有得到回調,雖然它確實達到回調一次請求超時! 請幫忙。

在旁邊註釋,如果你認爲有一個替代插件NGINX這會更適合我請讓我知道。

回答

2

確定後一些研究,我設法弄清楚,我在我的nginx的配置錯誤這使得連接無止境的,我把它改爲:

location /channels-stats { 
     # activate channels statistics mode for this location 
     push_stream_channels_statistics; 

     add_header 'Access-Control-Allow-Origin' '*'; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 

     # query string based channel id 
     set $push_stream_channel_id    $arg_id; 
    } 

    location ~ /pub/(.*) { 
     # activate publisher (admin) mode for this location 
     push_stream_publisher admin; 

     add_header 'Access-Control-Allow-Origin' '*'; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 

     # query string based channel id 
     set $push_stream_channel_id    $1; 
    } 

    location ~ /sub/(.*) { 
     # activate subscriber (streaming) mode for this location 
     push_stream_subscriber long-polling; //<----------------------EDITED LINE 

     add_header 'Access-Control-Allow-Origin' '*'; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 


     # positional channel path 
     set $push_stream_channels_path    $1; 
    } 

,我結束了兩個小功能,雙雙向通信

var datalink={ 
     listen:function(success,error,complete,url){ 
     //reciever 
      $.ajax({ 
       url:url, 
       dataType:'jsonp', 
       success:success, 
       error:error, 
       complete:complete 
      }); 
     }, 
     send:function(data,success,error,complete,url){ 
     //sender 
      $.ajax({ 
       url:url, 
       dataType:'json', 
       success:success, 
       error:error, 
       complete:complete 
       type:'POST', 
       data:JSON.stringify(data) 
      }); 
     } 

    }; 

注:用於發送數據的功能,使用方法JSON.stringify(你的對象) 而大多數瀏覽器都支持它,它建議使用這樣的事情: http://bestiejs.github.io/json3/ ,以便爲json.stringfying添加對舊瀏覽器的支持。

使用示例:

從服務器偵聽(訂閱):

datalink.listen(function(data){ 

        console.log(data);//<---your recieved object 
           },undefined,undefined,'http://example.com/sub/foo'); 

發送到服務器(發佈):

datalink.send({x:5}//<--the object you are about to send 
       ,undefined,undefined,undefined,'http://chessbless.com/pub/test'); 

所以這是我的解決方案,我希望你能找到這很有幫助,我道歉,如果有什麼不清楚,第一次回答SO。