diff --git a/Program.cs b/Program.cs index d3f0ce5..2faf514 100644 --- a/Program.cs +++ b/Program.cs @@ -8,55 +8,90 @@ using System.Diagnostics; 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) { string username = Environment.GetEnvironmentVariable("SNEK_USERNAME") ?? "anonymous"; string password = Environment.GetEnvironmentVariable("SNEK_PASSWORD") ?? "anonymous"; - using (var client = new ClientWebSocket()) + while (true) // Loop for auto-reconnection { - Uri serverUri = new Uri("wss://snek.molodetz.nl/rpc.ws"); - await client.ConnectAsync(serverUri, CancellationToken.None); - - var jsonMessage = JsonConvert.SerializeObject(new { - method = "login", - args = new[] { username, password } - }); - - var bytes = Encoding.UTF8.GetBytes(jsonMessage); - var buffer = new ArraySegment<byte>(bytes); - await client.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); - - Console.WriteLine("Message sent: " + jsonMessage); - Boolean isLoggedIn = false; - var receiveBuffer = new byte[1024000]; // Increased buffer size - while (client.State == WebSocketState.Open) + using (var client = new ClientWebSocket()) { + Uri serverUri = new Uri("wss://snek.molodetz.nl/rpc.ws"); + try { - WebSocketReceiveResult result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None); - var responseMessage = Encoding.UTF8.GetString(receiveBuffer, 0, result.Count); - Console.WriteLine("Message received: " + responseMessage); + await client.ConnectAsync(serverUri, CancellationToken.None); + Console.WriteLine("Connected to the server."); - dynamic jsonResponse = JsonConvert.DeserializeObject(responseMessage); - if(!isLoggedIn) + var jsonMessage = JsonConvert.SerializeObject(new { - isLoggedIn = true; - continue; - } - string receivedUsername = jsonResponse.username != null ? System.Security.SecurityElement.Escape((string)jsonResponse.username) : "Unknown User"; - string message = jsonResponse.message != null ? System.Security.SecurityElement.Escape((string)jsonResponse.message) : "No message provided"; + method = "login", + args = new[] { username, password } + }); - if (receivedUsername != username) + var bytes = Encoding.UTF8.GetBytes(jsonMessage); + var buffer = new ArraySegment<byte>(bytes); + await client.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); + Console.WriteLine("Message sent: " + jsonMessage); + + Boolean isLoggedIn = false; + var receiveBuffer = new byte[1024 * 1024]; // Optimized buffer size + + while (client.State == WebSocketState.Open) { - string command = $"notify-send '{receivedUsername}' '{message}'"; - await ExecuteCommand(command); + try + { + WebSocketReceiveResult result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None); + var responseMessage = Encoding.UTF8.GetString(receiveBuffer, 0, result.Count); + Console.WriteLine("Message received: " + responseMessage); + + dynamic jsonResponse = JsonConvert.DeserializeObject(responseMessage); + 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; + continue; + } + string receivedUsername = jsonResponse.username != null ? System.Security.SecurityElement.Escape((string)jsonResponse.username) : "Unknown User"; + string message = jsonResponse.message != null ? System.Security.SecurityElement.Escape((string)jsonResponse.message) : "No message provided"; + + if (receivedUsername != username) + { + notify(receivedUsername, message); + } + } + catch (Exception ex) + { + 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("Error processing message: " + ex.Message); + Console.WriteLine("An error occurred: " + ex.Message); } + + // Wait before attempting to reconnect + Console.WriteLine("Reconnecting in 5 seconds..."); + await Task.Delay(5000); } } }