2

I am using websocket4net to create a WebSocket client in C# and communicate with a server.

I am able to send data, but not able to receive the reply that the server is sending back. The Client_MessageReceived is not triggered at all. I am able to see the reply in WireShark though.

The code I am using is as below :

class CommandRPC
{
    public String jsonrpc;
    public String method;
    public Parameter @params;
    public String id;
}    

class Parameter
{
    public String cmd;
}

class Program
{
    static WebSocket Client;

    static CommandRPC command;

    static void Main(string[] args)
    {
        Client = new WebSocket("ws://10.131.35.32/DIAG");
        Client.Opened += new EventHandler(Client_Opened);
        Client.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(Client_Error);
        Client.Closed += new EventHandler(Client_Closed);
        Client.MessageReceived += new EventHandler<MessageReceivedEventArgs>(Client_MessageReceived);
        Client.DataReceived += new EventHandler<DataReceivedEventArgs>(Client_DataReceived);


        command = new CommandRPC();
        command.jsonrpc = "2.0";
        command.method = "SystemExec";
        command.@params = new Parameter();
        command.@params.cmd = "ls";
        command.id = "8302144";

        Client.Open();
        System.Threading.Thread.Sleep(2000);
        Console.WriteLine(Client.State);
        Console.ReadLine();
    }


    static void Client_DataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine("DATA received : " + e.Data);
    }

    static void Client_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        Console.WriteLine("Message received : " + e.Message);            
    }

    static void Client_Closed(object sender, EventArgs e)
    {
        Console.WriteLine("Websocket closed");
    }

    static void Client_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
    {
        Console.WriteLine("Error thrown trying to open websocket : " + e.Exception.Message);
    }

    static void Client_Opened(object sender, EventArgs e)
    {
        var json = new JavaScriptSerializer().Serialize(command);            
        Console.WriteLine("Websocket connection open.");

        for (int i = 1; i <= 2; i++)
        {
            Console.WriteLine("Sending Json: " + i.ToString());
            Client.Send(json);
            System.Threading.Thread.Sleep(500);
        }   
    }
}
wonea
  • 4,783
  • 17
  • 86
  • 139
Manoj
  • 5,011
  • 12
  • 52
  • 76
  • A bit late to the dance but I am having the exact same problem. How did you resolve it in the end? – Nick Mar 21 '16 at 15:04

3 Answers3

5

Use

   Task.Delay(300) //instead of Thread.Sleep(2000)

and it should do the trick

Liviu Sosu
  • 1,381
  • 3
  • 15
  • 26
  • How this can affect message receiving? – Ilya Luzyanin Sep 04 '14 at 13:50
  • 2
    when you write Thread.Sleep(int milliseconds) the whole thread of the application stops. While Task wil allow the thread of websocket to run in background. I'm not 100% sure but I've encountered the same problem and that is how I solve it. – Liviu Sosu Sep 04 '14 at 14:12
2

I am not familiar with websocket4net but it could be that it is running on the same thread and Thread.Sleep blocks the client from receiving the reply.

It is also possible that the reply comes while Console.ReadLine(); is blocking the execution and therefore it does not reach Client_MessageReceived.

A third option would be that the content of the reply is empty. In that case the WebSocket will not fire the event. I am saying that based on looking at the code for WebSocket here: http://websocket4net.codeplex.com/SourceControl/latest#WebSocket4Net/WebSocket.cs

Tal Z
  • 3,170
  • 17
  • 30
  • the reply is not empty. Would doing event registeration and Client.Open in a separate thread cause the Client_MessageReceived to be fired? I will try that, but is there any other more correct way of doing this? – Manoj Oct 15 '13 at 12:56
  • I suggest waiting for the reply in a different thread. What you're doing now is calling `Thread.Sleep` right after you send `json`, then you call `Send` and `Thread.Sleep` again, and immediately after that the execution is blocked by `Console.ReadLine`. It just seems that there's no time for the `client` to process the reply. I'm not saying that based on working with WebSocket, but based on working with sockets in general, So I can't be sure what the correct way is. It really depends on how `WebSocket` is implemented. – Tal Z Oct 15 '13 at 13:58
  • I just tested your code with a a different server and it actually works. `Client = new WebSocket("ws://echo.websocket.org/");` It sends 2 messages and receives both of them. Can you maybe post the reply that you're getting with WireShark? – Tal Z Oct 15 '13 at 14:15
  • When I try to open the connection with ws://echo.websocket.org/, I get the following error : Error thrown trying to open websocket : unknown server protocol version . I am in office and probably some network settings is preventing me from doing this. I will go home and try this shortly. – Manoj Oct 15 '13 at 14:45
0

I'm very late to the party, but just encountered the same issue with WebSocket4Net.

There was an issue raised on github that may explain why the _send message is never getting sent: https://github.com/kerryjiang/WebSocket4Net/issues/170

This manifested for me when sending multiple connection requests. Each request would wait for an acknowledgement before sending an additional request and, of course, eventually the acknowledgement would never arrive and block the application. The only thing that has worked thus far is a Thread.Sleep call just prior to sending.

jslmsca
  • 196
  • 1
  • 14