コード例 #1
0
ファイル: WebSocketClient.cs プロジェクト: librame/extensions
        public async Task StartAsync <TChannelHandler>(Func <Uri, TChannelHandler> channelHandlerFactory,
                                                       Action <IChannel> configureProcess, string host = null, int?port = null)
            where TChannelHandler : IChannelHandler
        {
            host = host.NotEmptyOrDefault(_clientOptions.Host);
            port = port.NotNullOrDefault(_clientOptions.Port);

            var builder = new UriBuilder
            {
                Scheme = _clientOptions.IsSsl ? "wss" : "ws",
                Host   = host,
                Port   = port.Value
            };

            if (!_clientOptions.VirtualPath.IsEmpty())
            {
                builder.Path = _clientOptions.VirtualPath;
            }

            IEventLoopGroup group = null;

            try
            {
                X509Certificate2 tlsCertificate = null;
                if (_clientOptions.IsSsl)
                {
                    var credentials = SigningCredentials.GetSigningCredentials(_clientOptions.SigningCredentialsKey);
                    tlsCertificate = credentials.ResolveCertificate();
                }

                var address          = IPAddress.Parse(builder.Uri.Host);
                var endPoint         = new IPEndPoint(address, builder.Uri.Port);
                var webSocketHandler = channelHandlerFactory.Invoke(builder.Uri);

                var channel = await WrapperFactory
                              .CreateTcp(_clientOptions.UseLibuv, out group)
                              .AddWebSocketHandler(tlsCertificate, webSocketHandler)
                              .ConnectAsync(endPoint, _clientOptions.RetryCount).ConfigureAwait();

                Logger.LogInformation($"Connect ip end point: {endPoint}");

                configureProcess.Invoke(channel);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.AsInnerMessage());
            }
            finally
            {
                await group.ShutdownGracefullyAsync(_clientOptions.QuietPeriod,
                                                    _clientOptions.TimeOut).ConfigureAwait();
            }
        }
コード例 #2
0
ファイル: FactorialClient.cs プロジェクト: librame/extensions
        public async Task StartAsync <TChannelHandler>(TChannelHandler channelHandler,
                                                       Action <IChannel> configureProcess, string host = null, int?port = null)
            where TChannelHandler : IChannelHandler
        {
            var address  = IPAddress.Parse(host.NotEmptyOrDefault(_clientOptions.Host));
            var endPoint = new IPEndPoint(address, port.NotNullOrDefault(_clientOptions.Port));

            IEventLoopGroup group = null;

            try
            {
                X509Certificate2 tlsCertificate = null;
                if (_clientOptions.IsSsl)
                {
                    var credentials = SigningCredentials.GetSigningCredentials(_clientOptions.SigningCredentialsKey);
                    tlsCertificate = credentials.ResolveCertificate();
                }

                var channel = await WrapperFactory
                              .CreateTcp(false, out group)
                              .AddFactorialHandler(tlsCertificate, channelHandler)
                              .ConnectAsync(endPoint, _clientOptions.RetryCount).ConfigureAwait();

                Logger.LogInformation($"Connect ip end point: {endPoint}");

                configureProcess.Invoke(channel);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.AsInnerMessage());
            }
            finally
            {
                group.ShutdownGracefullyAsync().Wait(_clientOptions.TimeOut);
            }
        }