-
Notifications
You must be signed in to change notification settings - Fork 3
/
p2pwebseedsclient.html
89 lines (75 loc) · 2.42 KB
/
p2pwebseedsclient.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
Welcome to P2P Web
<!--
<script src="/ws/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost', {resource: 'ws'});
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
-->
Address: <input id="address" type="text" value="ws://localhost:1338/ws/p2pwebseeds" size="100"/>
<br />
Status: <input id="status" type="text" value="" size="100" disabled/>
<br />
Message: <input id="message" type="text" value="" size="100"/>
<br />
<input id="encodejson" type="checkbox" checked/> Encode to JSON
<hr />
<input id="connect" type="button" value="connect" />
<input id="send" type="button" value="send" />
<input id="close" type="button" value="close" />
<script>
// let's invite Firefox to the party.
if (window.MozWebSocket) {
window.WebSocket = window.MozWebSocket;
}
var address = document.getElementById("address");
var status = document.getElementById("status");
var message = document.getElementById("message");
var encodejson = document.getElementById("encodejson");
var connect = document.getElementById("connect");
var send = document.getElementById("send");
var close = document.getElementById("close");
var sock;
connect.onclick = function(){
var addr = address.value;
console.log("Connecting to " + addr + "...");
status.value = "Connecting to " + addr + "...";
sock = new WebSocket(addr);
send.onclick = function(){
status.value = "Send message to " + addr;
console.log("Send message to " + addr);
var msg = message.value
if(encodejson.checked) msg = JSON.stringify(msg);
console.log(msg);
sock.send(msg);
}
close.onclick = function(){
sock.close();
}
sock.onopen = function(){
status.value = "Connected to " + addr;
console.log("Connected to " + addr);
}
sock.onclose = function(event){
status.value = "Closed connection to " + addr;
console.log("Closed connection to " + addr);
console.log(event);
}
sock.onerror = function(event){
status.value = "Error from " + addr;
console.log("Error from " + addr);
console.log(event);
}
sock.onmessage = function(event){
status.value = "Received message from " + addr;
message.value = event.data;
console.log("Received:");
console.log(event);
if(encodejson.checked) console.log(JSON.parse(event.data));
else console.log(event.data);
}
};
</script>