我正在尝试使用websocket-sharp与作为Windows窗体应用程序的专业演示者实例进行交互。我能够得到一个使用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();
}
答案 0 :(得分: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();