1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| <!DOCTYPE html > <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http - equiv="X-UA-Compatible" content="ie=edge"> <title> Document </title> <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="nofollow noopener" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.2.1/jquery.min.js"></script> <script src="/mqttws31.js" type="text/javascript"></script> <style> #contentList li { word-break: break-all; word-wrap: break-word; } </style> </head> <body> <div style="width: 900px;margin: 50px auto;"> <div class="form-group"> <label>评论人:</label> <input type="text" class="form-control" id="user"> </div> <div class="form-group"> <label>评论内容:</label> <textarea class="form-control" id="content" style="word-break:break-all;word-wrap:break-word;"></textarea> </div> <div class="form-group"> <input type="button" value="发表评论" class="btn btn-primary" onclick="send()"> </div> <div class="form-group"> <input type="button" value="连发测试" class="btn btn-primary" onclick="sendTest()"> </div> <div> <ul id="contentList" class="list-group">
</ul> </div> </div> <script> var hostname = '127.0.0.1', port = 8083, clientId = 'client-' + generateUUID(), timeout = 1000, keepAlive = 2000, cleanSession = false, ssl = false, userName = 'Nick', password = '12356', topic = 'ceshi'; client = new Paho.MQTT.Client(hostname, port, clientId); var options = { invocationContext: { host: hostname, port: port, path: client.path, clientId: clientId }, timeout: timeout, keepAliveInterval: keepAlive, cleanSession: cleanSession, useSSL: ssl, userName: userName, password: password, onSuccess: onConnect, onFailure: function (e) { console.log(e); } }; client.connect(options); function onConnect() { console.log("onConnected"); client.subscribe(topic); } client.onConnectionLost = onConnectionLost; client.onMessageArrived = onMessageArrived; function onConnectionLost(responseObject) { console.log(responseObject); if (responseObject.errorCode !== 0) { console.log("onConnectionLost:" + responseObject.errorMessage); console.log("连接已断开"); } } function onMessageArrived(message) { var msg = message.payloadString; console.log("收到消息:" + msg); console.log("收到消息时间戳:" + new Date().getTime()); var obj = JSON.parse(msg);
$('#contentList').append($(`<li class="list-group-item" > <span class="badge">评论人:` + obj.name + `,时间:` + obj.time + `</span>` + obj.content + `</li>`)); } function send() { var name = document.getElementById("user").value; var content = document.getElementById("content").value; console.log('name :>> ', name); console.log('content :>> ', content); var time = new Date().Format("yyyy-MM-dd hh:mm:ss"); console.log('time :>> ', time); console.log("发送前时间戳:" + new Date().getTime()); if (name) { $.ajax({ type: "post", url: "/getMsg", data: { name: name, content: content, time: time }, dataType: "json" }); document.getElementById("content").value = ""; document.getElementById("user").value = ""; } } function generateUUID() { var d = new Date().getTime(); if (window.performance && typeof window.performance.now === "function") { d += performance.now(); } var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; } Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S": this.getMilliseconds() }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } function sendTest() { for (var i = 1; i < 100; i++) { var name = "ceshi" + i; var content = "测试内容" + i; var time; time = new Date().getTime(); $.ajax({ type: "post", url: "/getMsg", data: { name: name, content: content, time: time }, dataType: "json" }); } } </script> </body> </html>
|