Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用websocket sharp创建客户端?_Websocket_Websocket Sharp_Clientwebsocket - Fatal编程技术网

如何使用websocket sharp创建客户端?

如何使用websocket sharp创建客户端?,websocket,websocket-sharp,clientwebsocket,Websocket,Websocket Sharp,Clientwebsocket,我正在使用ClientWebSocket订阅REST服务,但希望能够使用websocket sharp static async void MonitorISY(string IPAddress, string userName, string password, IMessageWriter writer) { ClientWebSocket client = new ClientWebSocket(); client.Optio

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

我认为最好的例子是

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

html是基于

        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();