vtortola / WebSocketListener

A lightweight and scalable asynchronous WebSocket listener
http://vtortola.github.io/WebSocketListener
313 stars 85 forks source link

SSL Deserialize Message not working #140

Closed chachew closed 4 years ago

chachew commented 4 years ago

I am adding SSL to my websocket connection and i'm having issues on the server side deserializing the request sent over the socket. The frontend is Vue and it creates a websocket connection with a backend api that is VB.net.

Without SSL on the websocket the data is sent/received/deserialized just fine but once the websocket uses SSL i can no longer deserialize it on the backend, i'm sure because its now encrypted. How do i decrypt it?

Frontend:

socket.send(
    JSON.stringify({
      data: 'I want to send this'
    })
 );

Backend API:

Private Shared Async Function HandleConnectionAsync(ws As vtortola.WebSockets.WebSocket, cancellation As CancellationToken) As Task(Of Task())
        ' A request is made
        Try
            While ws.IsConnected AndAlso Not cancellation.IsCancellationRequested
                Dim msg As String = Await ws.ReadStringAsync(cancellation)

                If msg IsNot Nothing Then
                    Dim deserializedResult = New JavaScriptSerializer().Deserialize(Of Request)(msg) <!-----FAILS HERE
                End If
            End While
        Catch aex As Exception
            ws.Close() <!-- EXCEPTION GETS CAUGHT HERE
        Finally
            ws.Dispose()
        End Try
End Function
tkennedy13 commented 4 years ago

I'm having this same issue in C#.

In the connection handler I'm reading the message like this: var msg = await ws.ReadStringAsync( cancellation ).ConfigureAwait( false );

Without using an SSL certificate it works fine. I can read the message and processes it without a problem. When using SSL the return value is "Ep\0Q\0+..." and I am unable to deserialize it.

image

This is how the server is set up:

var options = new WebSocketListenerOptions()
{
    NegotiationQueueCapacity = 128,
    ParallelNegotiations = 16,
    PingTimeout = Timeout.InfiniteTimeSpan,
    PingMode = PingModes.LatencyControl,    
    UseNagleAlgorithm = false
};
var server = new WebSocketListener( new IPEndPoint( IPAddress.Any, 8181 ), options );
server.Standards.RegisterStandard( new WebSocketFactoryRfc6455() );
server.ConnectionExtensions.RegisterExtension( new WebSocketSecureConnectionExtension( certificate ) );            
server.StartAsync();
chachew commented 4 years ago

So before when i was having this issue i was using a developer cert using mkcert and i was having this problem. Now i'm using a real cert from godaddy and i'm no longer having this problem... I also followed the vtortola documentation to setup the certificate as mentioned.

tkennedy13 commented 4 years ago

Ahh, thanks! I am using a dev certificate too! Thank you for passing along the information!

chachew commented 4 years ago

Ahh, thanks! I am using a dev certificate too! Thank you for passing along the information!

Im not 100% sure this was my original issue because that was months ago, but everything is working now with a real cert.