Hi agent178, thanks for your reply. How can this be? Playcanvas is providing a step by step tutorial doing this and the example works exact the same way. There must be another way to solve this. Do I have to allow my glich app to allow requests from playcanvas?.
You need to whitelist the playcanv.as domain name on your glitch.me project, or use a cors extension on your browser while developing as @agent178 said.
Ok, one last try because I have no idea. Regarding the tutorial: What do I have to add in glitch at the server.js to make it work? I’ve added the cors package to my glitch project.
var server = require('http').createServer();
var io = require('socket.io')(server);
io.sockets.on('connection', function(socket) {
console.log("Client has connected!");
});
console.log ('Server started.');
server.listen(3000);
I think the node.js cors module has to be used with express.js (the app instance you see on the code above). That’s a node.js middleware that makes it easier to setup routes/pages to your node.js server.
You can find a tutorial online most likely on how to use nodejs + express + sockets. Sorry for not being of more help!
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.sockets.on("connection",function(socket){
console.log("connection");
});
i almost forgot but you need for it to listen on a port also
Ok, but if I bring this together it has no effect on the communication. It still says that Origin https://playcanv.as is not allowed by Access-Control-Allow-Origin.
I ran into this same problem. This is actually a new problem with the latest socket.io version, I’m not sure why it suddenly requires this CORS work-around, but this is what the top of my server.js looks like on glitch to circumvent the problem:
var server = require('http').createServer();
var options={
cors:true,
origins:["http://127.0.0.1:5347"],
}
var io = require('socket.io')(server, options);
Hi @ChandlerTwins,
that’s it! Thanks for helping To sum it up:
Go to package.json of your glitch server and install CORS package
Go to the server.js of your glitch server and replace the first two lines:
var server = require('http').createServer();
var io = require('socket.io')(server);
with the code ChandlerTwins posted:
var server = require('http').createServer();
var options={
cors:true,
origins:["http://127.0.0.1:5347"],
}
var io = require('socket.io')(server, options);
The funny thing is, for me it doesn’t matter what I put into orgins. It works even if its empty. It seems that it is enough to set cors: true
Good to know! I found the answer on some obscure stackoverflow question when I was working on my fighting game, so it’s good to have this information in a more reachable place hahaha