具体关于MQTT是什么具体就不说了,百度一下,你就知道。这里用的是M2Mqtt的类库。可以去查官网然后Git下来自己生成一下。接下来直接上unity客户端的本地代码:
需要提前导入生成的DLL。
- using UnityEngine;
- using System.Collections;
- using System.Net;
- using System.Text;
- using uPLibrary.Networking.M2Mqtt;
- using uPLibrary.Networking.M2Mqtt.Messages;
- public class Mqtt : MonoBehaviour
- {
- private MqttClient mqttClient;
- void Awake()
- {
- //链接服务器
- mqttClient = new MqttClient(IPAddress.Parse("192.168.1.163"));
- //注册服务器返回信息接受函数
- mqttClient.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
- //客户端ID 一个字符串
- mqttClient.Connect("zsc");
- //监听FPS字段的返回数据
- mqttClient.Subscribe(new string[] { "fps" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
- }
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- if (Input.GetMouseButtonDown(0))
- {
- //这个字符串是向服务器发送的数据信息
- string strValue = "123";
- // 发送一个内容是123 字段是klabs的信息
- mqttClient.Publish("klabs", Encoding.UTF8.GetBytes(strValue));
- Debug.Log("发送数据123");
- }
- }
- static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
- {
- // handle message received
- Debug.Log("返回数据");
- string msg = System.Text.Encoding.Default.GetString(e.Message);
- Debug.Log(msg);
- }
- }