1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 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) 7})});
- 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();
- }
- }
- }
|