-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
108 lines (95 loc) · 4.01 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
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WebSocket and Bootstrap Sample</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<a href="#"></a>
<div id="header" class="container">
<h1>WebSocket and Bootstrap</h1>
</div>
<div class="container">
<div class="row">
<div class="col-sm-12" style="background:#ffffe5;">
<form class="form-inline" style="margin: 15px;">
<div class="form-group">
<label class="sr-only control-label" for="msg">Message</label>
<input id="msg-text" type="text" class="form-control">
</div>
<div class="form-group">
<input id="msg-submit" type="submit" value="submit" class="btn btn-primary">
</div>
</form>
<div class="btn-group" style="margin: 15px;">
<button id="on-btn" class="btn btn-success">ON</button>
<button id="off-btn" class="btn btn-default">OFF</button>
</div>
</div>
<!-- <div class="col-sm-6" style="background:#e5ffff;">bbb</div>
<div class="col-sm-3" style="background:#ffffe5;">ccc</div> -->
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready( function(){ // HTMLが準備できたら接続する
connect();
$('#msg-submit').click(function() {
var topic = 'Message';
var msg = $('#msg-text').val();
console.log(msg);
var payload = '{"' + topic + '": "' + msg + '"}';
webSocket.send(payload);
});
$('#on-btn').click(function() {
var topic = 'LED';
var msg = "on";
console.log(msg);
var payload = '{"' + topic + '": "' + msg + '"}';
webSocket.send(payload);
});
$('#off-btn').click(function() {
var topic = 'LED';
var msg = "off";
console.log(msg);
var payload = '{"' + topic + '": "' + msg + '"}';
webSocket.send(payload);
});
});
var wsUrl = 'ws://roadshow2015.mybluemix.net/ws/in';
var webSocket = null;
function connect() {
if (webSocket == null) {
// WebSocket の初期化
webSocket = new WebSocket(wsUrl);
console.log('connect');
console.log(webSocket);
// イベントハンドラの設定
// webSocket.onopen = onOpen;
webSocket.onopen = onOpen;
webSocket.onmessage = onMessage;
webSocket.onerror = onError;
webSocket.onclose = onClose;
}
}
function onOpen(event) {
// console.log('onMessage' + event.data);
}
function onMessage(event) {
console.log('onMessage' + event.data);
}
function onError(event) {
console.log(event.data);
}
function onClose(event) {
console.log(event.code);
webSocket = null; // 勝手に接続が切れることがあるので、自動再接続する
connect();
}
</script>
</body>
</html>