| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 97aaa5a commit d88be15
37 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,123 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Net.Sockets; | ||
| 3 | + using System.Text; | ||
| 4 | + using System.Threading; | ||
| 5 | + using NetCoreServer; | ||
| 6 | + | ||
| 7 | + namespace WsChatClient | ||
| 8 | + { | ||
| 9 | + class ChatClient : WsClient | ||
| 10 | + { | ||
| 11 | + public ChatClient(string address, int port) : base(address, port) { } | ||
| 12 | + | ||
| 13 | + public void DisconnectAndStop() | ||
| 14 | + { | ||
| 15 | + _stop = true; | ||
| 16 | + CloseAsync(1000); | ||
| 17 | + while (IsConnected) | ||
| 18 | + Thread.Yield(); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public override void OnWSConnecting(HttpRequest request) | ||
| 22 | + { | ||
| 23 | + request.SetBegin("GET", "/"); | ||
| 24 | + request.SetHeader("Host", "localhost"); | ||
| 25 | + request.SetHeader("Origin", "http://localhost"); | ||
| 26 | + request.SetHeader("Upgrade", "websocket"); | ||
| 27 | + request.SetHeader("Connection", "Upgrade"); | ||
| 28 | + request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString()))); | ||
| 29 | + request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat"); | ||
| 30 | + request.SetHeader("Sec-WebSocket-Version", "13"); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + public override void OnWSConnected(HttpResponse response) | ||
| 34 | + { | ||
| 35 | + Console.WriteLine($"Chat WebSocket client connected a new session with Id {Id}"); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public override void OnWSDisconnected() | ||
| 39 | + { | ||
| 40 | + Console.WriteLine($"Chat WebSocket client disconnected a session with Id {Id}"); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public override void OnWSReceived(byte[] buffer, long offset, long size) | ||
| 44 | + { | ||
| 45 | + Console.WriteLine($"Incoming: {Encoding.UTF8.GetString(buffer, (int)offset, (int)size)}"); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + protected override void OnDisconnected() | ||
| 49 | + { | ||
| 50 | + Console.WriteLine($"Chat WebSocket client disconnected a session with Id {Id}"); | ||
| 51 | + | ||
| 52 | + // Wait for a while... | ||
| 53 | + Thread.Sleep(1000); | ||
| 54 | + | ||
| 55 | + // Try to connect again | ||
| 56 | + if (!_stop) | ||
| 57 | + ConnectAsync(); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + protected override void OnError(SocketError error) | ||
| 61 | + { | ||
| 62 | + Console.WriteLine($"Chat WebSocket client caught an error with code {error}"); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + private bool _stop = false; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + class Program | ||
| 69 | + { | ||
| 70 | + static void Main(string[] args) | ||
| 71 | + { | ||
| 72 | + // WebSocket server address | ||
| 73 | + string address = "127.0.0.1"; | ||
| 74 | + if (args.Length > 0) | ||
| 75 | + address = args[0]; | ||
| 76 | + | ||
| 77 | + // WebSocket server port | ||
| 78 | + int port = 8080; | ||
| 79 | + if (args.Length > 1) | ||
| 80 | + port = int.Parse(args[1]); | ||
| 81 | + | ||
| 82 | + Console.WriteLine($"WebSocket server address: {address}"); | ||
| 83 | + Console.WriteLine($"WebSocket server port: {port}"); | ||
| 84 | + | ||
| 85 | + Console.WriteLine(); | ||
| 86 | + | ||
| 87 | + // Create a new TCP chat client | ||
| 88 | + var client = new ChatClient(address, port); | ||
| 89 | + | ||
| 90 | + // Connect the client | ||
| 91 | + Console.Write("Client connecting..."); | ||
| 92 | + client.ConnectAsync(); | ||
| 93 | + Console.WriteLine("Done!"); | ||
| 94 | + | ||
| 95 | + Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client..."); | ||
| 96 | + | ||
| 97 | + // Perform text input | ||
| 98 | + for (; ; ) | ||
| 99 | + { | ||
| 100 | + string line = Console.ReadLine(); | ||
| 101 | + if (string.IsNullOrEmpty(line)) | ||
| 102 | + break; | ||
| 103 | + | ||
| 104 | + // Disconnect the client | ||
| 105 | + if (line == "!") | ||
| 106 | + { | ||
| 107 | + Console.Write("Client disconnecting..."); | ||
| 108 | + client.DisconnectAsync(); | ||
| 109 | + Console.WriteLine("Done!"); | ||
| 110 | + continue; | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + // Send the entered text to the chat server | ||
| 114 | + client.SendTextAsync(line); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + // Disconnect the client | ||
| 118 | + Console.Write("Client disconnecting..."); | ||
| 119 | + client.DisconnectAndStop(); | ||
| 120 | + Console.WriteLine("Done!"); | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + <Project Sdk="Microsoft.NET.Sdk"> | ||
| 2 | + | ||
| 3 | + <PropertyGroup> | ||
| 4 | + <OutputType>Exe</OutputType> | ||
| 5 | + <TargetFramework>netcoreapp3.0</TargetFramework> | ||
| 6 | + </PropertyGroup> | ||
| 7 | + | ||
| 8 | + <ItemGroup> | ||
| 9 | + <ProjectReference Include="..\..\source\NetCoreServer\NetCoreServer.csproj" /> | ||
| 10 | + </ItemGroup> | ||
| 11 | + | ||
| 12 | + </Project> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,114 @@ | |||
| 1 | + using NetCoreServer; | ||
| 2 | + using System; | ||
| 3 | + using System.Net; | ||
| 4 | + using System.Net.Sockets; | ||
| 5 | + using System.Text; | ||
| 6 | + | ||
| 7 | + namespace WsChatServer | ||
| 8 | + { | ||
| 9 | + class ChatSession : WsSession | ||
| 10 | + { | ||
| 11 | + public ChatSession(WsServer server) : base(server) { } | ||
| 12 | + | ||
| 13 | + public override void OnWSConnected(HttpRequest request) | ||
| 14 | + { | ||
| 15 | + Console.WriteLine($"Chat WebSocket session with Id {Id} connected!"); | ||
| 16 | + | ||
| 17 | + // Send invite message | ||
| 18 | + string message = "Hello from WebSocket chat! Please send a message or '!' to disconnect the client!"; | ||
| 19 | + SendTextAsync(message); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + public override void OnWSDisconnected() | ||
| 23 | + { | ||
| 24 | + Console.WriteLine($"Chat WebSocket session with Id {Id} disconnected!"); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + public override void OnWSReceived(byte[] buffer, long offset, long size) | ||
| 28 | + { | ||
| 29 | + string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); | ||
| 30 | + Console.WriteLine("Incoming: " + message); | ||
| 31 | + | ||
| 32 | + // Multicast message to all connected sessions | ||
| 33 | + ((WsServer)Server).MulticastText(message); | ||
| 34 | + | ||
| 35 | + // If the buffer starts with '!' the disconnect the current session | ||
| 36 | + if (message == "!") | ||
| 37 | + Close(1000); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + protected override void OnError(SocketError error) | ||
| 41 | + { | ||
| 42 | + Console.WriteLine($"Chat WebSocket session caught an error with code {error}"); | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + class ChatServer : WsServer | ||
| 47 | + { | ||
| 48 | + public ChatServer(IPAddress address, int port) : base(address, port) { } | ||
| 49 | + | ||
| 50 | + protected override TcpSession CreateSession() { return new ChatSession(this); } | ||
| 51 | + | ||
| 52 | + protected override void OnError(SocketError error) | ||
| 53 | + { | ||
| 54 | + Console.WriteLine($"Chat WebSocket server caught an error with code {error}"); | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + class Program | ||
| 59 | + { | ||
| 60 | + static void Main(string[] args) | ||
| 61 | + { | ||
| 62 | + // WebSocket server port | ||
| 63 | + int port = 8080; | ||
| 64 | + if (args.Length > 0) | ||
| 65 | + port = int.Parse(args[0]); | ||
| 66 | + // WebSocket server content path | ||
| 67 | + string www = "../../../../../www/ws"; | ||
| 68 | + if (args.Length > 1) | ||
| 69 | + www = args[1]; | ||
| 70 | + | ||
| 71 | + Console.WriteLine($"WebSocket server port: {port}"); | ||
| 72 | + Console.WriteLine($"WebSocket server static content path: {www}"); | ||
| 73 | + //Console.WriteLine($"WebSocket server website: http://localhost:{port}/ws/index.html"); | ||
| 74 | + | ||
| 75 | + Console.WriteLine(); | ||
| 76 | + | ||
| 77 | + // Create a new WebSocket server | ||
| 78 | + var server = new ChatServer(IPAddress.Any, port); | ||
| 79 | + server.AddStaticContent(www, "/chat"); | ||
| 80 | + | ||
| 81 | + // Start the server | ||
| 82 | + Console.Write("Server starting..."); | ||
| 83 | + server.Start(); | ||
| 84 | + Console.WriteLine("Done!"); | ||
| 85 | + | ||
| 86 | + Console.WriteLine("Press Enter to stop the server or '!' to restart the server..."); | ||
| 87 | + | ||
| 88 | + // Perform text input | ||
| 89 | + for (; ; ) | ||
| 90 | + { | ||
| 91 | + string line = Console.ReadLine(); | ||
| 92 | + if (string.IsNullOrEmpty(line)) | ||
| 93 | + break; | ||
| 94 | + | ||
| 95 | + // Restart the server | ||
| 96 | + if (line == "!") | ||
| 97 | + { | ||
| 98 | + Console.Write("Server restarting..."); | ||
| 99 | + server.Restart(); | ||
| 100 | + Console.WriteLine("Done!"); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + // Multicast admin message to all sessions | ||
| 104 | + line = "(admin) " + line; | ||
| 105 | + server.MulticastText(line); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + // Stop the server | ||
| 109 | + Console.Write("Server stopping..."); | ||
| 110 | + server.Stop(); | ||
| 111 | + Console.WriteLine("Done!"); | ||
| 112 | + } | ||
| 113 | + } | ||
| 114 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + <Project Sdk="Microsoft.NET.Sdk"> | ||
| 2 | + | ||
| 3 | + <PropertyGroup> | ||
| 4 | + <OutputType>Exe</OutputType> | ||
| 5 | + <TargetFramework>netcoreapp3.0</TargetFramework> | ||
| 6 | + </PropertyGroup> | ||
| 7 | + | ||
| 8 | + <ItemGroup> | ||
| 9 | + <ProjectReference Include="..\..\source\NetCoreServer\NetCoreServer.csproj" /> | ||
| 10 | + </ItemGroup> | ||
| 11 | + | ||
| 12 | + </Project> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,131 @@ | |||
| 1 | + using NetCoreServer; | ||
| 2 | + using System; | ||
| 3 | + using System.Net.Sockets; | ||
| 4 | + using System.Security.Authentication; | ||
| 5 | + using System.Security.Cryptography.X509Certificates; | ||
| 6 | + using System.Text; | ||
| 7 | + using System.Threading; | ||
| 8 | + | ||
| 9 | + namespace WssChatClient | ||
| 10 | + { | ||
| 11 | + class ChatClient : WssClient | ||
| 12 | + { | ||
| 13 | + public ChatClient(SslContext context, string address, int port) : base(context, address, port) { } | ||
| 14 | + | ||
| 15 | + public void DisconnectAndStop() | ||
| 16 | + { | ||
| 17 | + _stop = true; | ||
| 18 | + CloseAsync(1000); | ||
| 19 | + while (IsConnected) | ||
| 20 | + Thread.Yield(); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public override void OnWSConnecting(HttpRequest request) | ||
| 24 | + { | ||
| 25 | + request.SetBegin("GET", "/"); | ||
| 26 | + request.SetHeader("Host", "localhost"); | ||
| 27 | + request.SetHeader("Origin", "http://localhost"); | ||
| 28 | + request.SetHeader("Upgrade", "websocket"); | ||
| 29 | + request.SetHeader("Connection", "Upgrade"); | ||
| 30 | + request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString()))); | ||
| 31 | + request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat"); | ||
| 32 | + request.SetHeader("Sec-WebSocket-Version", "13"); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public override void OnWSConnected(HttpResponse response) | ||
| 36 | + { | ||
| 37 | + Console.WriteLine($"Chat WebSocket client connected a new session with Id {Id}"); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public override void OnWSDisconnected() | ||
| 41 | + { | ||
| 42 | + Console.WriteLine($"Chat WebSocket client disconnected a session with Id {Id}"); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + public override void OnWSReceived(byte[] buffer, long offset, long size) | ||
| 46 | + { | ||
| 47 | + Console.WriteLine($"Incoming: {Encoding.UTF8.GetString(buffer, (int)offset, (int)size)}"); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + protected override void OnDisconnected() | ||
| 51 | + { | ||
| 52 | + Console.WriteLine($"Chat WebSocket client disconnected a session with Id {Id}"); | ||
| 53 | + | ||
| 54 | + // Wait for a while... | ||
| 55 | + Thread.Sleep(1000); | ||
| 56 | + | ||
| 57 | + // Try to connect again | ||
| 58 | + if (!_stop) | ||
| 59 | + ConnectAsync(); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + protected override void OnError(SocketError error) | ||
| 63 | + { | ||
| 64 | + Console.WriteLine($"Chat WebSocket client caught an error with code {error}"); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + private bool _stop = false; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + class Program | ||
| 71 | + { | ||
| 72 | + static void Main(string[] args) | ||
| 73 | + { | ||
| 74 | + // WebSocket server address | ||
| 75 | + string address = "127.0.0.1"; | ||
| 76 | + if (args.Length > 0) | ||
| 77 | + address = args[0]; | ||
| 78 | + | ||
| 79 | + // WebSocket server port | ||
| 80 | + int port = 8443; | ||
| 81 | + if (args.Length > 1) | ||
| 82 | + port = int.Parse(args[1]); | ||
| 83 | + | ||
| 84 | + Console.WriteLine($"WebSocket server address: {address}"); | ||
| 85 | + Console.WriteLine($"WebSocket server port: {port}"); | ||
| 86 | + | ||
| 87 | + Console.WriteLine(); | ||
| 88 | + | ||
| 89 | + // Create and prepare a new SSL client context | ||
| 90 | + var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("client.pfx", "qwerty"), (sender, certificate, chain, sslPolicyErrors) => true); | ||
| 91 | + | ||
| 92 | + // Create a new TCP chat client | ||
| 93 | + var client = new ChatClient(context, address, port); | ||
| 94 | + | ||
| 95 | + // Connect the client | ||
| 96 | + Console.Write("Client connecting..."); | ||
| 97 | + client.ConnectAsync(); | ||
| 98 | + Console.WriteLine("Done!"); | ||
| 99 | + | ||
| 100 | + Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client..."); | ||
| 101 | + | ||
| 102 | + // Perform text input | ||
| 103 | + for (; ; ) | ||
| 104 | + { | ||
| 105 | + string line = Console.ReadLine(); | ||
| 106 | + if (string.IsNullOrEmpty(line)) | ||
| 107 | + break; | ||
| 108 | + | ||
| 109 | + // Reconnect the client | ||
| 110 | + if (line == "!") | ||
| 111 | + { | ||
| 112 | + Console.Write("Client reconnecting..."); | ||
| 113 | + if (client.IsConnected) | ||
| 114 | + client.ReconnectAsync(); | ||
| 115 | + else | ||
| 116 | + client.ConnectAsync(); | ||
| 117 | + Console.WriteLine("Done!"); | ||
| 118 | + continue; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + // Send the entered text to the chat server | ||
| 122 | + client.SendTextAsync(line); | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + // Disconnect the client | ||
| 126 | + Console.Write("Client disconnecting..."); | ||
| 127 | + client.DisconnectAndStop(); | ||
| 128 | + Console.WriteLine("Done!"); | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments