Unity MQTT communication protocol in the application of --JS

Has been in the industry study, according to the requirements of digital twin, you need to get through the network, from the CLP to the IOT to Dass to a virtual factory (three-dimensional visualization), in order to open up the control and status anti-control, we need to open up the network communication for Unity first thought certainly Socket communications, which many developers are familiar with the method, but MQTT communication protocol used in the communications industry is very common, so today I MQTT communication protocol for use in Unity do introduce
me to be here because of the cloud, so, I tested two options
1. to achieve MQTT communication through JS, and through interaction with JS Unity, communicate
first need paho-mqtt.js, jquery.min.js two scripts, there are many online, if it is also proficient in JS you can write your own, we all know that used to doing.
Here is the code to connect and communicate information

    <script src="./jquery.min.js"></script>
    <script src="./paho-mqtt.js"></script>
        <scrpt>
           //MQTT连接
    //选中订阅主题
    var selectedTopics = [];
    //选中发布主题
    var currentTopic;
      //客户端选项
    var option = {
        "ServerUri": "127.0.0.1",
        "ServerPort": 61623,
        "UserName": "admin",
        "Password": "password",
        "ClientId": "",
        "TimeOut": 5,
        "KeepAlive": 100,
        "CleanSession": false,
        "SSL":false
    }

      //客户端
    var client;

      //连接
    function connectFun(){
        option .ClientId = guid();
        client = new Paho.Client(option.ServerUri, option.ServerPort, option.ClientId);
        client.onConnectionLost = onConnectionLost;
        client.onMessageArrived = onMessageArrived;
        client.connect({
            invocationContext: {
                host: option.ServerUri,//IP地址
                port: option.ServerPort,//端口号
               path: client.path,
                clientId: option.ClientId//标识
            },
            timeout: option.TimeOut,//连接超时时间
            keepAliveInterval: option.KeepAlive,//心跳间隔
            cleanSession: option.CleanSession,//是否清理Session
            useSSL: option.SSL,//是否启用SSL
            userName: option.UserName,  //用户名
            password: option.Password,  //密码
            onSuccess: onConnect,//连接成功回调事件
            onFailure: onError//连接失败回调事件
        }); 
    }

    //断开连接
    function disConnect(){
        //client.disconnect();
    }

    //订阅
    function onSubscrip(sub)
    {
        if (!client) {
            gameInstance.SendMessage("GameAPP","OnException","请连接服务");
            //alert("失败");
            return; 
        }

        selectedTopics.push(sub);
        client.subscribe(selectedTopics,0x02);
        //alert("订阅:" + sub);
        gameInstance.SendMessage("GameAPP","OnException","订阅频道:" + sub);
    }

            //发布
    function onRele(sub,info)
    {
        if (!client) {
            gameInstance.SendMessage("GameAPP","OnException","请连接服务");
            return;
        }

        var message = new Paho.Message(info);
        currentTopic = sub;
        message.destinationName = currentTopic;
        //client.send(message)
        client.publish(currentTopic,info,0x02,false);

        gameInstance.SendMessage("GameAPP","OnException","发布消息:" + info);

    }

         //接收消息事件
    function onMessageArrived(data) {
        gameInstance.SendMessage("GameAPP","OnGetInfo","接收消息:" + data.payloadString);
        //alert("消息:" + data.payloadString);
    } 

    //连接断开事件
    function onConnectionLost(e) {
        if (e.errorCode !== 0) {
           gameInstance.SendMessage("GameAPP","OnException","发布消息:" + e.errorCode);
        }
    }

    //连接成功回调事件
    function onConnect()
    {
        gameInstance.SendMessage("GameAPP","OnException","连接成功!");
    }

    function onError(e){
        gameInstance.SendMessage("GameAPP","OnException","连接失败,请重新连接,");
    }

    //生成GUID
    function guid() {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }
        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }

        </script>

Only need to accept the information sent to Unity, or information obtained from Unity issued.

Guess you like

Origin blog.51cto.com/myselfdream/2486066