Unity程序使用MQTT进行通讯

using HslCommunication.MQTT;
using Newtonsoft.Json;
using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using File = System.IO.File;

public class MQTTManager : SerializedMonoBehaviour
{
    public static MQTTManager instance = null;
    [FoldoutGroup("Guid")]
    [Header("存放客户端获取到的guid")]
    public string guid = "kcrobot";
    [FoldoutGroup("Guid")]
    [Header("存放json的字典")]
    public Dictionary<string, GuidJson> GuidDic = new Dictionary<string, GuidJson>();
    [FoldoutGroup("Guid")]
    [Header("json路径")]
    //public string GuidPath = Application.streamingAssetsPath + @"/NetworkConfiguration/JsonGuid.json";
    public string GuidPath;
    [FoldoutGroup("Guid"), LabelText("用于读取转换")]
    [Header("用于读取转换")]
    private GetGuid getGuid;
    [FoldoutGroup("Guid"), LabelText("机器人主动发送数据")]
    [Header("用于读取转换")]
    private SendKey sendkey;


    [FoldoutGroup("机器人运行数据"), LabelText("接收数据的数据结构")]
    [Header("要添加的订阅主题")]
    /*
      send:服务端发送数据,即机器人上报给客户端的数据,时间间隔1s。
    recv:客户端发送数据,即客户端给机器人服务端发送的控制数据,有控制命令时下发,无控制命令时无需下发
    alm:服务端上报实时报警数据,机器人产生报警后自动发送
    record:服务端上报实时识别记录数据,机器人产生检测结果时自动发送
    getKey:客户端获取权限,客户端需要控制权限时的申请权限topic
    sendKey:服务端返回权限,服务端返回控制权限id时的topic
    mapinfo: 机器人端转发激光导航数据.


    gsStop:每当到巡检点位停车时发送当前点巡检信息
    analysisCheck: 当识别标定配置为第三方检测时,第三方检测开始和结束均发送相应的配置信息.
    analysisReceive: 当第三方检测完成时,可通过此接口将结果传回机器人端.机器人端将会显示检测结果.

    */

    string[] topics = new string[] { "send", "getKey", "sendKey", "recv", "alm", "robotLog", "record", "mapinfo" };
    private Queue<string> msgQue = new Queue<string>();
    public bool isRecieve = false;



    [Header("测试文本")]
    public Text T1;
    public Text T2;
    public Text T3;
    public Text T4;
    [Header("机器人状态")]
    public int Robotstate;
    [FoldoutGroup("真实数据测试"), LabelText("机器人及环境数据显示路径")]
    public string pathTest;
    [FoldoutGroup("真实数据测试"), LabelText("数据显示str")]
    public string 测试读取队列内容;
    [FoldoutGroup("真实数据测试"), LabelText("定位数据str")]
    public string 定位Test;
    [FoldoutGroup("真实数据测试"), LabelText("定位数据路径")]
    public string 定位path;
    [FoldoutGroup("真实数据测试"), LabelText("报警数据str")]
    public string AlarmTest;
    [FoldoutGroup("真实数据测试"), LabelText("报警数据路径")]
    public string 告警Path;
    [FoldoutGroup("真实数据测试"), LabelText("智能数据str")]
    public string 智能Test;
    [FoldoutGroup("真实数据测试"), LabelText("智能数据路径")]
    public string 智能Path;
    private void Awake()
    {
        
        GuidPath = Application.dataPath + @"/StreamingAssets/NetworkConfiguration/JsonGuid.json";
        pathTest = Application.dataPath + @"/StreamingAssets/1.json";
        定位path = Application.dataPath + @"/StreamingAssets/Move.json";
        告警Path = Application.dataPath + @"/StreamingAssets/Alarm.json";
        智能Path = Application.dataPath + @"/StreamingAssets/Dispatch.json";
        IstGetGuid();//将需要发送的消息进行初始化赋值
        instance = this;
    }


    void Start()
    {
        测试读取队列内容 = File.ReadAllText(pathTest, Encoding.UTF8);
        定位Test = 
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Unity使用MQTT连接高德地图服务器,您需要遵循以下步骤: 1. 下载并安装MQTT库:您可以使用paho-mqtt库来在Unity使用MQTT。请确保在Unity中正确安装该库。 2. 创建MQTT连接:使用paho-mqtt库创建一个MQTT连接,该连接将连接到高德地图服务器。 3. 订阅主题:为了接收高德地图服务器发送的消息,您需要订阅一个或多个主题。 4. 处理消息:一旦您的Unity应用程序订阅了主题,它将开始接收消息。您需要编写代码来处理这些消息并根据需要更新您的Unity场景。 以下是一个示例代码片段,演示如何在Unity中连接到MQTT服务器并订阅高德地图服务器发送的主题: ```csharp using UnityEngine; using System.Collections; using MQTT; public class MqttExample : MonoBehaviour { private MQTT.Client client; // Use this for initialization void Start () { client = new MQTT.Client("tcp://mqtt.example.com:1883", "exampleclient"); client.Connect(); client.Subscribe(new string[] { "topic1", "topic2" }, new byte[] { MQTT.MsgBase.QOS_LEVEL_AT_MOST_ONCE, MQTT.MsgBase.QOS_LEVEL_AT_LEAST_ONCE }); client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; } void client_MqttMsgPublishReceived(object sender, MQTT.MqttMsgPublishEventArgs e) { Debug.Log("Received: " + System.Text.Encoding.UTF8.GetString(e.Message)); } } ``` 请注意,此代码仅是示例代码,并且需要进行修改以适应您的具体应用程序和服务器设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值