2024-02-12 17:30:56 +00:00
|
|
|
using System.Net;
|
2022-05-18 16:39:48 +01:00
|
|
|
using TcpDotNet;
|
2022-07-05 14:55:59 +01:00
|
|
|
using TcpDotNet.ClientIntegrationTest;
|
|
|
|
using TcpDotNet.ClientIntegrationTest.PacketHandlers;
|
2022-05-18 16:39:48 +01:00
|
|
|
using TcpDotNet.Protocol;
|
|
|
|
using TcpDotNet.Protocol.Packets.ClientBound;
|
|
|
|
using TcpDotNet.Protocol.Packets.ServerBound;
|
|
|
|
|
|
|
|
using var client = new ProtocolClient();
|
2022-05-19 10:37:37 +01:00
|
|
|
client.Disconnected += (_, e) => Console.WriteLine($"Disconnected: {e.DisconnectReason}");
|
|
|
|
|
2022-05-18 16:39:48 +01:00
|
|
|
client.RegisterPacketHandler(PacketHandler<PongPacket>.Empty);
|
2022-07-05 14:55:59 +01:00
|
|
|
client.RegisterPacketHandler(new GoodbyePacketHandler());
|
2024-02-13 00:06:42 +00:00
|
|
|
|
|
|
|
var remoteEP = new IPEndPoint(IPAddress.Loopback, 1234);
|
|
|
|
Console.WriteLine($"Connecting to {remoteEP}");
|
|
|
|
await client.ConnectAsync(remoteEP);
|
2022-05-18 16:39:48 +01:00
|
|
|
|
2022-05-19 10:37:37 +01:00
|
|
|
Console.WriteLine($"Connected to {client.RemoteEndPoint}. My session is {client.SessionId}");
|
2022-05-18 16:39:48 +01:00
|
|
|
|
2022-05-19 10:37:37 +01:00
|
|
|
var ping = new PingPacket();
|
2022-05-18 17:51:12 +01:00
|
|
|
Console.WriteLine($"Sending ping packet with payload: {BitConverter.ToString(ping.Payload)}");
|
2024-02-12 17:30:56 +00:00
|
|
|
var pong = await client.SendAndReceiveAsync<PongPacket>(ping);
|
2022-05-18 17:51:12 +01:00
|
|
|
|
|
|
|
Console.WriteLine($"Received pong packet with payload: {BitConverter.ToString(pong.Payload)}");
|
|
|
|
Console.WriteLine(pong.Payload.SequenceEqual(ping.Payload) ? "Payload matches!" : "Payload does not match!");
|
2022-07-05 14:55:59 +01:00
|
|
|
|
|
|
|
await client.SendPacketAsync(new HelloPacket {Message = "Hello, world!"});
|
|
|
|
|
|
|
|
while (client.IsConnected)
|
|
|
|
{
|
|
|
|
await client.ReadNextPacketAsync();
|
|
|
|
}
|
|
|
|
await Task.Delay(-1);
|