2015-02-07 112 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); 
    }); 
});