HTML5技术

Cowboy.WebSockets 开源 WebSocket 网络库 - 匠心十年(2)

字号+ 作者:H5之家 来源:博客园 2016-02-06 16:09 我要评论( )

class Program { static AsyncWebSocketClient _client; static void Main( string [] args){NLogLogger.Use();Task.Run( async () = { try { var config = new AsyncWebSocketClientConfiguration(); // config.Ss

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 发表自博客园个人博客,未经作者本人同意禁止以任何的形式转载,任何自动的或人为的爬虫转载行为均为耍流氓。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • C# 超高速高性能写日志 代码开源 - Emrys5

    C# 超高速高性能写日志 代码开源 - Emrys5

    2017-04-12 12:10

  • 开源 免费使用 打包下载自行部署 :升讯威 周报系统 - sheng.chao

    开源 免费使用 打包下载自行部署 :升讯威 周报系统 - sheng.chao

    2017-03-28 13:00

  • 期待微软平台即服务技术Service Fabric 开源 - 张善友

    期待微软平台即服务技术Service Fabric 开源 - 张善友

    2017-03-27 18:00

  • GitHub开源:升讯威ADO.NET增强组件 sheng.ADO.NET.Plus V1.3 - sheng.chao

    GitHub开源:升讯威ADO.NET增强组件 sheng.ADO.NET.Plus V1.3 - shen

    2017-03-22 13:02

网友点评
H