title: Send a message and receive answer with WebSocket client in C#
body: <p>I am trying to implement a WebSocket client in C#. I created a class named WSConnection, which creates the WebSocketClient object and connects it to a server via a ConnectAsync() method. The handshake seems to work, as the socket state, after it, is set to "open".</p>
  
  <p>The problem starts when I try to send the first request to the server (I need to send a specific connect request to validate the connection and authenticate myself). When I try to do it, the program stays stuck on the ReceiveAsync() line in the Receive() method, which means, if I understood well, that no message is received from the server.</p>
  
  <p>Here is the code of the WSConnection class :</p>
  
  <pre><code>class WSConnection
  {
      private const string url = "ws://...";
      private string authenticationKey;
      private ClientWebSocket ws;
  
      public WSConnection(string key)
      {
          this.authenticationKey = key;
          this.ws = new ClientWebSocket();
      }
  
      public async Task Connect()
      {
          while(ws.State != WebSocketState.Open)
          {
              await ws.ConnectAsync(new Uri(url), CancellationToken.None);
              Console.WriteLine("Web socket : " + ws.State);
              Console.WriteLine("Sending connect request...");
  
              // Send the connect request and wait for the response
              await Send("connect");
              await Receive();
          }
      }
  
      public async Task Send(string type)
      {
          StringBuilder message = new StringBuilder();
  
          // We send a connect request
          if (type == "connect")
          {
              message.Append("type=" + HttpUtility.UrlEncode("authentication") + "&amp;");
              message.Append("authenticationKey=" + HttpUtility.UrlEncode(authenticationKey));
          }
          else
          {
              Console.WriteLine("No valid message type");
              return;
          }
  
          Console.WriteLine("Send message : " + message.ToString());
          var sendBuffer = new ArraySegment&lt;Byte&gt;(Encoding.UTF8.GetBytes(message.ToString()));
          await ws.SendAsync(sendBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
      }
  
      public async Task Receive()
      {
          ArraySegment&lt;byte&gt; receivedBytes = new ArraySegment&lt;byte&gt;(new byte[1024]);
          WebSocketReceiveResult result = await ws.ReceiveAsync(receivedBytes, CancellationToken.None);
          Console.WriteLine(Encoding.UTF8.GetString(receivedBytes.Array, 0, result.Count));
      }
  }
  </code></pre>
  
  <p>After it, I simply call the Connect() method with an await in the main. </p>
  
  <p>I suppose that I don't receive any message from the server because I don't send a correct one, so the question is how do I have to change the message I send ?</p>
  
  <p>Thank you by advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As Juan suggested, I used Fiddler to see the frames, and I got 2 corresponding frames :</p>
  
  <p><a href="https://i.stack.imgur.com/eyzaH.png" rel="nofollow noreferrer">First frame</a></p>
  
  <p><a href="https://i.stack.imgur.com/pqnHs.png" rel="nofollow noreferrer">Second frame</a></p>
  
  <p>Again, I can't give the URL I'm trying to reach, so I blured it and the rest, but for what I'm looking for, the host is ok.</p>