Skip to content

Commit

Permalink
Fix the issue no statianzo#342 not log the information
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishant committed Jan 2, 2025
1 parent 45672e0 commit dd16b3b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/Fleck/FleckLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public static void Info(string message, Exception ex = null)
}

}
}
}
17 changes: 14 additions & 3 deletions src/Fleck/WebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>();
taskForException.SetException(e);
return taskForException.Task;
}
}

private void CloseSocket()
Expand Down
113 changes: 57 additions & 56 deletions src/Samples/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -1,64 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{3D7680B4-0224-4F0C-B1F6-74FB7586EBDC}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Fleck.Samples.ConsoleApp</RootNamespace>
<AssemblyName>Fleck.Samples.ConsoleApp</AssemblyName>
<FileAlignment>512</FileAlignment>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>False</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
<Compile Include="Server.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="client.html" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Fleck\Fleck.csproj">
<Project>{8B12D929-AFA9-4307-BEFF-2ED0F1070FEE}</Project>
<Name>Fleck</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<PropertyGroup>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{3D7680B4-0224-4F0C-B1F6-74FB7586EBDC}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Fleck.Samples.ConsoleApp</RootNamespace>
<AssemblyName>Fleck.Samples.ConsoleApp</AssemblyName>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>False</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
<Compile Include="Server.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="client.html" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Fleck\Fleck.csproj">
<Project>{8B12D929-AFA9-4307-BEFF-2ED0F1070FEE}</Project>
<Name>Fleck</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
66 changes: 40 additions & 26 deletions src/Samples/ConsoleApp/Server.cs
Original file line number Diff line number Diff line change
@@ -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<IWebSocketConnection>();
var server = new WebSocketServer("ws://0.0.0.0:8181");

try
{
FleckLog.Level = LogLevel.Debug;
var allSockets = new List<IWebSocketConnection>();
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")
Expand All @@ -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.");
}
}
}
}
}

0 comments on commit dd16b3b

Please sign in to comment.