5 <meta charset=
"utf-8" />
6 <title>Spy WS test
</title>
10 background-color:#b0c4de;
11 font:normal
16px/
20px
"Helvetica Neue", Helvetica, sans-serif;
17 background-color:#
0094ff;
24 <header><h1>mojo spy
</h1></header>
26 <input type=
"text" id=
"command" placeholder=
"enter spy command + enter" />
28 <p id=
"status">status: no connection
</p>
31 function openConnection() {
32 if (conn
.readyState
=== undefined || conn
.readyState
> 1) {
33 conn
= new WebSocket('ws://127.0.0.1:42424');
34 conn
.onopen = function () {
35 state
.innerHTML
= 'connected @port 42424';
37 conn
.onmessage = function (event
) {
38 var message
= event
.data
;
39 log
.innerHTML
+= "<br/>" + message
;
41 conn
.onclose = function (event
) {
42 state
.innerHTML
= 'connection closed';
44 conn
.onerror = function (event
) {
45 state
.innerHTML
= 'got error';
50 var addEvent
= (function () {
51 if (document
.addEventListener
) {
52 return function (el
, type
, fn
) {
53 if (el
&& el
.nodeName
|| el
=== window
) {
54 el
.addEventListener(type
, fn
, false);
55 } else if (el
&& el
.length
) {
56 for (var i
= 0; i
< el
.length
; i
++) {
57 addEvent(el
[i
], type
, fn
);
62 return function (el
, type
, fn
) {
63 if (el
&& el
.nodeName
|| el
=== window
) {
64 el
.attachEvent('on' + type
, function () { return fn
.call(el
, window
.event
); });
65 } else if (el
&& el
.length
) {
66 for (var i
= 0; i
< el
.length
; i
++) {
67 addEvent(el
[i
], type
, fn
);
74 var log
= document
.getElementById('log');
75 var cmd
= document
.getElementById('command');
78 var state
= document
.getElementById('status');
80 if (window
.WebSocket
=== undefined) {
81 state
.innerHTML
= 'websockets not supported';
83 addEvent(form
, 'submit', function (event
) {
84 event
.preventDefault();
85 if (conn
.readyState
=== 1) {
86 conn
.send(JSON
.stringify(cmd
.value
));
87 log
.innerHTML
= 'data sent';