-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
90 lines (87 loc) · 3.09 KB
/
chat.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
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<script src="fluiditypeer.js"></script>
<script src="peerjs.js"></script>
<script src="/tenvoy/cryptico.js"></script>
<script src="/tenvoy/tenvoy.js"></script>
</head>
<body>
<main style="display: none">
<p id="id">FluidityPeer ID: </p>
<input id="conn_id" placeholder="FluidityPeer ID" /><button onclick="connect()" id="conn_id_btn">Connect</button>
<input id="message" style="display: none;" placeholder="Message" /><button onclick="send()" style="display: none;" id="message_btn">Send</button>
<p id="conversation"></p>
</main>
<script>
async function init() {
window.peer = await new FluidityPeer();
peer.onReceive = (data, conn_id, peer_id) => {
document.querySelector("#conversation").innerHTML += `
<p>
<b>` + peer_id + `: </b>` + data + `
</p>
<hr>
`;
}
peer.onConnect = (conn_id, peer_id) => {
window.connected_peer = new FluidityConnection(peer.peer.connections[peer_id].find(c => c.connectionId == conn_id));
document.querySelector("#conn_id").style.display = "none";
document.querySelector("#conn_id_btn").style.display = "none";
document.querySelector("#message").style.display = "block";
document.querySelector("#message_btn").style.display = "block";
document.querySelector("#conversation").innerHTML += `
<p>
<b>New Connection: </b>` + peer_id + `
</p>
<hr>
`;
}
peer.onDisconnect = (conn_id, peer_id) => {
window.connected_peer = null;
document.querySelector("#conn_id").style.display = "block";
document.querySelector("#conn_id_btn").style.display = "block";
document.querySelector("#message").style.display = "none";
document.querySelector("#message_btn").style.display = "none";
document.querySelector("#conversation").innerHTML += `
<p>
<b>Closed Connection: </b>` + peer_id + `
</p>
<hr>
`;
}
document.querySelector("#id").innerHTML = "FluidityPeer ID: " + peer.id;
document.querySelector("main").style.display = "block";
}
async function connect() {
let peer_id = document.querySelector("#conn_id").value;
document.querySelector("#conn_id").value = "";
window.connected_peer = await peer.connect(peer_id);
document.querySelector("#conn_id").style.display = "none";
document.querySelector("#conn_id_btn").style.display = "none";
document.querySelector("#message").style.display = "block";
document.querySelector("#message_btn").style.display = "block";
document.querySelector("#conversation").innerHTML += `
<p>
<b>New Connection: </b>` + peer_id + `
</p>
<hr>
`;
}
async function send() {
let message = document.querySelector("#message").value;
if(window.connected_peer != null) {
document.querySelector("#message").value = "";
await window.connected_peer.send(message);
document.querySelector("#conversation").innerHTML += `
<p>
<b>` + window.peer.id + `: </b>` + message + `
</p>
<hr>
`;
}
}
init();
</script>
</body>
</html>