#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)
What should I do now?
change your code so that it does not Emit before being connected. You can for example put the emit into the "connect" callback
I do not know English well
But if you look at the code, you will understand
how to connect callback
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
I need some description what is working and what is not. probably logs...
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
Absolutely correct, because your response is not a string
i am not undestand
SERVER1: Firesplash.GameDevAssets.SocketIOPlus.SocketIOPacket
console is top text
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
this is work
working
Jobs how to fix ?
oh I see
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
how ?
[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 😄
Thank you very much
You're welcome
Let me check
did it work?
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
but in this case you will receive the data as a json string. If this is what you want, yes, that is an option.