Rocher0724 / socket.io-unity

MIT License
107 stars 19 forks source link

socket.Emit is not working #11

Open gdebojyoti opened 3 years ago

gdebojyoti commented 3 years ago

First of all, thank you for creating this library. It's making life simpler for people like me. 😃

I just started using your library for a demo multiplayer project. I ran into an issue - I cannot seem to emit messages from my Unity app. Here's the corresponding CS code.

using Socket.Quobject.SocketIoClientDotNet.Client;
using UnityEngine;

public class SocketController : MonoBehaviour {
  private QSocket socket;

  void Start () {
    // initialize socket
    socket = IO.Socket (Config.serverUrl);

    // on successful connection with node server
    socket.On (QSocket.EVENT_CONNECT, OnConnect);
  }

  void OnConnect () {
    Player host = new Player ("debojyotighosh");
    Debug.Log("this line gets printed..");

    socket.Emit ("HOST_GAME", host); // this does not seem to work
  }
}

The corresponding code in my Node JS app -

io = require('socket.io')(server);
io.on('connection', (socket) => {
    console.log("A user has connected"); // this line appears in my Node JS logs
    socket.on("HOST_GAME", () => {
        console.log("this line does not get logged");
    });
});

I know it's probably a lot to ask, but can you please tell me what I am missing?

gdebojyoti commented 3 years ago

socket.io version = 2.0.4

Rocher0724 commented 3 years ago

hi there @gdebojyoti

I tested with socket 2.0.4 Originally I was using Socket 1.7.4 version.

It didn't work at first, but it's working well after several attempts. I don't understand why it didn't work out at first.

but... It's working fine now on socket 2.0.4

If it doesn't work, try 1.7.4 socket version. still good

I'll attach the codes I used below.

Api Compatibility Level : .NET 4.x

check this out.

스크린샷 2021-05-04 오전 10 41 06 스크린샷 2021-05-04 오전 10 41 31 스크린샷 2021-05-04 오전 10 40 36 스크린샷 2021-05-04 오전 10 45 33

gdebojyoti commented 3 years ago

@Rocher0724 Thank you for your reply. However, I moved on to a different package (https://assetstore.unity.com/packages/tools/network/socket-io-v2-client-for-unity-standalone-webgl-186263).

Btw, I don't really want to downgrade my Socket version. I have a web app that is relying on the same service. 😬