using System.Net; using System.Threading.Tasks; using Makaretu.Dns; using MQTTnet; using MQTTnet.Server; using UnityEngine; namespace SicknessReduction.Haptic { public class DataBroker : MonoBehaviour { private IMqttServer server; private float lastTime = 0f; private async void Start() { server = new MqttFactory().CreateMqttServer(); var service = new ServiceProfile("blabla", "_mqtt._tcp", 1883, new[] {Helpers.GetIPAddress()}); var sd = new ServiceDiscovery(); sd.Advertise(service); await server.StartAsync(new MqttServerOptions()); Debug.Log("Mqtt Server started"); //await PublishBla(); } public async Task PublishBla() { await server.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("Fan/0/Control") .WithPayload("Hallihallo\0").Build()); } private async void Update() { var dif = Time.time - lastTime; if (dif > 4f) { lastTime = Time.time; //TODO: publish proper data //await PublishBla(); } } private async void OnDestroy() { if (!server.IsStarted) return; await server.StopAsync(); } } }