Beispiel #1
0
        public static Message getMessage(MqttApplicationMessage mess)
        {
            List <string> lstTop = FedNetWorker.getListByTopic(mess.Topic);

            if (lstTop[0] == FedNetConstante.CLIENT_TO_SERVER)
            {
                string theNewClientID = lstTop[1];
                lstTop.RemoveRange(0, 2);
                return(new Message(theNewClientID, lstTop, mess.Payload));
            } // server side
            if (lstTop[0] == FedNetConstante.SERVER_TO_CLIENT)
            {
                lstTop.RemoveAt(0);
            }
            lstTop.RemoveAt(0);
            return(new Message(lstTop, mess.Payload)); // client side
        }
Beispiel #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 !");
        }
Beispiel #3
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 !");
        }
Beispiel #4
0
 public bool sendMessage(List <string> lisTopic, byte[] Data, MessagePriority priority = FedNetConstante.DEFAULT_PRIORITY)
 {
     return(sendMessage(FedNetWorker.getTopicByList(lisTopic), Data, priority));
 }