function CreateIOTPushObject() { var obj = new Object(); obj.client = null; //华菱的 //obj.host = "39.97.75.240"; //obj.port = "8083"; //236的 obj.host = "47.104.236.109"; obj.port = "8083"; obj.clientId = null; obj.user = "fangyar"; obj.password = "fangyar"; obj.destination = []; obj.init = function () { this.setClient(); this.setMessageArrived(); this.setConnectionLost(); this.setConnect(); }; obj.setClient = function () { this.client = new Messaging.Client(this.host, Number(this.port), this.clientId); //创建客户端; this.client.onConnect = this.onSubscribe; //订阅 this.client.onConnectionLost = this.onConnectionLost; }; obj.getClient = function () { if (this.client == null) { this.setClient(); } return this.client; }; obj.setConnect = function () { if (this.client == null) { this.setClient(); } var options = { timeout: "number", userName: "string", password: "string", willMessage: "object", keepAliveInterval: "number", cleanSession: "boolean", useSSL: "boolean", invocationContext: "object", onSuccess: "function", onFailure: "function", hosts: "object", ports: "object" }; this.client.connect({ timeout: 30, keepAliveInterval: 60, userName: this.user, password: this.password, onSuccess: this.onSubscribe, //连接成功事件 onFailure: this.onFailure//连接失败事件 }); }; obj.reConnect = function () { this.client.connect({ timeout: 30, keepAliveInterval: 60, userName: this.user, password: this.password, onSuccess: this.onSubscribe, //连接成功事件 onFailure: this.onFailure//连接失败事件 }); }; obj.keepalive = function () { heartbeat_timer = setInterval(function () { obj.send('~H#C~', "keepalive"); console.log("发送心跳包"); }, 30000); } obj.setDisconnect = function () { if (this.client == null) { this.client.disconnect(); } }; obj.onSubscribe = function (frame) { console.log("--服务连接成功!--"); //obj.keepalive(); if (obj.destination != null && obj.destination.length > 0) { obj.destination.forEach(function (val, index) { obj.client.subscribe(val); console.log("订阅:[" + val + "]"); }); } }; obj.send = function (text, destinationName) { message = new Messaging.Message(text); message.destinationName = destinationName; obj.client.send(message); }; obj.onConnectionLost = function (responseObject) { obj.reConnect(); if (responseObject.errorCode !== 0) { console.log(obj.clientId + ": " + responseObject.errorCode); } }; obj.setMessageArrived = function () { this.client.onMessageArrived = this.MessageArrived; }; obj.MessageArrived = function (message) { console.log(message.payloadString); }; obj.onFailure = function (failure){ console.log("--服务连接失败!--"); console.log(failure.errorMessage); }; obj.guid = function () { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); }; obj.uuid = function (len, radix) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); var uuid = [], i; radix = radix || chars.length; if (len) { // Compact form for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]; } else { // rfc4122, version 4 form var r; // rfc4122 requires these characters uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; uuid[14] = '4'; // Fill in random data. At i==19 set the high bits of clock sequence as // per rfc4122, sec. 4.1.5 for (i = 0; i < 36; i++) { if (!uuid[i]) { r = 0 | Math.random() * 16; uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]; } } } return uuid.join(''); }; return obj; }