Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
royibernthal opened this issue Apr 17, 2023 · 2 comments
Closed

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

royibernthal opened this issue Apr 17, 2023 · 2 comments

Comments

@royibernthal
Copy link

royibernthal commented Apr 17, 2023

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
Copy link
Owner

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
Copy link
Author

@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:
#56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants