When I try to use socket.io it does not work. It says this in the console:
When I go to that url it brings me to a screen that says this:
There must be something I am not doing right, because your socket.io example works.
Here is my code:
var Network = pc.createScript('network');
Network.id = null;
Network.socket = null;
// initialize code called once per entity
Network.prototype.initialize = function() {
var socket = io.connect("https://fearless-ember-journey.glitch.me/");
Network.socket = socket;
};
That isn’t an error but a warning for missing the source map file for the socket.io library. That isn’t required to use the socket.io library, your code seems correct. Try sending/receiving a message to see if the connection is working.
Hi @HTML_Celeb, in case it helps, this is how I’m doing it on my project on both client and server side. It uses an HTTPS (and hence wss) connection, so it requires a little bit more configuration (credentials, cors):
/* Server side */
var cors = require('cors')
const express = require('express')()
express.use(cors())
var httpsServer = require('https').createServer(credentials, express)
// Server with CORS enabled and WS protocol for transport only
var wsServer = require('socket.io')(httpsServer, {
cors: {
origin: "*",
methods: ["GET", "POST"]
},
transports: ['websocket']
})
wsServer.sockets.on('connection', (socket) => {
console.log("Client has connected!")
socket.emit('customEvent', 'hello')
})
[...]
// Make sure your HTTP server is listening on the right port and address
httpsServer.listen(3000, '0.0.0.0')
And then in the client side (make sure you’ve got your socket.io javascript file included in your PlayCanvas project so it can load properly):
/* Client side (e.g your network.js script) */
var socket = io.connect('wss://<your_server_endpoint>:3000', { transports: ['websocket'] });
socket.on('customEvent', function(data){
console.log(data) // should print 'hello'
})