class Program { static AsyncWebSocketClient _client; static void Main(string[] args) { NLogLogger.Use(); Task.Run(async () => { try { var config = new AsyncWebSocketClientConfiguration(); //config.SslTargetHost = "Cowboy"; //config.SslClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(@"D:\\Cowboy.cer")); //config.SslPolicyErrorsBypassed = true; echo.websocket.org/"); uri = ); _client = new AsyncWebSocketClient(uri, OnServerTextReceived, OnServerBinaryReceived, OnServerConnected, OnServerDisconnected, config); await _client.Connect(); Console.WriteLine(, uri); Console.WriteLine(); while (_client.State == WebSocketState.Open) { try { string text = Console.ReadLine(); ) break; Task.Run(async () => { //await _client.SendText(text); _client.SendBinaryAsync(Encoding.UTF8.GetBytes(text)); Console.WriteLine(, _client.LocalEndPoint, text); }).Forget(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } await _client.Close(WebSocketCloseCode.NormalClosure); Console.WriteLine(, uri); } catch (Exception ex) { Logger.Get<Program>().Error(ex.Message, ex); } }).Wait(); Console.ReadKey(); } Task OnServerConnected(AsyncWebSocketClient client) { Console.WriteLine(, client.RemoteEndPoint)); await Task.CompletedTask; } Task OnServerTextReceived(AsyncWebSocketClient client, string text) { Console.Write(, client.RemoteEndPoint)); Console.WriteLine(, text)); await Task.CompletedTask; } Task OnServerBinaryReceived(AsyncWebSocketClient client, byte[] data, int offset, int count) { var text = Encoding.UTF8.GetString(data, offset, count); Console.Write(, client.RemoteEndPoint)); Console.WriteLine(, text)); await Task.CompletedTask; } Task OnServerDisconnected(AsyncWebSocketClient client) { Console.WriteLine(, client.RemoteEndPoint)); await Task.CompletedTask; } }
相关资料本篇文章《Cowboy.WebSockets 开源 WebSocket 网络库》由 Dennis Gao 发表自博客园个人博客,未经作者本人同意禁止以任何的形式转载,任何自动的或人为的爬虫转载行为均为耍流氓。