1
0
mirror of https://github.com/oliverbooth/TcpDotNet synced 2024-11-09 22:45:41 +00:00
TcpDotNet/TcpDotNet.ClientIntegrationTest/Program.cs

32 lines
1.2 KiB
C#
Raw Normal View History

2022-05-18 16:39:48 +01:00
using System.Net;
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();
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());
2022-05-18 16:39:48 +01:00
await client.ConnectAsync(IPAddress.IPv6Loopback, 1234);
Console.WriteLine($"Connected to {client.RemoteEndPoint}. My session is {client.SessionId}");
2022-05-18 16:39:48 +01:00
var ping = new PingPacket();
Console.WriteLine($"Sending ping packet with payload: {BitConverter.ToString(ping.Payload)}");
var pong = await client.SendAndReceiveAsync<PingPacket, PongPacket>(ping);
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);