VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C# ClientWebSocket使用详细

 #region ClientWebSocket

    readonly ClientWebSocket _webSocket = new ClientWebSocket();
    readonly CancellationToken _cancellation = new CancellationToken();

    public async void WebSocket() {
        try {
            //建立连接
            var url = "ws://121.40.165.18:8800";

            await _webSocket.ConnectAsync(new Uri(url), _cancellation);

            var bsend = new byte[1024];

            await _webSocket.SendAsync(new ArraySegment<byte>(bsend), WebSocketMessageType.Binary, true, _cancellation); //发送数据

            while (true) {
                var result = new byte[1024];

                await _webSocket.ReceiveAsync(new ArraySegment<byte>(result), new CancellationToken());//接受数据

                var lastbyte = ByteCut(result, 0x00);

                var str = Encoding.UTF8.GetString(lastbyte, 0, lastbyte.Length);

            }

        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }

   #endregion


相关教程