Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have try some simple code to receive message respond from server connecting through web socket but i cant receive any message from server. But when i test it using online web socket connection it shows the message. I want async websocket for client that receive every message from server. I have write some simple code in c# using websocket-sharp but i dont have any idea on it.

What I have tried:

using (ws = new WebSocket(webSocketUri))
{



    ws.Connect();
    ws.EmitOnPing = true;

    ws.OnMessage += (sender, e) =>
    {
        if (e.IsPing == true)
        {
            string data = e.Data.ToString();
            return;
        }
    };

}
Posted
Updated 30-Jun-20 20:18pm

1 solution

using is a very specific keyword in C#, it's used to control teh disposal of items when they ar efinished with.
Writing this:
C#
using (MyClass mc = new MyClass)
   {
   ...
   }
Is the equivelant of writing this:
C#
{
MyClass mc;
try
   {
   mc = new MyClass();
   ...
   }
finally
   {
   mc.Dispose();
}
When your app reaches the end of the using block and we goes out of scope is it Disposed automatically - so any open connection is Closed, any delegates are cleared, the memory is returned to the heap, and the WebSocket no longer exists at all.
So it can't receive anything - it isn't open, it doesn't exist, and it doesn't have any handler methods!

Make it a class level private variable, and it should start to work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900