C# - 使用fleck客户端

时间:2015-12-01 10:41:15

标签: c# ssl

我正在尝试使用Fleck lib建立TLS / SSL websocket连接。

https://github.com/statianzo/Fleck(0.9.8.25) 现在我得到了服务器的启动。 但是当客户端连接时,我收到以下消息。

28-02-2014 19:16:15 [Info] Server started at wss://localhost:8081
28-02-2014 19:18:51 [Debug] Client connected from 127.0.0.1:62543
28-02-2014 19:18:51 [Debug] Authenticating Secure Connection
28-02-2014 19:18:52 [Debug] 0 bytes read. Closing.
anybody got an idea of what im doing wrong ?

浏览器:Chrome,版本:33.0.1750.117

 class Program
    {
        private static UTF8Encoding encoding = new UTF8Encoding();
        static void Main(string[] args)
        {
            Connect("wss://localhost:8081").Wait();
        }

        public static async Task Connect(string uri)
        {
            Thread.Sleep(1000);
            ClientWebSocket webSocket = null;
          X509Certificate2 certif = new X509Certificate2(@"QtrackerServer_TemporaryKey.pfx", "jihedgam");
          webSocket.Options.ClientCertificates.Equals(certif);
            try
            {
                webSocket = new ClientWebSocket();
               
                await webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
                await Task.WhenAll(Receive(webSocket), Send(webSocket));
            }
            catch (Exception ex)
            {

                Console.WriteLine("Exeption: {0}", ex);
               
            }
            finally
            {
                if (webSocket != null)
                    webSocket.Dispose();
                Console.WriteLine();
                Console.WriteLine("WebSocket closed.");
            }
        }
        private static async Task Send(ClientWebSocket webSocket)
        {
            while (webSocket.State == WebSocketState.Open)
            {
                Console.WriteLine("Write some to send over to server..");
                string stringtoSend = Console.ReadLine();
                byte[] buffer = encoding.GetBytes(stringtoSend);
                await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, false, CancellationToken.None);
                Console.WriteLine("Send:     " + stringtoSend);
                await Task.Delay(1000);
            }
        }
        private static async Task Receive(ClientWebSocket webSocket)
        {
            byte[] buffer = new byte[1024];
            while (webSocket.State == WebSocketState.Open)
            {
                buffer = new byte[1024];
                var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
                if (result.MessageType == WebSocketMessageType.Close)
                {
                    await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
                }
                else
                {
                    Console.WriteLine("Recive:  " + Encoding.UTF8.GetString(buffer).TrimEnd('\0'));

                }
            }
        }
  }

0 个答案:

没有答案