简体   繁体   中英

How to connect to a nodejs socket server?

I am new to node JS and I am trying to create a real time application that consists of a node JS server with socket.io and a unity application that can connect to it

I created the server with the sockets in the below code:

const express = require('express');

const http = require('http');

const app = express();
const port = process.env.PORT || 9000;
const server = http.Server(app);
const io = require('socket.io')(server);
io.on('connection',(socket) => {
    console.log('A user connected');
    socket.on('disconnect', () => {
        console.log('user disconnected');
      });
})
app.get('/',(req,res) =>{
   res.json({worked : 'worked'});
});

server.listen(port,() => console.log(`Listening on localhost ${port}`));

I can connect to the socket server via the nodejs client files using socket.io-client

<script>
const socket = io('http://localhost:9000');
socket.on('message',(message) => console.log(message));

</script>

But the problem is whenever I try to connect from a different client I don't receive anything in the console. I tried to use Smart Web Socket client to debug what's happening but whenever I connect (try to) this happens在此处输入图像描述

Any help would be much appreciated and thanks in advance

So if anyone stumbles in this thread I was able to fix the problem by installing the latest version of socket.io (^3.1.0) and allowing the EIO3 connections

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