1

我正在尝试使用 websocket-sharp 作为 Windows 窗体应用程序与 pro-presenter 实例交互。我能够得到一个使用 JavaScript 的类似项目。我的 .net 版本连接到套接字并运行 OnOpen 事件,然后立即运行 OnClose 事件。下面是我的代码块。OnClose 事件中的 e.Reason 为空白。我哪里错了?

    using (WebSocket ws = new WebSocket("ws://" + config.ComputerIP + ":" + config.Port + "/stagedisplay"))
    {
        ws.OnOpen += (sender, e) =>
        {
            // send connect string
            ws.Send("{\"pwd\":\"" + config.Password + "\",\"ptl\":610,\"acn\":\"ath\"}");
        };
        ws.OnMessage += (sender, e) =>
        {
                slide.ProcessMessage(e.Data);
                CurrentSlideLbl.Text = slide.CurrentSlide;
        };
        ws.OnClose += (sender, e) =>
            CurrentSlideLbl.Text = "reason:" + e.Reason;

        ws.OnError += (sender, e) =>
            CurrentSlideLbl.Text = "Socket encountered error: Closing socket" + e.Message;

        ws.Connect();
    }
4

1 回答 1

0

经过大量故障排除后,我想通了。这是我试图打开连接的方式。

        WebSocket ws = new WebSocket("ws://" + config.ComputerIP + ":" + config.Port + "/stagedisplay");
        ws.OnMessage += (sender, e) =>
        {
            slide.ProcessMessage(e.Data);
            CurrentSlideLbl.Invoke(new Action(() => CurrentSlideLbl.Text = slide.CurrentSlide));
        };
        ws.OnOpen += (sender, e) =>
        {
            // send connect string
            ws.Send(JsonConvert.SerializeObject(new { pwd = config.Password, ptl = 610, acn = "ath" }));
        };
        ws.OnClose += (sender, e) => 
        {
            CurrentSlideLbl.Text = "reason:" + e.Reason;
        };

        ws.OnError += (sender, e) => 
        {
            CurrentSlideLbl.Text = "Socket encountered error: Closing socket" + e.Message;
        };

        ws.Connect();
于 2018-08-28T21:45:19.500 回答