socket.io and Access-Control-Allow-Headers issue

Feb 6, 08:53 AM

problem – we keep getting this error message:

XMLHttpRequest cannot load http://foo.bar:7076/
No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
Origin ‘http://foo.bar ‘ is therefore not allowed access.

express.io solution:
written from this


app = require(‘express.io’)()
app.http().io()

// Setup the ready route, and emit talk event.
app.io.route(‘ready’, function(req) { req.io.emit(‘talk’, { message: ‘io event from an io route on the server’ })
})

// Send the client html.
app.get(’/’, function(req, res) { res.header(“Access-Control-Allow-Origin”, “*”); res.header(“Access-Control-Allow-Headers”, “X-Requested-With”); res.sendfile(__dirname + ‘/client.html’)
})

app.listen(7076);

using the “origins” flag and not using express:

server: — notice the port number is 80 !
var io = require(‘socket.io’).listen(12345, {origins: ‘foo.bar:80’}) ;
// notice the port number is 80 ! ! ! !

var simple = io .sockets .on(‘connection’, function(socket) { console.log(‘connected! ‘ ); //socket.on(‘message’, function(data) { //console.log(‘sending ‘ + data ); //}); });

client:
var socket = io.connect(‘hillary2016.rocks:12345’);
$(document).ready(function() { socket.on(‘connect’, function() { alert(‘connected!’); });
});

Mark Edwards

,

---

Commenting is closed for this article.

---