0

I am trying to connect to websocket api in this address : wss://ws.cex.io/ws/

If i use javascript code, with the WebSocket object, everything is fine.

If i use c# code, using ClientWebSocket class, when i try to connect i get "cant access remote server" error. with the same url i used on the js code exactly. i tried putting the url in the browser and got the same cannot reach server error, so i am pretty sure there is a header or something like this that i should use in my c# code. can anyone tell me what i am missing in my c# code ? thanks

js code (connection is made):

var ws_path = 'wss://ws.cex.io/ws';
socket = new WebSocket(ws_path);

c# code (cannot find sever)

        WebSocketWrapper CexSocket = new WebSocketWrapper("wss://ws.cex.io/ws");
        CexSocket.Connect();

    public WebSocketWrapper(string uri)
    {
        _ws = new ClientWebSocket();
        _ws.Options.KeepAliveInterval = TimeSpan.FromSeconds(20);
        _uri = new Uri(uri);
        _cancellationToken = _cancellationTokenSource.Token;
    }

    public WebSocketWrapper Connect()
    {
        ConnectAsync();
        return this;
    }

    public WebSocketWrapper OnConnect(Action<WebSocketWrapper> onConnect)
    {
        _onConnected = onConnect;
        return this;
    }

    private async void ConnectAsync()
    {
        await _ws.ConnectAsync(_uri, _cancellationToken); // here i get the error
        CallOnConnected();
        StartListen();
    }
Jiahao Cai
  • 1,222
  • 1
  • 11
  • 25
CUNAIM
  • 11
  • 2
  • Use a sniffer like wireshark or fiddler. Compare the http headers for java code with c# code. Usually the issue is missing headers. Also check if the c# is using http 1.0 (not 1.1). 1.1 doesn't work with Net Library and you must change default from 1.1 to 1.0, – jdweng Jul 15 '17 at 12:13
  • thank you. first thing i do use http 1.1. how can i change it ? – CUNAIM Jul 15 '17 at 14:08
  • second thing, the headers look like this : they both trying to connect to "CONNECT ws.cex.io:443 HTTP/1.1". js is using header "Host: ws.cex.io:443" and c# for some reason using header "Host: ws.cex.io" without the 443 port. could that be the probliem ? but both requests are identified as "CONNECT ws.cex.io:443 HTTP/1.1" so that is odd. – CUNAIM Jul 15 '17 at 14:12
  • another difference is that js is using TLS 1.2 and c# using TLS 1.0. could it be that the server only works with 1.2 ? – CUNAIM Jul 15 '17 at 14:15
  • the tls did it. i changed to 1.2 and it worked. thank you very much – CUNAIM Jul 15 '17 at 14:20
  • Try request.ProtocolVersion = HttpVersion.Version10; With 1.1 you get chunk mode and in Net there is no way of getting next chunk. – jdweng Jul 15 '17 at 15:15

0 Answers0