2015-02-07 128 views
0

我知道這是一個非常討論的話題,但我似乎仍然無法使其探索互聯網上的每個解決方案。我app.js代碼如下所示:Node.js + Express + socket.io - socket.io服務不正常

var express = require('express'); 
var io = require('socket.io'); 

var app = express(); 
app.use(express.static(__dirname + '/public')); 

// view engine setup 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'jade'); 

// uncomment after placing your favicon in /public 
//app.use(favicon(__dirname + '/public/favicon.ico')); 
app.use(logger('dev')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 


/***************************************/ 
/*********** START SERVER **************/ 
/***************************************/ 
var server = app.listen(3000, '0.0.0.0', function() { 
    console.log('Listening on port %d', server.address().port); 
}); 
io = io.listen(server); 

/***************************************/ 
/*********** COMMUNICATIONS ************/ 
/***************************************/ 
io.sockets.on('connection', function(socket){..... //continues 

index.jade文件的相關部分:

script(type='text/javascript' src='/socket.io/socket.io.js') 
script(type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js') 
script(type='text/javascript' src='/javascripts/index.js') 

和我index.js看起來是這樣的:

//init the connection with the server 
var socket = io.connect('/'); 

socket.on('message', function(message){ 
    //parse message 
    message = JSON.parse(message); 
    alert(message); 
}); 

$(function(){ 
    var data = {message: 'test test test'}; 
    socket.send(JSON.stringify(data)); 
}); 

尋找Chrome開發者控制檯,我收到以下錯誤:

socket.io.js:2935 GET http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347369182-0 
arielschon12.koding.io/:1 XMLHttpRequest cannot load http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347369182-0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://arielschon12.koding.io:3000' is therefore not allowed access. The response had HTTP status code 404. 
socket.io.js:2935 GET http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347370898-1 
arielschon12.koding.io/:1 XMLHttpRequest cannot load http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347370898-1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://arielschon12.koding.io:3000' is therefore not allowed access. The response had HTTP status code 404. 
socket.io.js:2935 GET http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347372842-2 
arielschon12.koding.io/:1 XMLHttpRequest cannot load http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347372842-2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://arielschon12.koding.io:3000' is therefore not allowed access. The response had HTTP status code 404. 

同樣類型的更多錯誤每隔幾秒就會彈出一次。我不知道該怎麼做。任何幫助感謝!

+0

你需要的只是內部記錄它的HTTP響應來代替。因爲它來自不同的域,請確保您添加了正確的CORS標頭。 – dandavis 2015-02-07 23:13:31

回答

1

在客戶端,你需要:

var socket =io(); 
socket.on('connect',function(){}); 

和服務器端:

var app = require('express')(); 
var server = require('http').Server(app); 
var io = require('socket.io')(server); 

server.listen(80); 

io.on('connection', function (socket) { 
    socket.emit('news', { hello: 'world' }); 
    socket.on('my other event', function (data) { 
    console.log(data); 
    }); 
});