MQTT--websocketsテスト
10824 ワード
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Templatetitle>
<link href="css/bootstrap.min.css" rel="stylesheet">
head>
<body>
<h1> , !h1>
<div class="form-group">
<label for="exampleInputEmail1"> :chatlabel>
div>
<div class="form-group">
<label for="exampleInputEmail1">websocket:www.liefyuan.com:9001label>
div>
<div class="form-group">
<label for="exampleInputEmail1">mqtt:www.liefyuan.com:1883label>
div>
<div class="form-group">
<input type="email" class="form-control" id="msg" placeholder=" ">
<button type="submit" onclick="sendMsg()"class="btn btn-default"> button>
div>
<div class="form-group">
<label for="exampleInputEmail1"> :label>
div>
<div class="form-group">
<textarea class="form-control" rows="10" id="messageShow">textarea>
div>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js">script>
<script src="https://cdn.bootcss.com/paho-mqtt/1.0.2/mqttws31.min.js">script>
<script type="text/javascript">
client = new Paho.MQTT.Client("www.liefyuan.top", Number(9001), "websockets-test");//
client.connect({onSuccess:onConnect});//
function onConnect() {
console.log("onConnected");
client.subscribe("chat");//
console.log("subscribed");
//
message = new Paho.MQTT.Message("helloWS");
message.destinationName = "chat";
client.send(message);
console.log("sended")
}
client.onConnectionLost = onConnectionLost;//
client.onMessageArrived = onMessageArrived;//
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
console.log(" ");
}
}
function onMessageArrived(message) {
console.log(" :"+message.payloadString);
document.getElementById("messageShow").value+=message.payloadString;
document.getElementById("messageShow").value+="
";
}
function sendMsg() {
message = new Paho.MQTT.Message(document.getElementById("msg").value);
message.destinationName = "chat";
client.send(message);
console.log("sended")
}
script>
body>
html>