Mqtt to Unity via shiftr.io: Unterschied zwischen den Versionen

Aus hyperdramatik
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „Create yourself a namespace on == https://shiftr.io/ ==“)
 
Zeile 1: Zeile 1:
Create yourself a namespace on  
Create yourself a namespace on  
== https://shiftr.io/ ==
 
'''Create yourself a namespace on
[https://shiftr.io/ shiftr.io] '''
 
'''Download MQQT library from
[[hyperdramatik.net/mediawiki/index.php?title=MQTT_in_Unity]] gitlab'''
 
'''Create a Unity Project (2019 version)'''
 
go to ASSETS --> Import Custom Pages --> Import the Mqtt Package you just downloaded to your Unity project
 
Go to Project Window --> Assets --> MQTT --> Scripts --> Test → delete TestScript. Create a new Script with the following Code:
 
----
 
using UnityEngine;
using System;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using uPLibrary.Networking.M2Mqtt.Utility;
using uPLibrary.Networking.M2Mqtt.Exceptions;
 
 
 
public class mqttShiftIO : Mirror.NetworkBehaviour
 
{
private MqttClient client;
// Use this for random Client_ID
//const string glyphs = "abcdefghijklmnopqrstuvwxyz0123456789";
//int charAmount;
public string Shiftrio_ClientID = "MyNameOnShiftIoIwant";
public string Shiftrio_Key = "key findest du im token";
public string Shiftrio_Secret = "langeNummer findest du unter secret im token";
 
public string msg = "";
 
public List<string> topics_sub = new List<string>();
 
void Start () {
// create client instance
client = new MqttClient(IPAddress.Parse("34.76.6.166"), 1883 , false , null ); ////////"34.76.6.166" ---> IP from boker.shiftr.io
 
// register to message received ---> EventSystem
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
/////////////////////////////////////////////////////////Randomize Client ID ///////////////////////////////
//charAmount = UnityEngine.Random.Range(3, 8);
//for (int i = 0; i < charAmount; i++)
//{
// Shiftrio_ClientID += glyphs[UnityEngine.Random.Range(0, glyphs.Length)];
//}
 
client.Connect(Shiftrio_ClientID, Shiftrio_Key, Shiftrio_Secret);
// subscribe to the topics in the list with QoS 2
foreach (String topic in topics_sub) {
client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
 
}
 
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
string topic = e.Topic;
msg = System.Text.Encoding.UTF8.GetString(e.Message);
Debug.Log("Received: " + topic +" --> "+ msg);
debug.text = msg;
}
 
void OnGUI()//creates Button to Press
{
if (GUI.Button(new Rect(20, 40, 80, 20), "Send"))
{
Debug.Log("sending...");
client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("Sending from Unity3D!!!"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
Debug.Log("sent");
}
}
}
 
----

Version vom 22. Mai 2020, 18:36 Uhr

Create yourself a namespace on

Create yourself a namespace on shiftr.io

Download MQQT library from hyperdramatik.net/mediawiki/index.php?title=MQTT_in_Unity gitlab

Create a Unity Project (2019 version)

go to ASSETS --> Import Custom Pages --> Import the Mqtt Package you just downloaded to your Unity project

Go to Project Window --> Assets --> MQTT --> Scripts --> Test → delete TestScript. Create a new Script with the following Code:


using UnityEngine; using System; using System.Net; using System.Collections; using System.Collections.Generic; using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; using uPLibrary.Networking.M2Mqtt.Utility; using uPLibrary.Networking.M2Mqtt.Exceptions;


public class mqttShiftIO : Mirror.NetworkBehaviour

{ private MqttClient client;

// Use this for random Client_ID //const string glyphs = "abcdefghijklmnopqrstuvwxyz0123456789"; //int charAmount;

public string Shiftrio_ClientID = "MyNameOnShiftIoIwant"; public string Shiftrio_Key = "key findest du im token"; public string Shiftrio_Secret = "langeNummer findest du unter secret im token";


public string msg = "";

public List<string> topics_sub = new List<string>();


void Start () { // create client instance

client = new MqttClient(IPAddress.Parse("34.76.6.166"), 1883 , false , null ); ////////"34.76.6.166" ---> IP from boker.shiftr.io

// register to message received ---> EventSystem client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

/////////////////////////////////////////////////////////Randomize Client ID /////////////////////////////// //charAmount = UnityEngine.Random.Range(3, 8); //for (int i = 0; i < charAmount; i++) //{ // Shiftrio_ClientID += glyphs[UnityEngine.Random.Range(0, glyphs.Length)]; //}

client.Connect(Shiftrio_ClientID, Shiftrio_Key, Shiftrio_Secret);

// subscribe to the topics in the list with QoS 2 foreach (String topic in topics_sub) { client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); }


}


void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { string topic = e.Topic; msg = System.Text.Encoding.UTF8.GetString(e.Message); Debug.Log("Received: " + topic +" --> "+ msg); debug.text = msg;

}

void OnGUI()//creates Button to Press { if (GUI.Button(new Rect(20, 40, 80, 20), "Send")) { Debug.Log("sending..."); client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("Sending from Unity3D!!!"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true); Debug.Log("sent"); } } }