在unity3d中接受MQTT消息协议数据C# .net,M2Mqtt
2015-11-03 19:43
701 查看
具体关于MQTT是什么具体就不说了,百度一下,你就知道。这里用的是M2Mqtt的类库。可以去查官网然后git下来自己生成一下。接下来直接上unity客户端的本地代码:
需要提前导入生成的DLL。
需要提前导入生成的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); } }
相关文章推荐
- MQTT协议笔记之头部信息
- PyQt4 学习笔记7之控件II
- 运用QRegExp 解析字符串
- 设置QTabWidget中tab页的大小
- QT5入门之30-控件
- qt移植到arm上的过程
- Qtcreator4.8+Ubuntu14.04 fcitx无法输入中文
- QT中QPainter的使用及矩形、圆形等常见图形的画法
- 简单的QT绘图程序
- Qt学习之2D绘图(画刷和画笔)
- QT实现的类似QQ的头像选择窗
- Qt使用教程之创建一个基于Qt部件的应用程序(三)
- 一位高人的QTP学习笔记
- Qt5简介及Qt5基本框架
- PyQt4(简单界面)
- mqtt 的学习
- Qt头文件中的QT_BEGIN_NAMESPACE
- qt5.5.1在Cortex-a8上移植记录
- Qt5启动画面
- Qt小总结----QSplitter、QDockWidget、QToolBar、布局