92 lines
3.1 KiB
C#
92 lines
3.1 KiB
C#
using EonaCat.Connections.Models;
|
|
|
|
namespace EonaCat.Connections.REST.Client.Example
|
|
{
|
|
public class Program
|
|
{
|
|
private const string SERVER_IP = "127.0.0.1";
|
|
private const int SERVER_PORT = 5580;
|
|
private const int WEBHOOK_PORT = 5581;
|
|
|
|
public static async Task Main(string[] args)
|
|
{
|
|
var config = new Configuration
|
|
{
|
|
Host = SERVER_IP,
|
|
Port = SERVER_PORT,
|
|
IsSelfSignedEnabled = true,
|
|
EnableAutoReconnect = true,
|
|
ReconnectDelayInSeconds = 5,
|
|
};
|
|
|
|
await using var client = new RestNetworkClient(config);
|
|
|
|
client.OnConnected += (sender, e) =>
|
|
{
|
|
Console.WriteLine($"[REST Client] Connected to {e.RemoteEndPoint} (clientId: {e.ClientId})");
|
|
};
|
|
|
|
client.OnDataReceived += (sender, e) =>
|
|
{
|
|
Console.WriteLine($"[REST Client] Received: {e.StringData}");
|
|
};
|
|
|
|
client.OnDisconnected += (sender, e) =>
|
|
{
|
|
Console.WriteLine($"[REST Client] Disconnected: {e.Reason}");
|
|
};
|
|
|
|
client.OnGeneralError += (sender, e) =>
|
|
{
|
|
Console.WriteLine($"[REST Client] Error: {e.Message} - {e.Exception?.Message}");
|
|
};
|
|
|
|
Console.WriteLine("[REST Client] Connecting...");
|
|
var connected = await client.ConnectAsync();
|
|
|
|
if (!connected)
|
|
{
|
|
Console.WriteLine("[REST Client] Failed to connect. Press any key to exit.");
|
|
Console.ReadKey();
|
|
return;
|
|
}
|
|
|
|
await client.SendNicknameAsync("RestTestClient");
|
|
|
|
Console.WriteLine($"[REST Client] Starting webhook listener on port {WEBHOOK_PORT}...");
|
|
await client.StartWebhookListenerAsync(WEBHOOK_PORT);
|
|
await client.RegisterWebhookAsync();
|
|
Console.WriteLine("[REST Client] Webhook registered.");
|
|
|
|
Console.WriteLine("[REST Client] Commands: type a message to send, 'broadcast <msg>' to broadcast, 'exit' to quit.");
|
|
|
|
while (true)
|
|
{
|
|
Console.Write("> ");
|
|
var input = Console.ReadLine();
|
|
|
|
if (string.IsNullOrEmpty(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (input.StartsWith("broadcast ", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
var msg = input.Substring("broadcast ".Length);
|
|
await client.BroadcastAsync(msg);
|
|
Console.WriteLine("[REST Client] Broadcast sent.");
|
|
}
|
|
else
|
|
{
|
|
await client.SendAsync(input);
|
|
}
|
|
}
|
|
|
|
await client.StopWebhookListenerAsync();
|
|
await client.DisconnectAsync();
|
|
Console.WriteLine("[REST Client] Disconnected. Press any key to exit.");
|
|
Console.ReadKey();
|
|
}
|
|
}
|
|
}
|