[更新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(「起源= *」),但這似乎不是在這種情況下,工作也是如此。
我嘗試在Nodejisui上託管socket.io,並將我的JavaScript on rails應用程序指向xxx.jit.su var socket = io.connect('http://xxxxx.jit.su');但仍然有相同的問題 – WYH 2013-02-14 12:23:40
它似乎與託管有關的域名。即本地主機上的[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
此處提供了類似的問題: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