-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
178 lines (130 loc) · 4.19 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
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>ブラウザテスト</title>
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
<script type="text/javascript">
var ws;
function connectSocketServer(callback) {
var support = "MozWebSocket" in window ? 'MozWebSocket' : ("WebSocket" in window ? 'WebSocket' : null);
if (support == null) {
return;
}
if( ws != null){
ws.close();
}
console.log("* Connecting to server ..");
// create a new websocket and connect
ws = new window[support]('ws://127.0.0.1/');
// when data is comming from the server, this metod is called
ws.onmessage = function (evt) {
var json = evt.data;
//console.log(json);
var data = JSON.parse(json);
if( data["command"] == "add_message_to_browser"){
var message = "[" + data["time"] + "] 受信:" + data["message"];
add_message(message);
}
};
// when the connection is established, this method is called
ws.onopen = function () {
console.log('* Connection open');
if( typeof callback != "undefined" ){
callback("open");
}
};
// when the connection is closed, this method is called
ws.onclose = function () {
console.log('* Connection closed');
if( typeof callback != "undefined" ){
callback("close");
}
}
}
function disconnectWebSocket() {
if (ws) {
ws.close();
}
console.log("close");
}
function add_message(message){
$("#_message").append(message + "<br>");
}
function connect(){
$("#_signal_green").attr("src", "signal_green_off.png");
$("#_signal_red").attr("src", "signal_red_off.png");
connectSocketServer(function(state){
if( state == "open" ){
$("#_signal_green").attr("src", "signal_green_on.png");
$("#_signal_red").attr("src", "signal_red_off.png");
$("#_button_send").removeAttr("disabled");
}
else{
$("#_signal_green").attr("src", "signal_green_off.png");
$("#_signal_red").attr("src", "signal_red_on.png");
$("#_button_send").attr("disabled", "disabled");
}
});
}
function send_message(){
var message = $("#_message_send").val();
var time_str = get_time_str();
var send = {
"command":"add_message_to_app",
"message": message,
"time": time_str
};
var send_str = JSON.stringify(send);
ws.send(send_str);
var self_msg = "[" + time_str + "] 送信:" + message;
add_message(self_msg);
}
function get_time_str(){
var now = new Date();
var y = now.getFullYear();
var m = padding_zero(now.getMonth() + 1, 2);
var d = padding_zero(now.getDate(), 2);
var h = padding_zero(now.getHours(), 2);
var i = padding_zero(now.getMinutes(), 2);
var s = padding_zero(now.getSeconds(), 2);
var ret = y + "/" + m + "/" + d + " " + h + ":" + i + ":" + s;
return ret;
}
function padding_zero(src, len){
return ("0" + src).slice(-len);
}
$(document).ready(function(){
connect();
});
</script>
</head>
<body>
<div style="display:table;margin-top: 10px; width:600px; height:300px;">
<div style="display:table-row;">
<div id="_message" style="display:table-cell; border: solid 1px gray;">
</div>
</div>
</div>
<div style="display:table;margin-top: 10px; width:600px;">
<div style="display:table-row;">
<div style="display:table-cell; width:150px;">
<span style="font-size:11px;">ネイティブアプリ:</span>
<img id="_signal_green" src="signal_green_off.png" style="width:18px;"
onclick="connect();"/>
<img id="_signal_red" src="signal_red_on.png" style="width:18px; "
onclick="connect();"/>
</div>
<div style="display:table-cell; text-align: right;">
<input type="text" id="_message_send" style="width:80%"/>
<input id="_button_send" type="button" value="送信" onclick="send_message();" disabled/>
</div>
</div>
<div style="display:table-row;">
</div>
</div>
</body>
</html>