12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- 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[] {new IPAddress(new[] {(byte) 192, (byte) 168, (byte) 1, (byte) 4})});
- 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("Test/Bla")
- .WithPayload("Hallihallo").Build());
- }
- private async void Update()
- {
- var dif = Time.time - lastTime;
- if (dif > 4f)
- {
- lastTime = Time.time;
- await PublishBla();
- }
- }
- private async void OnDestroy()
- {
- if (!server.IsStarted) return;
- await server.StopAsync();
- }
- }
- }
|