namespace TestApp { class Program { const string WSS_TEST_SERVER = "wss://echo.websocket.org"; static void Main(string[] args) { TestConnection(WSS_TEST_SERVER).GetAwaiter().GetResult(); } static async Task TestConnection(string server) { static async Task TestConnection(string server) { using (var ws = new System.Net.WebSockets.Managed.ClientWebSocket()) { //建立连接 await ws.ConnectAsync(new Uri(server), CancellationToken.None); const string msg = ""; var testMsg = new ArraySegment<byte>(Encoding.UTF8.GetBytes(msg)); await ws.SendAsync(testMsg, WebSocketMessageType.Text, true, CancellationToken.None); while (true) { var buffer = new ArraySegment<byte>(new byte[1024]); var read = await ws.ReceiveAsync(buffer, CancellationToken.None); var replyMsg = Encoding.UTF8.GetString(buffer.Array, 0, read.Count); } } } } } }
标签:string,Win7,net45,static,ws,var,new,ClientWebSocket From: https://www.cnblogs.com/anjingdian/p/17271424.html