Esempio n. 1
0
        public FedNetClient(ConnectorData MQTTConnectorData, IFedNetLogger newlogSystem = null)
        {
            _logSystem = newlogSystem;
            if (_logSystem == null)
            {
                _logSystem = new DefaultLogger();
            }

            _MQTTHostAndPort = MQTTConnectorData.getHasSE();

            _ClientConfiguration = new MqttClientOptionsBuilder();
            _ClientConfiguration.WithClientId(String.Format("{0}-{1}_{2}", MQTTConnectorData.Username, DateTime.Now.ToString("ffffmmHHss"), FedNetWorker.getRandomString(10)));
            if (MQTTConnectorData.useCreditential)
            {
                _ClientConfiguration.WithCredentials(MQTTConnectorData.Username, MQTTConnectorData.Password);
            }
            _ClientConfiguration.WithTcpServer(MQTTConnectorData.Host, MQTTConnectorData.Port);

            _theGameClient = new MqttFactory().CreateMqttClient();
            _theGameClient.UseDisconnectedHandler(e => {
                _logSystem.Info("Disconnected (reason : " + (e.AuthenticateResult != null ? e.AuthenticateResult.ResultCode.ToString() : "unknow") + ")");
                if (Disconnected != null)
                {
                    Disconnected.Invoke(this, e);
                }
                if (reconnectOnDisco)
                {
                    Task.Delay(1000).Wait();
                    Connect();
                }
            });
            _theGameClient.UseConnectedHandler(e => {
                _logSystem.Info("Connected !!");
                _theGameClient.SubscribeAsync(FedNetConstante.SERVER_TO_CLIENT + FedNetConstante.DEFAULT_TOPIC_SEPARATOR + ClientId + FedNetConstante.DEFAULT_TOPIC_SEPARATOR + FedNetConstante.DEFAULT_TOPIC_JOKER, (MqttQualityOfServiceLevel)FedNetConstante.PRIORITY_SERVER_TO_CLIENT);
                _theGameClient.SubscribeAsync(FedNetConstante.SERVER_BROADCAST + FedNetConstante.DEFAULT_TOPIC_SEPARATOR + FedNetConstante.DEFAULT_TOPIC_JOKER, (MqttQualityOfServiceLevel)FedNetConstante.PRIORITY_SERVER_BROADCAST);
                if (Connected != null)
                {
                    Connected.Invoke(this, e);
                }
            });
            _theGameClient.UseApplicationMessageReceivedHandler(e => {
                if (e.ClientId == "" || e.ClientId == " " || e.ClientId == null)
                {
                    return;
                }
                if (MessageReceived != null)
                {
                    MessageReceived.Invoke(this, Message.getMessage(e.ApplicationMessage));
                }
            });

            reconnectOnDisco = true;

            _logSystem.Info("Client initialized !");
        }
Esempio n. 2
0
        public void changeClientConfig(ConnectorData MQTTConnectorData)
        {
            _MQTTHostAndPort     = MQTTConnectorData.getHasSE();
            _ClientConfiguration = new MqttClientOptionsBuilder();
            _ClientConfiguration.WithClientId(String.Format("{0}-{1}_{2}", MQTTConnectorData.Username, DateTime.Now.ToString("ffffmmHHss"), FedNetWorker.getRandomString(10)));
            if (MQTTConnectorData.useCreditential)
            {
                _ClientConfiguration.WithCredentials(MQTTConnectorData.Username, MQTTConnectorData.Password);
            }
            if (!MQTTConnectorData.useCreditential)
            {
                _ClientConfiguration.WithCredentials(MQTTConnectorData.Username, "");
            }
            _ClientConfiguration.WithTcpServer(MQTTConnectorData.Host, MQTTConnectorData.Port);

            _logSystem.Info("MQTT Client reinitialized !");
        }