【问题标题】:How do you create a client using websocket-sharp?如何使用 websocket-sharp 创建客户端?
【发布时间】:2017-05-29 18:32:18
【问题描述】:

我正在使用 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();

【问题讨论】:

    标签: websocket websocket-sharp clientwebsocket


    【解决方案1】:

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

    虽然我确实需要进行一些微小的调整才能让它在 Visual Studio 2017 Enterprise 中编译。

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

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2012-10-02
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多