How to send data with tag, serialization, class? #48
Answered
by
alec1o
nvh2001
asked this question in
Q&A (Question and Answer)
-
client:
server
thanks!!! |
Beta Was this translation helpful? Give feedback.
Answered by
alec1o
May 16, 2024
Replies: 1 comment 9 replies
-
Yes it's possible! Use Byter is included in Netly project (no need external install) e.g. (Byter and Netly Event) = 💯 + 😄 using Byter;
// create writer
Writer w = new Writer();
w.Add((int)1234567890); // tag
w.Add((string)"Alecio"); // user
w.Add((string)"Alecio123"); // password
byte[] BYTER_BUFFER = w.GetBytes(); // send data
client.ToEvent("my login event", BYTER_BUFFER);
// receive data
client.OnEvent((string name, byte[] data) =>
{
if (name == "my login event")
{
Reader r = new Reader(data);
string tag = r.Read<string>();
string user = r.Read<string>();
string password = r.Read<string>();
// check is it's sucess, if false mean error on read any or all data. It don't make exception.
if (!r.Sucess)
{
client.ToEvent("login error event", "Invalid tag, user, or password! try again.");
}
Console.WriteLine($"Login event [{r.Sucess?"sucess": "error"}]: tag={tag}, user={user}, password={password}");
}
}); e.g. (Byter and Without Netly Event) = 💯 + 🎃 using Byter;
// create writer
Writer w = new Writer();
w.Add((string)"my login event"); // message id
w.Add((int)1234567890); // tag
w.Add((string)"Alecio"); // user
w.Add((string)"Alecio123"); // password
byte[] BYTER_BUFFER = w.GetBytes(); // send data
client.ToData(BYTER_BUFFER);
// receive data
client.OnData((byte[] data) =>
{
Reader r = new Reader(data);
string messageId = r.Read<string>();
if (messageId == "my login event" && r.Sucess)
{
string tag = r.Read<string>();
string user = r.Read<string>();
string password = r.Read<string>();
// check is it's sucess, if false mean error on read any or all data. It don't make exception.
if (!r.Sucess)
{
client.ToData("Invalid tag, user, or password! try again.");
}
Console.WriteLine($"Login event [{r.Sucess?"sucess": "error"}]: tag={tag}, user={user}, password={password}");
}
else
{
Console.WriteLine("This isn't login data");
}
}); |
Beta Was this translation helpful? Give feedback.
9 replies
Answer selected by
nvh2001
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes it's possible! Use Byter is included in Netly project (no need external install)
e.g. (Byter and Netly Event) = 💯 + 😄