title: How do you create a client using websocket-sharp?
body: <p>I'm using <strong>ClientWebSocket</strong> to subscribe to a REST service but want to be able to use websocket-sharp instead.</p>
  
  <pre><code>static async void MonitorISY(string IPAddress, string userName, string password, IMessageWriter writer)
          {
              ClientWebSocket client = new ClientWebSocket();
              client.Options.AddSubProtocol("ISYSUB");
              client.Options.SetRequestHeader("Origin", "com.universal-devices.websockets.isy");
              var auth = Convert.ToBase64String(Encoding.Default.GetBytes(userName + ":" + password));
              client.Options.SetRequestHeader("Authorization", "Basic " + auth);
              await client.ConnectAsync(new Uri("ws://" + IPAddress + "/rest/subscribe"), CancellationToken.None);
  
              var receiveBufferSize = 512;
              byte[] buffer = new byte[receiveBufferSize];
  
              writer.Clear();
  
              while (true)
              {
                  var result = await client.ReceiveAsync(new ArraySegment&lt;byte&gt;(buffer), CancellationToken.None);
                  var resultJson = (new UTF8Encoding()).GetString(buffer);
                  writer.WriteLn(resultJson);
                  writer.WriteLn();
              }
          }
  </code></pre>
  
  <p>Here is my websocket-sharp attempt.  When <strong>ws.Connect();</strong> is called, I'm getting a <em>Not a WebSocket handshake response</em> error message.  In the working code, I have to setup the Origin, SubProtocol and the RequestHeader.  I think I'm doing that correctly for the websocket-sharp code, with the exception of the Request Header.  I've been unable to find a working example that specifies authentication.</p>
  
  <pre><code>        using (var nf = new Notifier())
          using (var ws = new WebSocket("ws://172.16.0.40/rest/subscribe", "ISYSUB"))
          {
              ws.Log.Level = LogLevel.Trace;
  
              var username = "user";
              var password = "pass";
              ws.Origin = "com.universal-devices.websockets.isy";
              ws.SetCredentials(username, password, true);
  
              ws.OnOpen += (sender, e) =&gt; ws.Send("Hi, there!");
  
              ws.OnMessage += (sender, e) =&gt;
                  nf.Notify(
                    new NotificationMessage
                    {
                        Summary = "WebSocket Message",
                        Body = !e.IsPing ? e.Data : "Received a ping.",
                        Icon = "notification-message-im"
                    }
                  );
  
              ws.OnError += (sender, e) =&gt;
                  nf.Notify(
                    new NotificationMessage
                    {
                        Summary = "WebSocket Error",
                        Body = e.Message,
                        Icon = "notification-message-im"
                    }
                  );
  
              ws.OnClose += (sender, e) =&gt;
                  nf.Notify(
                    new NotificationMessage
                    {
                        Summary = String.Format("WebSocket Close ({0})", e.Code),
                        Body = e.Reason,
                        Icon = "notification-message-im"
                    }
                  );
  
              ws.Connect();
  </code></pre>