#how to working unity engine socket io.emit and io.on. i am buy socket.io v4 plus

1 messages · Page 1 of 1 (latest)

silent kernel
#

You can't emit before connecting (and actually BEING connected - connect takes a few ms)

still barn
#

What should I do now?

silent kernel
#

change your code so that it does not Emit before being connected. You can for example put the emit into the "connect" callback

still barn
#

I do not know English well
But if you look at the code, you will understand

#

how to connect callback

silent kernel
#

I understand. Your problem is that you want to Emit while the connection is not ready.

You are trying this: "Init -> Send -> Connect" but you must "Init -> Connect -> Send"

 private void Start()
        {

            io.D.On("connect", () =>
            {
                Debug.Log("local connected");
                io.D.Emit("read", new object[] { Myid });
            });

            io.D.On<string>("Jobs", (payload) =>
            {
                Debug.Log("SERVER: " + payload);
            });

            io.Connect("http://localhost:8000");
        }

#

this would work for example

#

You can not use Emit before the io.D.On("connect", ... has been called

still barn
#

okey

#

not working

silent kernel
#

I need some description what is working and what is not. probably logs...

still barn
#

Debug.log Server2 is working

#

server 1 is not working

#

my server is

#

socket.on('Book', (MyId) => {
// Assuming you perform some database query or processing to get the results
const results = [
{ id: 1, title: MyId },
{ id: 2, title: 'Book 2' },
{ id: 3, title: 'Book 3' }
];

  // Emit the results back to the client
  socket.emit('Jobs', results);
});
socket.emit('Casper', "results");
#

json data response

silent kernel
#

Absolutely correct, because your response is not a string

still barn
#

i am not undestand

#

SERVER1: Firesplash.GameDevAssets.SocketIOPlus.SocketIOPacket

#

console is top text

silent kernel
#

Your server is emitting an event "Jobs" and it transfers an array of objects

[
        { id: 1, title: MyId },
        { id: 2, title: 'Book 2' },
        { id: 3, title: 'Book 3' }
      ]

your client side is expecting a "string"

#
io.D.On<string>("Casper", (payload) =>
            {
                Debug.Log("SERVER2: " + payload);
            });
#

this is never called because no matching event is coming in

still barn
#

working

#

Jobs how to fix ?

silent kernel
#

oh I see

still barn
#

my console

#

server 2 is working

#

server 1 is response is not working

silent kernel
#

it is

#

you are only not extracting the payload from the Event (you are registering without using the Generic type so your callback receives a SocketIOEvent instead of a string)

#

but this is what I wrote earlier

#

I just confused the code snippets

#

you are sending an array of objects so you need to receive an array of objects as well

#

I would create a matching type in C# and use the generic

silent kernel
#
[Serializable]
struct Job {
  public int id;
  public string title;
}

io.D.On<Job[]>("Jobs", (jobs) => {
  //jobs is now your array
);

This should work

#

argh

#

wait

#

now it's correct. I am confusing the events over and over 😄

still barn
#

Thank you very much

silent kernel
#

You're welcome

still barn
#

Let me check

silent kernel
#

did it work?

still barn
#

No , not working

#

[Serializable] this not working red line bottom

#

But I made it work

#

My server response is array i convert to json
// Assuming you perform some database query or processing to get the results
const results = [
{ id: 1, title: 'book 1' },
{ id: 2, title: 'Book 2' },
{ id: 3, title: 'Book 3' }
];
const jsonString = JSON.stringify(results);

  // Emit the results back to the client
  socket.emit('Jobs', jsonString);
#

and next

#

io.D.On<string>("Jobs", (jobs) =>
{
Debug.Log(jobs);
}
Whis is working

#

working

silent kernel
#

but in this case you will receive the data as a json string. If this is what you want, yes, that is an option.