- Home /
Question by
vikrimuhammadgafar · Jul 29, 2021 at 09:34 AM ·
networkingprogrammingsleepsleeping
My mqtt protocol is stopping
my program are using "uPLibrary.Networking.M2Mqtt" library. yes it works flawlessly for sending data and receiving data.
the only problem is if my phone (Android) are sleeping the MQTT are not connected.
the question is how to make the MQTT is still connected if the phone in sleep mode?
here's the code.
using UnityEngine;
using System.Collections;
using System.Net;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using uPLibrary.Networking.M2Mqtt.Utility;
using uPLibrary.Networking.M2Mqtt.Exceptions;
using System;
public class mqttTest : MonoBehaviour {
private MqttClient client;
// Use this for initialization
void Start () {
// create client instance
client = new MqttClient(IPAddress.Parse("8.8.8.8"),1883 , false , null );
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "/home/temperature" with QoS 2
client.Subscribe(new string[] { "test/data" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
Debug.Log("Received: " + System.Text.Encoding.UTF8.GetString(e.Message) );
}
void send_data(){
Debug.Log("sending...");
client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("Sending from Unity3D!!!"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
Debug.Log("sent");
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Multiple array variable 2 Answers
UNET Server not setting [SyncVar] 0 Answers
Unity networking tutorial? 6 Answers
Does NetworkPlayer.guid stay the same for the entire lenght of the networking session? 1 Answer