From ab3e9e520bb0eec0fb9bb995a1633c0e00530161 Mon Sep 17 00:00:00 2001 From: Peace Date: Wed, 29 May 2024 15:27:20 +0900 Subject: [PATCH] web socket echo client --- InterMediateSharp/InterMediateSharp.sln | 22 ++++++++ .../WebSocketEchoClient/Program.cs | 51 +++++++++++++++++++ .../WebSocketEchoClient.csproj | 10 ++++ .../WebSocketEchoClient.sln | 25 +++++++++ 4 files changed, 108 insertions(+) create mode 100644 InterMediateSharp/InterMediateSharp.sln create mode 100644 InterMediateSharp/WebSocketEchoClient/Program.cs create mode 100644 InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.csproj create mode 100644 InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.sln diff --git a/InterMediateSharp/InterMediateSharp.sln b/InterMediateSharp/InterMediateSharp.sln new file mode 100644 index 0000000..73daa71 --- /dev/null +++ b/InterMediateSharp/InterMediateSharp.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSocketEchoClient", "WebSocketEchoClient\WebSocketEchoClient.csproj", "{03CF4D05-601B-4CF8-9DE1-B19AB3191EFF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {03CF4D05-601B-4CF8-9DE1-B19AB3191EFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03CF4D05-601B-4CF8-9DE1-B19AB3191EFF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03CF4D05-601B-4CF8-9DE1-B19AB3191EFF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03CF4D05-601B-4CF8-9DE1-B19AB3191EFF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/InterMediateSharp/WebSocketEchoClient/Program.cs b/InterMediateSharp/WebSocketEchoClient/Program.cs new file mode 100644 index 0000000..6c2b851 --- /dev/null +++ b/InterMediateSharp/WebSocketEchoClient/Program.cs @@ -0,0 +1,51 @@ +using System.Net.WebSockets; +using System.Text; + +namespace WebSocketEchoClient; + +class Program +{ + static async Task Main(string[] args) + { + Console.WriteLine("Starting WebSocket Client..."); + + using (ClientWebSocket ws = new ClientWebSocket()) + { + try + { + await ws.ConnectAsync(new Uri("ws://echo.websocket.org"), CancellationToken.None); + Console.WriteLine("Connected!"); + + Task receiveTask = ReceiveMessages(ws); + await SendMessages(ws); + + await receiveTask; + } + catch (Exception ex) + { + Console.WriteLine($"Exception: {ex.Message}"); + } + } + } + static async Task SendMessages(ClientWebSocket ws) + { + while (ws.State == WebSocketState.Open) + { + string message = Console.ReadLine(); + byte[] bytes = Encoding.UTF8.GetBytes(message); + await ws.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Text, true, CancellationToken.None); + } + } + + static async Task ReceiveMessages(ClientWebSocket ws) + { + byte[] buffer = new byte[1024]; + + while (ws.State == WebSocketState.Open) + { + WebSocketReceiveResult result = await ws.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); + string message = Encoding.UTF8.GetString(buffer, 0, result.Count); + Console.WriteLine($"Received: {message}"); + } + } +} diff --git a/InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.csproj b/InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.sln b/InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.sln new file mode 100644 index 0000000..d7c46cc --- /dev/null +++ b/InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSocketEchoClient", "WebSocketEchoClient.csproj", "{099C7E7D-6089-4A25-973E-40B4B90F43CA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {099C7E7D-6089-4A25-973E-40B4B90F43CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {099C7E7D-6089-4A25-973E-40B4B90F43CA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {099C7E7D-6089-4A25-973E-40B4B90F43CA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {099C7E7D-6089-4A25-973E-40B4B90F43CA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {252A9955-1B49-40FA-B17E-B9A551111109} + EndGlobalSection +EndGlobal