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 !"); }
public FedNetServer(ServerConfigData MQTTConfigData, IFedNetLogger newlogSystem = null) { _logSystem = newlogSystem; if (_logSystem == null) { _logSystem = new DefaultLogger(); } _theAuthenticator = MQTTConfigData.Authenticator; _ServerConfiguration = new MqttServerOptionsBuilder(); _ServerConfiguration.WithConnectionValidator(this); _ServerConfiguration.WithApplicationMessageInterceptor(this); _ServerConfiguration.WithSubscriptionInterceptor(this); _ServerConfiguration.WithClientId("Server"); _ServerConfiguration.WithDefaultEndpointBoundIPAddress(MQTTConfigData.listenAdress); _ServerConfiguration.WithDefaultEndpointPort(MQTTConfigData.Port); _ServerConfiguration.WithMaxPendingMessagesPerClient(FedNetConstante.MAX_MESSAGE_PENDING); _theClientList = new List <ClientData>(); _theGameServer = new MqttFactory().CreateMqttServer(); _theGameServer.UseApplicationMessageReceivedHandler(e => { if (e.ClientId == "" || e.ClientId == " " || e.ClientId == null) { return; } if (MessageReceived != null) { MessageReceived.Invoke(this, Message.getMessage(e.ApplicationMessage)); } }); _theGameServer.UseClientConnectedHandler(e => { _logSystem.Info("new client connected : " + e.ClientId); //_theGameServer.SubscribeAsync("Server", FedNetConstante.CLIENT_TO_SERVER + FedNetConstante.DEFAULT_TOPIC_SEPARATOR + FedNetConstante.DEFAULT_TOPIC_JOKER, (MqttQualityOfServiceLevel)FedNetConstante.PRIORITY_SERVER_TO_CLIENT); }); _theGameServer.StartedHandler = this; _theGameServer.StoppedHandler = this; _theGameServer.ClientDisconnectedHandler = this; _logSystem.Info("initialisation finished !!"); }
public void ChangeLogger(IFedNetLogger newlogSystem) { _logSystem = newlogSystem; }