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>
    var selectedTopics = [];
    var currentTopic;
    var option = {
        "ServerUri": "",
        "ServerPort": 61623,
        "UserName": "admin",
        "Password": "password",
        "ClientId": "",
        "TimeOut": 5,
        "KeepAlive": 100,
        "CleanSession": false,

    var client;

    function connectFun(){
        option .ClientId = guid();
        client = new Paho.Client(option.ServerUri, option.ServerPort, option.ClientId);
        client.onConnectionLost = onConnectionLost;
        client.onMessageArrived = onMessageArrived;
            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(){

    function onSubscrip(sub)
        if (!client) {

        //alert("订阅:" + sub);
        gameInstance.SendMessage("GameAPP","OnException","订阅频道:" + sub);

    function onRele(sub,info)
        if (!client) {

        var message = new Paho.Message(info);
        currentTopic = sub;
        message.destinationName = currentTopic;

        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()

    function onError(e){

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


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

Guess you like

Origin blog.51cto.com/myselfdream/2486066