itisnajim / SocketIOUnity

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

Socket.io client connects but doesn't emit in a Unity project #55

Closed royibernthal closed 1 year ago

royibernthal commented 1 year ago

I'm trying to get socket.io to work in Unity.

On both the client (Unity) and the server (Node) I can see that it was able to connect successfully.

However, the server never receives the "message" event that I'm emitting upon connection.

I don't see any errors.

using UnityEngine;
using System;

public class Test : MonoBehaviour
{
    void Start() {
        var uri = new Uri("ws://localhost:8090");

        var client = new SocketIOUnity(uri);

        client.OnConnected += async (sender, e) =>
        {
            Debug.Log("OnConnected");

            await client.EmitAsync("message", "bla"); // client.Emit doesn't work either
        };

        client.OnError += (sender, e) => {
            Debug.Log("OnError: " + e);
        };

        client.Connect();
    }
}

Here's the Node server:

const httpServer = require("http").createServer();

const io = require("socket.io")(httpServer, { cors: true });

io.on("connection", (socket) => {
    console.log('connection');
});

io.on("message", (value) => {
    console.log('message', value);
});

httpServer.listen(8090);
itisnajim commented 1 year ago

replace ws:by http:and set the protocol to transporter to WebSocket like in:

var uri = new Uri("http://localhost:8090");
socket = new SocketIOUnity(uri, new SocketIOOptions
{
    Query = new Dictionary<string, string>
        {
            //{"token", "UNITY" }
        }
    ,
    Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
});

and retry the emitting!

royibernthal commented 1 year ago

@itisnajim Thanks :) My bad it actually worked with the example above as well, the server was throwing a silent error which was extremely hard to notice.

However I now encountered a new issue: https://github.com/itisnajim/SocketIOUnity/issues/56