Websockets in C# for UWP ping pong

Victor Denisenko 76 Reputation points
2020-10-13T17:09:39.637+00:00

I use this Microsoft example to build UWP app for Windows IoT Core on C#. I use npm ws client for the browser and the same npm ws for Node.js server. The Server uses standard ping (opcode 0x9) and pong frames.
How to get "ping" from the server in UWP app and how to answer "pong" to it? I cannot find any C# classes to do that.

Windows for IoT
Windows for IoT
A family of Microsoft operating systems designed for use in Internet of Things (IoT) devices.
381 questions
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Michael Xu 336 Reputation points
    2020-10-14T09:46:39.99+00:00

    Thanks for providing the details. I have tested with the Fleck library in my last post. If i send the ping frame, the ws server invokes the message callback(incoming) while not ping callback(heartbeat). In another hand, I also tried with a package named Chilkat.WebSocket, it works fine.

                   Chilkat.WebSocket ws = new Chilkat.WebSocket();
    
                    ws.PongAutoConsume = true;
    
                    Chilkat.Rest rest = new Chilkat.Rest();
                    bool success = rest.Connect("localhost", 8080, false, false);
                    ws.UseConnection(rest);
                    ws.AddClientHeaders();
                    string responseBodyIgnored = rest.FullRequestNoBody("GET", "/wsChilkatEcho.ashx");
                    success = ws.ValidateServerHandshake();
                    if (success != true)
                    {
                        Console.WriteLine(ws.LastErrorText);
                        return;
                    }
    
                    bool finalFrame = false;
                    success = ws.SendFrame("This is the 1st frame\r\n", finalFrame);
                    if (success != true)
                    {
                        Console.WriteLine(ws.LastErrorText);
                        return;
                    }
    
                   // Let's send a Ping frame here...
                   success = ws.SendPing("This is a ping");
                   if (success != true)
                   {
                         Console.WriteLine(ws.LastErrorText);
                         return;
                   }
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Michael Xu 336 Reputation points
    2020-10-14T02:58:20.773+00:00

    @Victor Denisenko , a ping or pong is just a regular frame, but it's a control frame. So you can implement getting ping frame or sending pong frame in your websocket server.I don't know how you hosted the websocket server in your UWP app, or what library of websocket server you used. I found out a repo which includes the implements. You can refer to the open source.


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Victor Denisenko 76 Reputation points
    2020-10-14T06:09:09.857+00:00

    Thank you, Michael, but I need to refine my question. I use a socket server that has implemented getting ping and pong frames. The library for the socket server is https://www.npmjs.com/package/ws.
    For client implementation I use Windows.Networking.Sockets Namespace. This namespace contains MessageWebSocket Class and I use it for communication with the server. I am sending the message as

     using (var dataWriter = new DataWriter(messageWebSocket.OutputStream))
                            {
                                dataWriter.WriteString(message);
                                await dataWriter.StoreAsync();
                                dataWriter.DetachStream();
                            }
    

    Websockets server obtains this message in javascript event handler

    ws.on('message', function incoming(data) {});
    

    But, as for ping, the server is receiving ping in another event:

    client.on('ping', function heartbeat() {});
    

    So, my question is, which method I can use to send the ping for receiving it by the server in ping event client.on('ping'...)?
    You advise me to implement ping-pong myself, but what classes I need to use to send ping and receive pong in RFC-6455 (websockets standard) compatible way? You advise me Fleck, but this is a server (I need client ) and it works on Windows 7 and Server 2008 hosts, but not on Windows 10 IoT Core, that needs for me.

    0 comments No comments

  3. Victor Denisenko 76 Reputation points
    2020-10-14T18:02:41.257+00:00

    Thank you for your suggestion. It needs some time to study if Chilkat.WebSocket works on Windows 10 IoT Core. Now I understand that Windows.Networking.Sockets Namespace doesn't contain the ping-pong methods. Because I have already developed a big project using this namespace, it will be better for me to embed my own non-standard ping-pong procedure instead of using Chilkat.WebSocket.
    So, I am closing this issue.

    0 comments No comments