예제 #1
0
파일: sample11.cs 프로젝트: zzl1010/Docs
    static void Main(string[] args)
    {
        var connection = new HubConnection("http://www.contoso.com/");

        connection.AddClientCertificate(X509Certificate.CreateFromCertFile("MyCert.cer"));
        connection.Start().Wait();
    }
        public void Start()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("SafeguardEventListener");
            }
            CleanupConnection();
            _signalrConnection = new HubConnection(_eventUrl);
            if (_accessToken != null)
            {
                _signalrConnection.Headers.Add("Authorization", $"Bearer {_accessToken.ToInsecureString()}");
            }
            else
            {
                _signalrConnection.Headers.Add("Authorization",
                                               _apiKey != null
                        ? $"A2A {_apiKey.ToInsecureString()}"
                        : $"A2A {string.Join(" ", _apiKeys.Select(apiKey => apiKey.ToInsecureString()))}");
                _signalrConnection.AddClientCertificate(_clientCertificate.Certificate);
            }
            SignalrHubProxy = _signalrConnection.CreateHubProxy(NotificationHub);

            try
            {
                _signalrConnection.Received += HandleEvent;
                _signalrConnection.Closed   += HandleDisconnect;
                _signalrConnection.Start(_ignoreSsl ? new IgnoreSslValidationHttpClient() : new DefaultHttpClient())
                .Wait();
                _isStarted = true;
            }
            catch (Exception ex)
            {
                throw new SafeguardDotNetException("Failure starting SignalR", ex);
            }
        }
예제 #3
0
        public static void ConnectToService()
        {
            //TODO - remove this line after MSniper.com back to work
            return;

            while (true)
            {
                try
                {
                    if (!isConnected)
                    {
                        Thread.Sleep(10000);
                        _connection = new HubConnection(_msniperServiceUrl, useDefaultUrl: false);
                        X509Certificate2 sertifika = new X509Certificate2();
                        sertifika.Import(Properties.Resources.msvc);
                        _connection.AddClientCertificate(sertifika);
                        _msniperHub = _connection.CreateHubProxy("msniperHub");
                        _msniperHub.On <MSniperInfo2>("msvc", p =>
                        {
                            lock (locker)
                            {
                                autoSnipePokemons.Add(p);
                            }
                        });
                        _connection.Received     += Connection_Received;
                        _connection.Reconnecting += Connection_Reconnecting;
                        //_connection.Reconnected += Connection_Reconnected;
                        _connection.Closed += Connection_Closed;
                        _connection.Start().Wait();
                        //Logger.Write("connecting", LogLevel.Service);
                        _msniperHub.Invoke("Identity");
                        isConnected = true;
                    }
                    break;
                }
                catch (CaptchaException cex)
                {
                    throw cex;
                }
                catch (Exception)
                {
                    //Logger.Write("service: " +e.Message, LogLevel.Error);
                    Thread.Sleep(500);
                }
            }
        }
예제 #4
0
        private async void ConnectAsync()
        {
            connection = new HubConnection(SignalRClientConfig.ServerUrl);

            switch (AuthenticationType)
            {
            case AuthenticationType.None:
                break;

            case AuthenticationType.Cookie:
                Cookie returnedCookie;
                var    authResult = SignalRClientConfig.CookieAuthenticate.AuthenticateUser(out returnedCookie);
                if (authResult)
                {
                    connection.CookieContainer = new CookieContainer();
                    connection.CookieContainer.Add(returnedCookie);
                }
                else
                {
                    Console.WriteLine("Login failed");
                }
                break;

            case AuthenticationType.Windows:
                connection.Credentials = CredentialCache.DefaultCredentials;
                break;

            case AuthenticationType.Token:
                connection.Headers.Add(SignalRClientConfig.TokenData.Name, SignalRClientConfig.TokenData.Token);
                break;

            case AuthenticationType.Certificate:
                connection.AddClientCertificate(X509Certificate.CreateFromCertFile(SignalRClientConfig.CertificateName));
                break;

            default:
                break;
            }

            connection.Closed         += Connection_Closed;
            connection.Reconnected    += Connection_Reconnected;
            connection.ConnectionSlow += Connection_ConnectionSlow;
            HubProxy = connection.CreateHubProxy(SignalRClientConfig.HubName);

            RegisterServerMethod();

            try
            {
                await connection.Start().ContinueWith((t) =>
                {
                    if (t.IsFaulted)
                    {
                        foreach (var exItem in t.Exception.InnerExceptions)
                        {
                            var msg = exItem.Message + exItem.GetType().ToString() + exItem.StackTrace;

                            LoggerPool.Log(Name, new LogContentEntity()
                            {
                                Level   = "Error",
                                Message = msg
                            });
                        }
                    }
                    else
                    {
                        LoggerPool.Log(SignalRClientConfig.HubName, new LogContentEntity()
                        {
                            Level   = "Info",
                            Message = "Connect success"
                        });

                        OnConnected();
                    }
                });
            }
            catch (HttpRequestException ex)
            {
                var msg = "Unable to connect to server: Start server before connecting clients.";

                LoggerPool.Log(SignalRClientConfig.HubName, new LogContentEntity()
                {
                    Level   = "Error",
                    Message = msg + ex.Message + ex.StackTrace
                });
            }
        }