'c# failed to connect to websocket server

I'm trying to connect to a websocket server.

The reference is here: reference.

However, it doesn't work like the example.

The code runs to OnError method.

The error is:

A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond 202.160.125.44:80

Here is my url:

https://<some websocket url>/notisocket/noti

Here is my code:

public class NotiSocket
{
    private WebSocketSharp.WebSocket m_webSocket;
    public NotiSocket()
    {
        m_webSocket = new WebSocketSharp.WebSocket("ws://<some websocket url>/notisocket/noti");
        m_webSocket.OnMessage += m_webSocket_OnMessage;
        m_webSocket.OnError += m_webSocket_OnError;
        m_webSocket.OnOpen += m_webSocket_OnOpen;
        m_webSocket.OnClose += m_webSocket_OnClose;
        m_webSocket.Connect();
        Console.ReadLine();
    }

    private void m_webSocket_OnClose(object sender, WebSocketSharp.CloseEventArgs e)
    {
        Console.WriteLine("Disconnected to websocket server.");
    }

    //error on websocket
    private void m_webSocket_OnError(object sender, WebSocketSharp.ErrorEventArgs e)
    {
        Console.WriteLine(e.Message);
    }

    //connected to websocket server
    private void m_webSocket_OnOpen(object sender, EventArgs e)
    {
        Console.WriteLine("Connected to websocket server");
        //m_webSocket.Send("Hello");
    }

    //receive msg from websocket server
    private void m_webSocket_OnMessage(object sender, WebSocketSharp.MessageEventArgs e)
    {
        Console.WriteLine(e.Data);
    }
}

I don't understand why have to change from http, htpps to ws, wss.

And what's wrong with the connection here?



Solution 1:[1]

The string given to WebSocketSharp.WebSocket() should be changed from

"ws://websocket.vndirect.com.vn/notisocket/noti"

to

"wss://websocket.vndirect.com.vn/notisocket/noti"

The reason you have to replace http: and https: with ws: and wss: is simply because the WebSocket client library (WebSocketSharp) checks the scheme part to determine which to use HTTP or HTTPS. If the WebSocket client library accepts http: and https: and internally interprets them as ws: and wss:, you don't have to replace the scheme part. In fact, there exists such a WebSocket client library.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Takahiko Kawasaki