From dd16b3b9036d907f4ba472713558fb7a0c7169fc Mon Sep 17 00:00:00 2001 From: Nishant Date: Thu, 2 Jan 2025 20:26:29 +0530 Subject: [PATCH] Fix the issue no #342 not log the information --- src/Fleck/FleckLog.cs | 2 +- src/Fleck/WebSocketConnection.cs | 17 +++- src/Samples/ConsoleApp/ConsoleApp.csproj | 113 ++++++++++++----------- src/Samples/ConsoleApp/Server.cs | 66 +++++++------ 4 files changed, 112 insertions(+), 86 deletions(-) diff --git a/src/Fleck/FleckLog.cs b/src/Fleck/FleckLog.cs index 0bf2a7e8..dbc07759 100644 --- a/src/Fleck/FleckLog.cs +++ b/src/Fleck/FleckLog.cs @@ -41,4 +41,4 @@ public static void Info(string message, Exception ex = null) } } -} +} diff --git a/src/Fleck/WebSocketConnection.cs b/src/Fleck/WebSocketConnection.cs index 5aba2740..6b0f4a21 100644 --- a/src/Fleck/WebSocketConnection.cs +++ b/src/Fleck/WebSocketConnection.cs @@ -202,20 +202,31 @@ private void HandleReadError(Exception e) private Task SendBytes(byte[] bytes, Action callback = null) { + try + { return Socket.Send(bytes, () => { FleckLog.Debug("Sent " + bytes.Length + " bytes"); if (callback != null) callback(); }, - e => + e => { if (e is IOException) - FleckLog.Debug("Failed to send. Disconnecting.", e); + FleckLog.Debug("Failed to send. Disconnecting. Due to IOException"); else - FleckLog.Info("Failed to send. Disconnecting.", e); + FleckLog.Info("Failed to send. Disconnecting."); CloseSocket(); }); + } + catch (Exception e) + { + FleckLog.Error("Exception while sending bytes", e); + CloseSocket(); + var taskForException = new TaskCompletionSource(); + taskForException.SetException(e); + return taskForException.Task; + } } private void CloseSocket() diff --git a/src/Samples/ConsoleApp/ConsoleApp.csproj b/src/Samples/ConsoleApp/ConsoleApp.csproj index 57c0df0f..0ea916ee 100644 --- a/src/Samples/ConsoleApp/ConsoleApp.csproj +++ b/src/Samples/ConsoleApp/ConsoleApp.csproj @@ -1,64 +1,65 @@  - - Debug - x86 - 8.0.30703 - 2.0 - {3D7680B4-0224-4F0C-B1F6-74FB7586EBDC} - Exe - Properties - Fleck.Samples.ConsoleApp - Fleck.Samples.ConsoleApp - 512 - v4.5 - - - - x86 - True - full - False - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - x86 - pdbonly - True - bin\Release\ - TRACE - prompt - 4 - false - - - - - - - - - - - - - - - - {8B12D929-AFA9-4307-BEFF-2ED0F1070FEE} - Fleck - - - - - \ No newline at end of file + diff --git a/src/Samples/ConsoleApp/Server.cs b/src/Samples/ConsoleApp/Server.cs index 363bde5a..6507661b 100644 --- a/src/Samples/ConsoleApp/Server.cs +++ b/src/Samples/ConsoleApp/Server.cs @@ -1,36 +1,43 @@ using System; using System.Collections.Generic; +using Fleck; +using System.Net.Sockets; using System.Linq; -using System.Threading; -namespace Fleck.Samples.ConsoleApp +class Program { - class Server + static void Main() { - static void Main() + FleckLog.Level = LogLevel.Debug; + FleckLog.LogAction = (level, message, ex) => + { + if (level >= FleckLog.Level) + Console.WriteLine("{0} [{1}] {2} {3}", DateTime.Now, level, message, ex?.Message); + }; + + var allSockets = new List(); + var server = new WebSocketServer("ws://0.0.0.0:8181"); + + try { - FleckLog.Level = LogLevel.Debug; - var allSockets = new List(); - var server = new WebSocketServer("ws://0.0.0.0:8181"); server.Start(socket => + { + socket.OnOpen = () => { - socket.OnOpen = () => - { - Console.WriteLine("Open!"); - allSockets.Add(socket); - }; - socket.OnClose = () => - { - Console.WriteLine("Close!"); - allSockets.Remove(socket); - }; - socket.OnMessage = message => - { - Console.WriteLine(message); - allSockets.ToList().ForEach(s => s.Send("Echo: " + message)); - }; - }); - + Console.WriteLine("Open!"); + allSockets.Add(socket); + }; + socket.OnClose = () => + { + Console.WriteLine("Close!"); + allSockets.Remove(socket); + }; + socket.OnMessage = message => + { + Console.WriteLine(message); + allSockets.ToList().ForEach(s => s.Send("Echo: " + message)); + }; + }); var input = Console.ReadLine(); while (input != "exit") @@ -41,7 +48,14 @@ static void Main() } input = Console.ReadLine(); } - + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + if (ex is SocketException socketEx && socketEx.SocketErrorCode == SocketError.AddressAlreadyInUse) + { + Console.WriteLine("The port is already in use. Please use a different port."); + } } } -} +} \ No newline at end of file