4

我正在使用ClientWebSocket订阅 REST 服务,但希望能够使用 websocket-sharp 代替。

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<byte>(buffer), CancellationToken.None);
                var resultJson = (new UTF8Encoding()).GetString(buffer);
                writer.WriteLn(resultJson);
                writer.WriteLn();
            }
        }

这是我的 websocket-sharp 尝试。当ws.Connect(); 被调用时,我收到Not a WebSocket 握手响应错误消息。在工作代码中,我必须设置 Origin、SubProtocol 和 RequestHeader。我认为我为 websocket-sharp 代码正确地做到了这一点,但请求标头除外。我一直找不到指定身份验证的工作示例。

        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) => ws.Send("Hi, there!");

            ws.OnMessage += (sender, e) =>
                nf.Notify(
                  new NotificationMessage
                  {
                      Summary = "WebSocket Message",
                      Body = !e.IsPing ? e.Data : "Received a ping.",
                      Icon = "notification-message-im"
                  }
                );

            ws.OnError += (sender, e) =>
                nf.Notify(
                  new NotificationMessage
                  {
                      Summary = "WebSocket Error",
                      Body = e.Message,
                      Icon = "notification-message-im"
                  }
                );

            ws.OnClose += (sender, e) =>
                nf.Notify(
                  new NotificationMessage
                  {
                      Summary = String.Format("WebSocket Close ({0})", e.Code),
                      Body = e.Reason,
                      Icon = "notification-message-im"
                  }
                );

            ws.Connect();
4

1 回答 1

0

我认为最好的例子是https://github.com/sta/websocket-sharp/tree/master/Example3

尽管我确实必须进行一些微小的调整才能让它在 Visual Studio 2017 Enterprise 中编译。

index.html 基于 http://www.websocket.org/echo.html

于 2019-06-07T02:04:11.000 回答