简体   繁体   中英

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

I know this is a very discussed topic, yet I still can't seem to make it work exploring every solution on the internet. My app.js code looks like this:

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

The relevant part of the index.jade file:

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')

and my index.js looks like this:

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

Looking in the chrome developer console, I get the following errors:

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.

And more errors of the same type keep on popping up every few seconds.. I have no idea what to do. Any help appreciated!

In the client side you need:

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

And server side:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM