-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathindex.html
42 lines (38 loc) · 1.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
<!DOCTYPE html>
<html>
<head></head>
<body>
<p>nanomsg ws test: <span></span></p>
<input type='text'>
<button>send</button>
<script>
var ws = new WebSocket('ws://127.0.0.1:7789', [
// see the rfc on sp websocket mapping:
// raw.githubusercontent.com/nanomsg/nanomsg/master/rfc/sp-websocket-mapping-01.txt
'pair.sp.nanomsg.org'
]);
ws.onopen = function open() { ws.send('Ping'); }
ws.onerror = function error(err) { console.log(err); }
ws.onmessage = function message(e) {
var reader = new FileReader();
reader.addEventListener('loadend', function() {
document.querySelector('span').textContent = reader.result;
});
reader.readAsText(e.data);
}
var input = document.querySelector('input');
input.addEventListener('keydown', function(e){
if (e.which === 13) {
return send();
}
}, false);
document
.querySelector('button')
.addEventListener('click', send, false);
function send(e) {
ws.send(input.value); // send input value
input.value = ''; // clear input
}
</script>
</body>
</html>