V
V
Vanes Ri_Lax2015-10-26 13:37:10
C++ / C#
Vanes Ri_Lax, 2015-10-26 13:37:10

Websocket Why doesn't the OnClose method work?

Hello, I am writing a ws client in c#
For the client, I used this: https://github.com/sta/websocket-sharp
I did everything according to the documentation, everything works well, but for some reason, if you take and specifically turn off the server, then nothing on the client not happening.
Here's how I did it:

public static WebSocket ws;
private void wsConnector()
        {
            addLog("Подключаемся");
            using (ws = new WebSocket("ws://127.0.0.1:3322"))
            {
                ws.OnMessage += (sender, e) => onMessage(e.Data);
               
                ws.OnOpen += (sender, e) => onOpen();
                ws.OnError += (sender, e) => onError();
                ws.OnClose += (sender, e) => {
                   
                    addLog("Отключаемся от сервера");
                };

            }
            ws.Connect();
        }
public Form1()
        {
            InitializeComponent();
            wsConnector();
        }

Maybe I don’t understand something, but it seems to me that if the client loses contact with the server, the OnClose event should fire, but it does not.
What am I doing wrong?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Mozhaykin, 2015-10-26
@smozhaykin

It did not work with websocket-sharp, but judging by your code, by the time ws.Connect your socket should already be invalid, because. when exiting the using block, Dispose is called...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question