web socket echo client

main
Peace 11 months ago
parent 31886cb511
commit ab3e9e520b
  1. 22
      InterMediateSharp/InterMediateSharp.sln
  2. 51
      InterMediateSharp/WebSocketEchoClient/Program.cs
  3. 10
      InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.csproj
  4. 25
      InterMediateSharp/WebSocketEchoClient/WebSocketEchoClient.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

@ -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<byte>(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<byte>(buffer), CancellationToken.None);
string message = Encoding.UTF8.GetString(buffer, 0, result.Count);
Console.WriteLine($"Received: {message}");
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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
Loading…
Cancel
Save