1

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?

7
  • Since it says "host has failed to respond 202.160.125.44:80", have you check that you have access to that ip and port? Sounds like there could be some kind of firewall issues? Please validate that you have access to that ip and port. Commented Apr 27, 2016 at 6:13
  • How to check I have access to that ip and port? And how to solve the firewall issues? Commented Apr 27, 2016 at 7:08
  • If you know how to use telnet you could try that and just try to connect and see that it says. If not use a web browser and just surf to: 202.160.125.44:80. (You dont really need to specify :80 since that is the default port for http). It is possible that you get an error what is interesting is what kind of error, is the error that you can't connect it could be a firewall issue, if there is something else it can be ok. Try it out and let us know. Commented Apr 27, 2016 at 7:30
  • If it is indeed a firewall issue we need to know more about the server. It could be that the server has not opened up port 80 for outside access? Commented Apr 27, 2016 at 7:31
  • Telnet failed. So it's the firewall issue that doesn't open port 80 for me to access? Commented Apr 27, 2016 at 7:51

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.