Skip to content

Commit 0d166f5

Browse files
committed
ping timer fro websocket example
1 parent ba3d87a commit 0d166f5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

docs/websocket/websocket.js.html

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<form onsubmit="return false;">
1111
Command : <input type="text" id="command" value="login 7038b715824d45269a3662c9648ffb9f" style="width:500px;"/><br>
1212
<input type="button" value="Send"
13-
onclick="send()" />
13+
onclick="send(command.value)" />
1414

1515
<h3>Output</h3>
1616
<textarea id="output" style="width:500px;height:300px;"></textarea>
@@ -21,6 +21,7 @@ <h3>Output</h3>
2121
<script src="Command.js"></script>
2222

2323
<script type="text/javascript">
24+
const PING_INTERVAL_MILLIS = 5000;
2425
var isRunning = false;
2526

2627
var command = document.getElementById('command');
@@ -36,6 +37,7 @@ <h3>Output</h3>
3637
socket.binaryType = 'arraybuffer';
3738
socket.onmessage = function(event) {
3839
if (event.data instanceof ArrayBuffer) {
40+
console.log(">>>", event.data);
3941
output.value += "Receive : " + messageToDebugString(event.data) + "\r\n";
4042
} else {
4143
output.value = "unexpected type : " + event.data + "\r\n";
@@ -56,6 +58,10 @@ <h3>Output</h3>
5658
alert("Your browser does not support Web Socket.");
5759
}
5860

61+
window.setInterval(function() {
62+
send("ping");
63+
}, PING_INTERVAL_MILLIS);
64+
5965
function messageToDebugString(bufArray) {
6066
var dataview = new DataView(bufArray);
6167
var cmdString = getStringByCommandCode(dataview.getInt8(0));
@@ -65,15 +71,15 @@ <h3>Output</h3>
6571
return "Command : " + cmdString + ", msgId : " + msgId + ", responseCode : " + responseCode;
6672
}
6773

68-
function send() {
74+
function send(data) {
6975
if (!window.WebSocket || !isRunning) {
7076
return;
7177
}
7278

7379
if (socket.readyState == WebSocket.OPEN) {
74-
var commandAndBody = command.value.split(" ");
80+
var commandAndBody = data.split(" ");
7581
var message = createMessage(commandAndBody);
76-
output.value += 'sending : ' + command.value + '\r\n';
82+
output.value += 'sending : ' + data + '\r\n';
7783
socket.send(message);
7884
} else {
7985
alert("The socket is not open.");

0 commit comments

Comments
 (0)