2013-02-14 128 views
0

[更新1] 我發現io.connect('http://xxxxx.herokuapp.com')實際發出的請求xxxxx.herokuapp.com端口3000: 的連接工作,如果該請求是沒有端口號。我沒有在我的io.connect中指定一個端口,我該如何擺脫它?連接到從一個Rails服務器Socket.io服務器

Request URL:http://xxxxx.herokuapp.com:3000/socket.io/1/?t=1360853265439 
    Request Headersview source 
    Cache-Control:max-age=0 
    Origin:http://localhost:3000 
    Referer:http://localhost:3000/about 
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like  Gecko) Chrome/24.0.1312.57 Safari/537.17 
    Query String Parametersview sourceview URL encoded 
    t:1360853265439 

[更新2] 這裏比較是成功的連接的HEAD當我運行從本地文件系統

Request URL:http://xxxxx.herokuapp.com/socket.io/1/?t=1360854705943 
Request Method:GET 
Status Code:200 OK 
Request Headersview source 
Accept:*/* 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Cache-Control:max-age=0 
Connection:keep-alive 
Host :xxxxx.herokuapp.com 
Origin:null 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like  Gecko) Chrome/24.0.1312.57 Safari/537.17 

[更新3] 組端口的Java腳本到80似乎在工作

var socket = io.connect('http://xxxxx.herokuapp.com',{port:80}); 

[Question]我打算在一臺主機上設置socket.io,與另一臺主機上的Rails應用進行通信。當客戶端Javascript託管在localhost:3000上以連接到託管在localhost:5000上的node.js服務器上的Socket.io時,它一切正常。當同一端口上的客戶端JavaScript和Socket.io託管在Heroku上時,它也可以工作。

socket_server.js託管在XXXX.herokuapp.com

   var app = require('http').createServer(handler) 
       , io = require('socket.io').listen(app) 
       , fs = require('fs') 
       , i = 0 


      io.configure(function() { 
       io.set("origin = *"); 
       io.set("transports", ["xhr-polling"]); 
       io.set("polling duration", 100); 
      }); 

      var port = process.env.PORT || 5000; // Use the port that Heroku provides or default to 5000 
      app.listen(port, function() { 
       console.log(">>>>>>socket server up and running on port: "+port); 
      }); 


      function handler (req, res) { 
       fs.readFile(__dirname + '/socket.html', 
       function (err, data) { 
       if (err) { 
        res.writeHead(500); 
        return res.end('Error loading socket.html'); 
       } 

       res.writeHead(200); 
       res.end(data); 
       }); 
      } 

      io.sockets.on('connection', function (socket) { 
       console.log(">>>>>>client connected through socket"); 
       socket.emit('news', '>>>>>>server say hello to client', i); 
       console.log('>>>>>>server say hello to client' +'['+i+']') 
       socket.on('my other event', function (data) { 
       socket.emit('news', i); 
       i++; 
       console.log(data +'['+i+']'); 
       }); 
      }); 

如果我把客戶端的JavaScript在XXXX.herokuapp.com相同socket.html作爲socket.io它像預期的那樣。

    <div id='sandbox'>sandbox</div> 

     <script src="/socket.io/socket.io.js"></script> 

     <script> 

      var socket = io.connect('window.location.hostname'); 
      $('#sandbox').append('<div> lora </div>'); 
      socket.on('news', function (data, index) { 
      $('#sandbox').append('<div>' + data + ' ' + index + '</div>'); 
      console.log(data); 
      socket.emit('my other event', { my: 'data' }); 
      }); 
     </script> 

但是如果我把客戶端的JavaScript上YYYY.herokuapp.com Rails的服務器上,並嘗試連接到xxx.herokuapp.com Socket.io服務器,它不工作。它設法檢索服務器上的socket.io.js,但是io.connect('http://xxxxx.herokuapp.com')沒有得到來自服務器的任何響應。

    <div id='sandbox'>sandbox</div> 

     <script src="http://xxxxx.herokuapp.com/socket.io/socket.io.js"></script> 

     <script> 

      var socket = io.connect('http://xxxxx.herokuapp.com'); 
      $('#sandbox').append('<div> lora </div>'); 
      socket.on('news', function (data, index) { 
      $('#sandbox').append('<div>' + data + ' ' + index + '</div>'); 
      console.log(data); 
      socket.emit('my other event', { my: 'data' }); 
      }); 
     </script> 

我讀了幾後點解決方案來io.set(「起源= *」),但這似乎不是在這種情況下,工作也是如此。

回答

0

默認情況下,socket.io中的起源設置爲*。這將是io.set("origin", "*");,但由於這是默認值,因此您可以忽略此功能。

我注意到,你的客戶端代碼中也有var socket = io.connect('window.location.hostname'),你可能會想:var socket = io.connect(window.location.hostname)沒有單引號。 heroku文檔建議使用io.set("polling duration", 100);但您將其設置爲100,而不是建議的10。否則,我沒有看到您的代碼有任何問題,它應該可以正常工作。

如果這些修復程序沒有幫助,那麼我會責怪你的託管。在部署實時應用程序時,Heroku是一個非常嚴格的平臺。所以可能是他們在這裏搞亂了一些東西。我會建議嘗試在nodejitsu.com上部署其他地方,它也支持WebSockets,並且不會對輪詢進行任何限制,以查看它是否適用於您。

+0

我嘗試在Nodejisui上託管socket.io,並將我的JavaScript on rails應用程序指向xxx.jit.su var socket = io.connect('http://xxxxx.jit.su');但仍然有相同的問題 – WYH 2013-02-14 12:23:40

+0

它似乎與託管有關的域名。即本地主機上的[working] client js:3000 <==> localhost:5000上的socket.io。 [working] xxx.herokuapp.com上的客戶端js <==> xxx.herokuapp.com上的socket.io [細分]本地主機上的客戶端js:4000 <==> xxx.herokuapp.com上的socket.io – WYH 2013-02-14 12:59:13

+0

此處提供了類似的問題:http ://stackoverflow.com/questions/12439852/bridging-a-simple-node-js-socket-io-chat-app-with-a-rails-app-on-heroku/14875456#14875456 – WYH 2013-02-14 13:02:49