這裏我的服務器:
var io = require('socket.io');
var socket = io.listen(8080, { /* options */ });
socket.set('log level', 1);
socket.on('connection', function(socket) {
console.log("connected");
socket.on('message1', function(data) {
socket.emit("message1",JSON.stringify({type:'type1',message: 'messageContent'}));
});
socket.on('disconnect', function() {
console.log("diconnected");
});
});
當我運行它生成此網址:https://xxx-c9-smartytwiti.c9.io,告訴我,我的代碼在此URL運行。
注意:xxx是我的工作區
我在客戶端做了什麼: 連接到 「https://xxx-c9-smartytwiti.c9.io:8080/」 ....
然後我得到的控制檯(火狐瀏覽器)這個錯誤:
cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://xxx-c9-smartytwiti.c9.io:8080/socket.io/1/?t=1406060495041. This can be fixed by moving the resource to the same domain or enabling CORS.
注意:當我主持我的本地服務器上它完美的作品。
似乎c9.io使用代理或防火牆,但我怎樣才能測試我的代碼寫在c9.io遠程?
UPDATE
據魯本的迴應,我已經改變了我的服務器,它工作時,我的socket.io客戶端託管在C9,但還是不能讓這個遠程客戶機上的工作(我還主持在我的FTP客戶端,但相同的結果):
// module dependencies
var http = require("http"),
sio = require("socket.io");
// create http server
var server = http.createServer().listen(process.env.PORT, process.env.IP),
// create socket server
io = sio.listen(server);
// set socket.io debugging
io.set('log level', 1);
io.set('origins', '*:*');
io.sockets.on('connection', function (socket) {
socket.emit('news', { message: 'Hello world!' });
socket.on('my other event', function (data) {
console.log(data.message);
});
});
看起來起源的配置已被忽略,我也沒有把握C9.io ..
建議?
乾杯。
感謝Ruben,現在我的客戶端代碼在c9中工作,但仍然在遠程訪問中出現「跨源請求被阻止」,請參閱我的更新 –