itisnajim / SocketIOUnity

A Wrapper for socket.io-client-csharp to work with Unity.
MIT License
379 stars 65 forks source link

No events received from socket.io v2 #62

Closed jeffrson closed 1 year ago

jeffrson commented 1 year ago

Hi, I have this NodeJS-Socket.io server (socket.io@2.5.0):

import Server from "socket.io"
const io = new Server(3000)
io.on("connection", (socket) => console.log("connection"))
setInterval(() => { console.log("emit hi"); io.sockets.emit("hi", "everyone")}, 5000)

and this code in a MonoBehaviour:

async void Start()
{
    Uri uri = new Uri("http://localhost:3000");
    SocketIOUnity socket = new SocketIOUnity(uri, new SocketIOOptions
    {
       Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
    });
    await socket.ConnectAsync();
    socket.On("hi", (response) =>
    {
        print(response);
    });
}

When the project in Unity starts, it apparenty connects (server outputs "connection" twice!!!), but no response can be seen in Unity. OTOH, with this client (socket.io-client@2.5.0):

import io from 'socket.io-client'
const socket = io("http://localhost:3000", { transports: ['websocket'] })
socket.on('connect', () => console.log("connect"))
socket.on('hi', response => console.log(response))

it works as expected (only one "connection" in server, "everyone" in client).

Furthermore, with a server based on socket.io v4 (only change in import), the same Unity code works fine.

Unfortunately I need to connect to v2 server api :-/

itisnajim commented 1 year ago

in SocketIOOptionsset EIOoption to V3 see more at: https://github.com/doghappy/socket.io-client-csharp#options

jeffrson commented 1 year ago

Thank you so much! Works as intended.