title: "Expected a JSON header message before receiving binary data" error using WebSocket-Sharp and IBM Watson TTS
body: <p>I'm trying to use IBM Watson's WebSocket interface so I can obtain the word timings of the audio. <a href="https://cloud.ibm.com/docs/services/text-to-speech?topic=text-to-speech-usingWebSocket" rel="nofollow noreferrer">Here is a link to the documentation.</a> The authentication part seems to be working fine. But I keep getting the error "Expected a JSON header message before receiving binary data." after sending my JSON message in the OnOpen method. I got the WebSocket-Sharp library from NuGet (called websocket-sharp-core). </p>
  
  <p>How do I solve this problem? </p>
  
  <pre class="lang-cs prettyprint-override"><code>using WebSocketSharp;
  using Newtonsoft.Json.Linq;
  using System.Net.Http;
  using System.Net;
  using System.Net.Http.Headers;
  
  async void GenVoice(string text, string dest)
  {
      Console.WriteLine("obtaining access token...");
      var dict = new Dictionary&lt;string, string&gt;();
      dict.Add("grant_type", "urn:ibm:params:oauth:grant-type:apikey");
      dict.Add("apikey", "&lt;my_api_key&gt;");
      var authClient = new HttpClient();
      authClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      var response = await authClient.PostAsync("https://iam.cloud.ibm.com/identity/token", new FormUrlEncodedContent(dict));
      var auth = JObject.Parse(await response.Content.ReadAsStringAsync());
      var accessToken = auth["access_token"].ToString();
      Console.WriteLine("access token is " + accessToken);
  
      var client = new WebSocket($"wss://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?access_token={accessToken}&amp;voice=en-US_MichaelV3Voice");
      client.Connect();
      client.OnOpen += OnOpen(client, text);
      client.OnMessage += OnMessage;
      client.OnError += (sender, e) =&gt;
      {
          Console.WriteLine(e.Message);
      };
      client.OnClose += (sender, e) =&gt;
      {
          Console.WriteLine($"closed; code={e.Code}; reason={e.Reason}; wasclean={e.WasClean}");
      };
  }
  
  System.EventHandler OnOpen(object sender, string text)
  {
      var client = (WebSocket)sender;
  
      var message = new JObject();
      message.Add("content-type", JToken.FromObject("application/json"));
      message.Add("accept", JToken.FromObject("*/*"));
      message.Add("text", JToken.FromObject(text));
      message.Add("timings", JToken.FromObject(new string[] { "words" }));
      client.Send(Encoding.UTF8.GetBytes(message.ToString()));
      Console.WriteLine("successfully opened socket");
      return null;
  }
  
  void OnMessage(object sender, MessageEventArgs e)
  {
      if (e.IsText)
      {
          Console.WriteLine("got a message of type string, it says: " + e.Data);
      }
      if (e.IsBinary)
      {
          Console.WriteLine("got a message of type binary");
      }
      if (e.IsPing) { Console.WriteLine("got a message of type ping"); }
  }
  </code></pre>