Reconnect functionallity.
This commit is contained in:
parent
7a2036beaa
commit
54d3b91150
49
Program.cs
49
Program.cs
@ -8,17 +8,30 @@ using System.Diagnostics;
|
|||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
|
static async void notify(string title, string message)
|
||||||
|
{
|
||||||
|
string command = $"notify-send '{title}' '{message}'";
|
||||||
|
await ExecuteCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
string username = Environment.GetEnvironmentVariable("SNEK_USERNAME") ?? "anonymous";
|
string username = Environment.GetEnvironmentVariable("SNEK_USERNAME") ?? "anonymous";
|
||||||
string password = Environment.GetEnvironmentVariable("SNEK_PASSWORD") ?? "anonymous";
|
string password = Environment.GetEnvironmentVariable("SNEK_PASSWORD") ?? "anonymous";
|
||||||
|
|
||||||
|
while (true) // Loop for auto-reconnection
|
||||||
|
{
|
||||||
using (var client = new ClientWebSocket())
|
using (var client = new ClientWebSocket())
|
||||||
{
|
{
|
||||||
Uri serverUri = new Uri("wss://snek.molodetz.nl/rpc.ws");
|
Uri serverUri = new Uri("wss://snek.molodetz.nl/rpc.ws");
|
||||||
await client.ConnectAsync(serverUri, CancellationToken.None);
|
|
||||||
|
|
||||||
var jsonMessage = JsonConvert.SerializeObject(new {
|
try
|
||||||
|
{
|
||||||
|
await client.ConnectAsync(serverUri, CancellationToken.None);
|
||||||
|
Console.WriteLine("Connected to the server.");
|
||||||
|
|
||||||
|
var jsonMessage = JsonConvert.SerializeObject(new
|
||||||
|
{
|
||||||
method = "login",
|
method = "login",
|
||||||
args = new[] { username, password }
|
args = new[] { username, password }
|
||||||
});
|
});
|
||||||
@ -26,10 +39,11 @@ class Program
|
|||||||
var bytes = Encoding.UTF8.GetBytes(jsonMessage);
|
var bytes = Encoding.UTF8.GetBytes(jsonMessage);
|
||||||
var buffer = new ArraySegment<byte>(bytes);
|
var buffer = new ArraySegment<byte>(bytes);
|
||||||
await client.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
|
await client.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
|
||||||
|
|
||||||
Console.WriteLine("Message sent: " + jsonMessage);
|
Console.WriteLine("Message sent: " + jsonMessage);
|
||||||
|
|
||||||
Boolean isLoggedIn = false;
|
Boolean isLoggedIn = false;
|
||||||
var receiveBuffer = new byte[1024000]; // Increased buffer size
|
var receiveBuffer = new byte[1024 * 1024]; // Optimized buffer size
|
||||||
|
|
||||||
while (client.State == WebSocketState.Open)
|
while (client.State == WebSocketState.Open)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -39,8 +53,15 @@ class Program
|
|||||||
Console.WriteLine("Message received: " + responseMessage);
|
Console.WriteLine("Message received: " + responseMessage);
|
||||||
|
|
||||||
dynamic jsonResponse = JsonConvert.DeserializeObject(responseMessage);
|
dynamic jsonResponse = JsonConvert.DeserializeObject(responseMessage);
|
||||||
if(!isLoggedIn)
|
if (!isLoggedIn)
|
||||||
{
|
{
|
||||||
|
Boolean isSuccess = jsonResponse.success != null ? (bool)jsonResponse.success : false;
|
||||||
|
if (!isSuccess)
|
||||||
|
{
|
||||||
|
notify("Snek", $"Login as {username} failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
notify("Snek", "Logged in as " + username);
|
||||||
isLoggedIn = true;
|
isLoggedIn = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -49,16 +70,30 @@ class Program
|
|||||||
|
|
||||||
if (receivedUsername != username)
|
if (receivedUsername != username)
|
||||||
{
|
{
|
||||||
string command = $"notify-send '{receivedUsername}' '{message}'";
|
notify(receivedUsername, message);
|
||||||
await ExecuteCommand(command);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Error processing message: " + ex.Message);
|
Console.WriteLine("Error processing message: " + ex.Message);
|
||||||
|
break; // Exit the inner loop to reconnect
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (System.InvalidOperationException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error connecting to server: " + ex.Message);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("An error occurred: " + ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait before attempting to reconnect
|
||||||
|
Console.WriteLine("Reconnecting in 5 seconds...");
|
||||||
|
await Task.Delay(5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task ExecuteCommand(string command)
|
private static async Task ExecuteCommand(string command)
|
||||||
|
Loading…
Reference in New Issue
Block a user