简体   繁体   English

接收对客户端请求的服务器响应

[英]Receiving back server response towards clients request

Currently I am using WebSocket-Sharp. 目前,我正在使用WebSocket-Sharp。 I am able to connect to the server through my application and I am able to send a Client.Send(Move.HeadNod); 我可以通过我的应用程序连接到服务器,并且可以发送Client.Send(Move.HeadNod); to the server on button click. 单击按钮上的服务器。 However even though I declared 但是即使我宣布

private WebSocket client;
const string host="ws://localhost:80";
public Form1()
{
    InitializeComponent();
    client=new WebSocket(host);
    client.connect();
    Client.OnMessage+=client_OnMessage 
}

where: 哪里:

client_OnMessage(object sender,MessageEventArgs e)
{
    textbox1.text=convert.tostring(e);
    client.send(move.headleft);
}

I am still unable to get a response from the server and continue sending command afterwards. 我仍然无法从服务器获得响应,然后继续发送命令。 Edit 编辑

void Client_OnMessage(object sender,MessageEventArgs e)
{
    if(e.IsText)
    {
        edata=e.data;
        return;
    }
    else if(e.IsBinary)
    {
        Textbox1.Text=Convert.Tostring(e.RawData);
        return;
    }
}

This is the complete code that works on my machine. 这是在我的机器上可以使用的完整代码。 Put a break-point in both event handlers to see what happens. 在两个事件处理程序中都设置一个断点,以查看会发生什么。 Maybe your web socket server throws an exception and you just don't know it: 也许您的Web套接字服务器引发了异常,而您只是不知道:

public partial class Form1 : Form
{
    private readonly WebSocket _client;

    public Form1()
    {
        InitializeComponent();
        _client = new WebSocket("ws://echo.websocket.org");
        _client.OnMessage += Ws_OnMessage;
        _client.OnError += Ws_OnError;
        _client.Connect();
    }

    private void Ws_OnError(object sender, ErrorEventArgs e)
    {
    }

    private void Ws_OnMessage(object sender, MessageEventArgs e)
    {
        if (e.IsText)
        {           
            Invoke(new MethodInvoker(delegate () {
                textBox1.Text = e.Data;
            }));
        }
        else if (e.IsBinary)
        {
            Invoke(new MethodInvoker(delegate () {
                textBox1.Text = Convert.ToString(e.RawData);
            }));               

        }
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        _client.Send("Hi");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM