-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
98 lines (98 loc) · 2.28 KB
/
index.html
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
<!DOCTYPE html>
<!-- parse the mesage from SAKURA IoT Platform -->
<!-- 2016-06-20 K.OHWADA -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sakura IoT WebSocket</title>
<script type="text/javascript" src="./jquery.min.js"></script>
<script type="text/javascript">
var TOKEN = "your_token";
var URL = "wss://secure.sakura.ad.jp/iot-alpha/ws/" + TOKEN;
var ws = null;
// init
function init() {
open();
}
// open
function open() {
if (ws == null) {
ws = new WebSocket(URL);
ws.onopen = onOpen;
ws.onmessage = onMessage;
ws.onclose = onClose;
ws.onerror = onError;
}
}
// onOpen
function onOpen(event) {
dispMsg("connected");
}
// onMessage
function onMessage(event) {
if (event && event.data) {
parseMessage(event.data);
}
}
// onError
function onError(event) {
dispMsg("onError: " + event.data);
}
// onClose
function onClose() {
dispMsg("disconected: " + event.code);
ws = null;
// re-connect
setTimeout("open()", 3000);
}
// parse Message
function parseMessage(data) {
var obj = JSON.parse(data);
if (obj.type == null) {
dispMsg(event.data);
} else if (obj.type == "keepalive") {
parseKeepalive(obj)
} else if (obj.type == "channels") {
parseChannels(obj);
}
}
// parse Keepalive
function parseKeepalive(obj) {
var text = "type: " + obj.type;
text += ", datetime: " + obj.datetime;
dispMsg(text)
}
// parse Channels
function parseChannels(obj) {
var channels = obj.payload.channels;
var len = channels.length;
for (i=0; i<len; i++) {
var ch = channels[len - i - 1];
text = "channel: " + ch.channel;
text += ", type: " + ch.type;
text += ". value: " + ch.value;
dispMsg(text)
}
var text = "type: " + obj.type;
text += ", datetime: " + obj.datetime;
text += ", module: " + obj.module;
dispMsg(text);
}
// dispMsg
function dispMsg(message) {
var msgs = $("message").find("div");
while (msgs.length >= 100) {
msgs = msgs.last().remove();
}
var msgtag = $("<div>").text(message);
$("#message").prepend(msgtag);
}
// init
$(init);
</script>
</head>
<body>
<h1>Sakura IoT WebSocket</h1>
<div id="message"></div>
</body>
</html>