doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
745 stars 128 forks source link

Socket connect to https #212

Closed vikoms closed 3 years ago

vikoms commented 3 years ago

Hello, I'm currently developing an Android and iOS application using Xamarin Forms, and when I want to connect my application to a socket, I get an error like this:

System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Mono.Btls.MonoBtlsException: Ssl error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/boringssl/ssl/handshake_client.c:1132 at Mono.Btls.MonoBtlsContext.ProcessHandshake () [0x00042] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Btls/MonoBtlsContext.cs:220 at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status, System.Boolean renegotiate) [0x000da] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:715 at (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus,bool) at Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:289 at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken) [0x000fc] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:223 --- End of inner exception stack trace --- at Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication (System.Boolean runSynchronously, Mono.Net.Security.MonoSslAuthenticationOptions options, System.Threading.CancellationToken cancellationToken) [0x0025c] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:310 at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken, System.Net.WebSockets.ClientWebSocketOptions options) [0x0014f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs:106 at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken, System.Net.WebSockets.ClientWebSocketOptions options) [0x00385] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs:150 at System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x000d1] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/ClientWebSocket.cs:157 at SocketIOClient.WebSocketClient.ClientWebSocket.ConnectAsync (System.Uri uri) [0x000d2] in <3d7cf19a8621482a8473b5923e1bf013>:0 at SocketIOClient.SocketIO.ConnectCoreAsync (System.Boolean allowedRetryFirstConnection, System.Action connectBefore) [0x00250] in <3d7cf19a8621482a8473b5923e1bf013>:0 at SocketIOClient.SocketIO.ConnectAsync () [0x0006e] in <3d7cf19a8621482a8473b5923e1bf013>:0 at Cerdas.ViewModels.Authentication.LoginViewModel.LoginAction (System.Object obj) [0x00075] in E:\Xamarin Project\CERDAS\Cerdas\Cerdas\ViewModels\Authentication\LoginViewModel.cs:291 [id.bts.cerdas] Explicit concurrent copying GC freed 25457(1666KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 3MB/6MB, paused 164us total 66.391ms

and this is my code to connect the socket: image

at first, when the server uses HTTP for this socket it runs properly, but when the server changes to HTTPS I get an error like this both on Android & iOS

Thank you for your attention, have a nice day

cc : @doghappy

doghappy commented 3 years ago

try it

var client = new SocketIO("http://localhost:11000/");
client.ClientWebSocketProvider = () =>
{
    var clientWebSocket = new DefaultClientWebSocket
    {
        ConfigOptions = o =>
        {
            var options = o as ClientWebSocketOptions;
            options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
            {
                Console.WriteLine("SslPolicyErrors: " + sslPolicyErrors);
                return true;
            };
        }
    };
    return clientWebSocket;
};
vikoms commented 3 years ago

Hi, @doghappy thanks for answering my question. i have done what you suggested but i got error like thisi think you converted json wrong. thanks again, have a nice day :)

'2' is invalid after a single JSON value. Expected end of data. LineNumber: 0 | BytePositionInLine: 95. Stacktrace => at System.Text.Json.ThrowHelper.ThrowJsonReaderException (System.Text.Json.Utf8JsonReader& json, System.Text.Json.ExceptionResource resource, System.Byte nextByte, System.ReadOnlySpan1[T] bytes) [0x00009] in :0 at System.Text.Json.Utf8JsonReader.ConsumeNextToken (System.Byte marker) [0x00051] in :0 at System.Text.Json.Utf8JsonReader.ConsumeNextTokenOrRollback (System.Byte marker) [0x00024] in :0 at System.Text.Json.Utf8JsonReader.ReadSingleSegment () [0x00126] in :0 at System.Text.Json.Utf8JsonReader.Read () [0x00008] in :0 at System.Text.Json.JsonDocument.Parse (System.ReadOnlySpan1[T] utf8JsonSpan, System.Text.Json.JsonReaderOptions readerOptions, System.Text.Json.JsonDocument+MetadataDb& database, System.Text.Json.JsonDocument+StackRowStack& stack) [0x0025d] in <cb68364b029e41c3bf425990b94c94d9>:0 at System.Text.Json.JsonDocument.Parse (System.ReadOnlyMemory1[T] utf8Json, System.Text.Json.JsonReaderOptions readerOptions, System.Byte[] extraRentedBytes) [0x00037] in :0 at System.Text.Json.JsonDocument.Parse (System.ReadOnlyMemory1[T] json, System.Text.Json.JsonDocumentOptions options) [0x00064] in <cb68364b029e41c3bf425990b94c94d9>:0 at System.Text.Json.JsonDocument.Parse (System.String json, System.Text.Json.JsonDocumentOptions options) [0x00014] in <cb68364b029e41c3bf425990b94c94d9>:0 at SocketIOClient.Transport.TransportRouter.ConnectAsync () [0x0018b] in <9f19149de3064b76ad7214cb3c65342b>:0 at SocketIOClient.SocketIO.ConnectAsync () [0x000ce] in <9f19149de3064b76ad7214cb3c65342b>:0 at SocketIOClient.SocketIO.ConnectAsync () [0x00264] in <9f19149de3064b76ad7214cb3c65342b>:0 at SocketIOTest.MainPage.Button_Clicked (System.Object sndr, System.EventArgs e) [0x00184] in E:\Xamarin Project\socketiotest\SocketIOTest\SocketIOTest\SocketIOTest\MainPage.xaml.cs:77

doghappy commented 3 years ago

Your server is using socket.io server v2.x

SocketIOClient v3.x no longer supports socket.io server v2.x.(Next, I will consider adding support for it)

If you cannot upgrade your server, you need to use SocketIOClient v2.x.

This is an example of SocketIOClient v2.3.1

        static async Task Main(string[] args)
        {
            var uri = new Uri("http://localhost:11003");

            var socket = new SocketIO(uri, new SocketIOOptions
            {
                Query = new Dictionary<string, string>
                {
                    {"token", "V3" }
                },
            });

            var transport = socket.Socket as SocketIOClient.WebSocketClient.ClientWebSocket;
            transport.Config = options =>
            {
                options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
                {
                    Console.WriteLine("SslPolicyErrors: " + sslPolicyErrors);
                    return true;
                };
            };
            socket.OnConnected += Socket_OnConnected;

            await socket.ConnectAsync();

            Console.ReadLine();
        }
vikoms commented 3 years ago

issue closed, I have spoken to the Back-End team to upgrade the socket io server to v4.x

and if you have an error on the certificate I solve it by adding httpClientHandler to the socket.HttpClient property

thank your for @doghappy, have a nice day 😊😊

FireBird4 commented 3 years ago

How did you add the httpClientHandler? Can you share some code?