locked
Having trouble calling a websocket using Aspnetcore Signalr Client 5.0.2 RRS feed

  • Question

  • User1031775717 posted

    Hi,

    I'm trying to call a websocket service located at wss://socket.btcmarkets.net/v2 using Aspnetcore Signalr Client 5.0.2.

    I know the service works as if I browse to https://www.wss-websocket.net/ put in the websocket url of wss://socket.btcmarkets.net/v2 and then connect and send the message {"clientType":"webclient","marketIds":["BTC-AUD"],"channels":["tick"],"messageType":"subscribe"} then the data starts to stream back.

    The code I tried to use to do the same in .net is this:

                var subscribeRequest = new RequestSubscribe() { Channels = new List<string> { "tick" }, MarketIds = new List<string> { "BTC-AUD" }, MessageType = "subscribe", ClientType = "webclient" };
                var subText = JsonConvert.SerializeObject(subscribeRequest);
    
                var hubConnection = new HubConnectionBuilder()
                        .WithUrl("wss://socket.btcmarkets.net/v2", 
                        options => { 
                            options.SkipNegotiation = true;
                            options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
                        })
                        .ConfigureLogging(logging =>
                        {
                            // Log to the Console
                            logging.AddConsole();
                            // This will set ALL logging to Debug level
                            logging.SetMinimumLevel(LogLevel.Trace);
                        })
                        .Build();
    
    
                hubConnection.On("tick", Tick);
    
                await hubConnection.StartAsync();
    
                await hubConnection.SendAsync("", subText);
            }
    
            public async Task Tick()
            {
    
            }
    

    I would really appreciate if someone could tell me what i'm doing wrong.  When I run the code the following log is produced:

    dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[40]
          Registering handler for client method 'tick'.
    trce: Microsoft.AspNetCore.SignalR.Client.HubConnection[42]
          Waiting on Connection Lock in StartAsyncInner (/_/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs:249).
    trce: Microsoft.AspNetCore.SignalR.Client.HubConnection[80]
          The HubConnection is attempting to transition from the Disconnected state to the Connecting state.
    dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[41]
          Starting HubConnection.
    dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[1]
          Starting HttpConnection.
    dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[7]
          Starting transport 'WebSockets' with Url: wss://socket.btcmarkets.net/v2.
    info: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[1]
          Starting transport. Transfer mode: Text. Url: 'wss://socket.btcmarkets.net/v2'.
    dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[19]
          Started transport.
    dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[18]
          Transport 'WebSockets' started.
    info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3]
          HttpConnection Started.
    info: Microsoft.AspNetCore.SignalR.Client.HubConnection[24]
          Using HubProtocol 'json v1'.
    dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[28]
          Sending Hub Handshake.
    dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[13]
          Received message from application. Payload size: 32.
    fail: Microsoft.AspNetCore.SignalR.Client.HubConnection[82]
          The handshake timed out after 15 seconds.
          System.OperationCanceledException: The operation was canceled.
             at System.Threading.CancellationToken.ThrowOperationCanceledException()
             at System.Threading.CancellationToken.ThrowIfCancellationRequested()
             at System.IO.Pipelines.Pipe.GetReadAsyncResult()
             at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
             at Microsoft.AspNetCore.SignalR.Client.HubConnection.HandshakeAsync(ConnectionState startingConnectionState, CancellationToken cancellationToken)
    fail: Microsoft.AspNetCore.SignalR.Client.HubConnection[43]
          Error starting connection.
          System.OperationCanceledException: The operation was canceled.
             at System.Threading.CancellationToken.ThrowOperationCanceledException()
             at System.Threading.CancellationToken.ThrowIfCancellationRequested()
             at System.IO.Pipelines.Pipe.GetReadAsyncResult()
             at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
             at Microsoft.AspNetCore.SignalR.Client.HubConnection.HandshakeAsync(ConnectionState startingConnectionState, CancellationToken cancellationToken)
             at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore(CancellationToken cancellationToken)
    dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[4]
          Disposing HttpConnection.
    info: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[6]
          Transport is stopping.
    dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[8]
          Send loop stopped.
    dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[2]
          Transport stopped.
    info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[6]
          HttpConnection Disposed.
    trce: Microsoft.AspNetCore.SignalR.Client.HubConnection[80]
          The HubConnection is attempting to transition from the Connecting state to the Disconnected state.
    trce: Microsoft.AspNetCore.SignalR.Client.HubConnection[20]
          Releasing Connection Lock in StartAsyncInner (/_/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs:282).
    dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[5]
          Receive loop canceled.
    dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[4]
          Receive loop stopped.

    Much thanks in advance

    Saturday, January 16, 2021 1:10 PM

All replies

  • User-1330468790 posted

    Hi auschucky,

     

    Is 'v2' the hub name? 

    SignalR is like a 'helper' library for WebSockets and introduces the "hub" concept, which allows both the server and client to call each other's methods. You have to follow its rules so that you could use it to connect client with server. It is a different way of using WebSocket directly.

    The common steps to use SignalR:

    • Add the SignalR client library.
    • Create a SignalR hub.
    • Configure the project to use SignalR.
    • Add code that sends messages from any client to any/some connected clients.

    Could you please confirm if you have configured all of the necessary things? 

     

    Best regards,

    Sean

    Monday, January 18, 2021 8:59 AM
  • User1031775717 posted

    Hi Sean,

    Thanks for your message.

    I installed fiddler and learnt the Signal R was getting stuck in the handshake negotiation stage trying to establish that the protocol was json.  The service I was trying to consume is a generic websocket service that doesn't understand the signalr handshake process so didn't respond to the handshake request and hence the timeout.

    From that finding I reverted to using the clientwebsocket class which has lower level control over what messages are sent and received.  This fixed the problem but means you need to implement lower level message reading, so not ideal.

    From what I understood the handshake in signal r can not be bypassed so I thought it wasn't appropriate for the service I was trying to consume.  If there's further advice you could give it'd be greatly appreciated.

    Kind regards

    Tuesday, February 2, 2021 9:38 AM
  • User-1330468790 posted

    Hi auschucky,

      

    Have you had a chance to look into this documentation:

    WebSockets support in ASP.NET Core

    It illustrates how to get started with WebSockets in ASP.NET Core and provides you with samples.

     

    Best regards,

    Sean

    Wednesday, February 3, 2021 5:46 AM