国内综合精彩aⅤ无码不卡_日本少妇做爰全过程_欧美性爱在线播放免费_午夜?V日韩一区二区_免费a级毛片无码免费播放_成在人线av无码喷水_亚洲精品网站色视频_国产婷婷精品成人_老师撩起裙子让我桶的视频_秋霞影院国产

WebSocket模擬溫濕度數(shù)據(jù)上傳

作者:wcx7758258 | 更新時(shí)間:2017-11-30 | 瀏覽量:2125

直接上源碼

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>WebSocket</title>
    <!--
        WebSocket實(shí)現(xiàn)溫濕度的模擬上傳
        version:1.0
        author:wcx0407@gamil.com
    -->
</head>
<body>
    <div id="getMsg">
        <p id="printMsg">心跳次數(shù):0</p>
        <p id="printMsg1">非心跳返回值:0</p>
        返回內(nèi)容:<p id="printMsg2">null</p>
    </div>
    <form>
        <input type="text" id="text1">
        <input type="button" id="send1" value="發(fā)送溫度" onclick="sendTem()"><br><br>
        <input type="text" id="text2">
        <input type="button" id="send1" value="發(fā)送濕度" onclick="sendHum()"><br><br>
        <input type="button" id="send2" value="查看是否在線" onclick="isOL()">
        <input type="button" id="log" value="登陸溫濕度傳感器" onclick="logo()">
        <input type="button" id="close" value="關(guān)閉連接" onclick="socket.close()">
    </form>
    <script>
        var socket=new WebSocket("ws://m.placeboworld.cn:8383");
        var temAndHumLogo="{\"M\":\"checkin\",\"ID\":\"xx1\",\"K\":\"xx2\"}\n";              //xx1為:設(shè)備ID ,xx2為:該設(shè)備的apikey
        var tem1="{\"M\":\"update\",\"ID\":\"xx1\",\"V\":{\"xx3\":\"";  //xx3為:該設(shè)備的溫度數(shù)據(jù)接口id
        var hum1="{\"M\":\"update\",\"ID\":\"xx1\",\"V\":{\"xx4\":\"";  //xx4為:該設(shè)備的濕度接口id
        var temAndHum="\"}}\n";
        var isol="{\"M\":\"isOL\",\"ID\":[\"xx5\"]}\n";                 //xx5為:要查詢的設(shè)備id
        var tem=0;
        var hum=0;
        //監(jiān)聽WebSocket打開時(shí)的操作
        socket.onopen=function(){
            alert("WebSocket已打開!");
        }
        //監(jiān)聽WebSocket關(guān)閉時(shí)的操作
        socket.onclose=function(){
            alert("WebSocket已關(guān)閉!");
        }
        //登陸溫濕度傳感器設(shè)備
        function logo(){
            socket.send(temAndHumLogo);
        }
        //查詢當(dāng)前設(shè)備是否在線
        function isOL(){
            socket.send(isol);
        }
        //發(fā)送溫度數(shù)據(jù)
        function sendTem(){
            tem=document.getElementById("text1").value;
            socket.send(tem1+tem+temAndHum);
        }
        //發(fā)送濕度數(shù)據(jù)
        function sendHum(){
            hum=document.getElementById("text2").value;
            socket.send(hum1+hum+temAndHum);
        }
        //監(jiān)聽服務(wù)器傳回信息時(shí)的操作,心跳保持在線,輸出返回信息
        socket.onmessage=function(msg){
            document.getElementById("printMsg2").innerHTML=msg.data;
            if(msg.data=="{\"M\":\"ping\"}"){
                document.getElementById("printMsg").innerHTML="心跳次數(shù):"+(i++);
                socket.send("ping");
            }else{
                document.getElementById("printMsg1").innerHTML="非心跳返回值:"+(j++);
                socket.send("ping");
            }   
        }
    </script>
</body>
</html>
 


評(píng)論:共6條

貝殼物聯(lián) 評(píng)論于:2017-11-30 12:09:01
不錯(cuò)多謝分享
wcx7758258 回復(fù)于:2018-04-08 22:45:13
回復(fù) @貝殼物聯(lián):謝謝你無私奉獻(xiàn)呢
ericsma 評(píng)論于:2018-03-16 17:40:27
刷個(gè)分?
wcx7758258 回復(fù)于:2018-04-08 22:44:54
回復(fù) @ericsma:刷什么
lindx 評(píng)論于:2020-03-05 23:00:20
能否讀回上傳的數(shù)據(jù)?
wcx7758258 評(píng)論于:2020-05-05 18:31:38
什么意思
返回頂部