10
10
< form onsubmit ="return false; ">
11
11
Command : < input type ="text " id ="command " value ="login 7038b715824d45269a3662c9648ffb9f " style ="width:500px; "/> < br >
12
12
< input type ="button " value ="Send "
13
- onclick ="send() " />
13
+ onclick ="send(command.value ) " />
14
14
15
15
< h3 > Output</ h3 >
16
16
< textarea id ="output " style ="width:500px;height:300px; "> </ textarea >
@@ -21,6 +21,7 @@ <h3>Output</h3>
21
21
< script src ="Command.js "> </ script >
22
22
23
23
< script type ="text/javascript ">
24
+ const PING_INTERVAL_MILLIS = 5000 ;
24
25
var isRunning = false ;
25
26
26
27
var command = document . getElementById ( 'command' ) ;
@@ -36,6 +37,7 @@ <h3>Output</h3>
36
37
socket . binaryType = 'arraybuffer' ;
37
38
socket . onmessage = function ( event ) {
38
39
if ( event . data instanceof ArrayBuffer ) {
40
+ console . log ( ">>>" , event . data ) ;
39
41
output . value += "Receive : " + messageToDebugString ( event . data ) + "\r\n" ;
40
42
} else {
41
43
output . value = "unexpected type : " + event . data + "\r\n" ;
@@ -56,6 +58,10 @@ <h3>Output</h3>
56
58
alert ( "Your browser does not support Web Socket." ) ;
57
59
}
58
60
61
+ window . setInterval ( function ( ) {
62
+ send ( "ping" ) ;
63
+ } , PING_INTERVAL_MILLIS ) ;
64
+
59
65
function messageToDebugString ( bufArray ) {
60
66
var dataview = new DataView ( bufArray ) ;
61
67
var cmdString = getStringByCommandCode ( dataview . getInt8 ( 0 ) ) ;
@@ -65,15 +71,15 @@ <h3>Output</h3>
65
71
return "Command : " + cmdString + ", msgId : " + msgId + ", responseCode : " + responseCode ;
66
72
}
67
73
68
- function send ( ) {
74
+ function send ( data ) {
69
75
if ( ! window . WebSocket || ! isRunning ) {
70
76
return ;
71
77
}
72
78
73
79
if ( socket . readyState == WebSocket . OPEN ) {
74
- var commandAndBody = command . value . split ( " " ) ;
80
+ var commandAndBody = data . split ( " " ) ;
75
81
var message = createMessage ( commandAndBody ) ;
76
- output . value += 'sending : ' + command . value + '\r\n' ;
82
+ output . value += 'sending : ' + data + '\r\n' ;
77
83
socket . send ( message ) ;
78
84
} else {
79
85
alert ( "The socket is not open." ) ;
0 commit comments