Node.js + 快递 + socket.io - socket.io 服务不正常

Node.js + Express + socket.io - socket.io not serving properly

我知道这是一个讨论非常多的话题,但我似乎仍然无法探索互联网上的每个解决方案。我的 app.js 代码如下所示:

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.

并且每隔几秒就会不断弹出更多相同类型的错误。我不知道该怎么办。任何帮助表示赞赏!

在客户端你需要:

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);
  });
});