0

Currently I am using WebSocket-Sharp. I am able to connect to the server through my application and I am able to send a 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;
    }
}
smn.tino
  • 2,272
  • 4
  • 32
  • 41

1 Answers1

0

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:

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");
    }
}
vhr
  • 1,528
  • 1
  • 13
  • 21
  • Hi , I have edited my code in my question, this is what i used and another button event to send one request over, thats all. As for nf.Notify, i do not have the .dll for that reference. – Shaun Tan Jun Kai Jul 17 '18 at 09:09
  • Hi,I have did your suggestion, however because I am running this on the same thread as the textbox control thread, I can't update the textbox because of thread issues. – Shaun Tan Jun Kai Jul 19 '18 at 01:20
  • see my updated answer, it should resolve your cross-thread issue – vhr Jul 19 '18 at 06:36
  • oh it works! Sorry for troubling you, but currently whenever I send a request and receive a response, the response overwrites the older response. Is there any way I can prevent the overwrite? – Shaun Tan Jun Kai Jul 19 '18 at 07:30
  • great news, also please don't forget to mark my answer:) – vhr Jul 19 '18 at 07:44
  • Thanks a alot! Marked :) however for my next question, I reckon I should ask on another question would be better right? – Shaun Tan Jun Kai Jul 19 '18 at 07:54