/
chat.html
78 lines (66 loc) · 2.22 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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>ION Chat</title>
</head>
<body>
<div id="logs">
</div>
<input id="chat" disabled type="text" onkeypress="return keyPress(event)" style="width:100%;" placeholder="Type chat messages here when connected" id="chat" />
<script src="iota-browser.js"></script>
<script src="iota-ion.lib.js"></script>
<script>
// START logging functions (not important)
var logDiv = document.getElementById('logs');
function escapeHTML(unsafe_str) {
return unsafe_str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\"/g, '"')
.replace(/\'/g, ''')
.replace(/\//g, '/')
}
function log(msg) {
logDiv.innerHTML += escapeHTML(msg) + "<br />";
}
// END logging functions
if(!window.location.hash) {
window.location.hash = ION.utils.randomString()
}
var nickname = null;
while(nickname === null) {
nickname = prompt("Please enter a nickname (only known to the people in this room)")
}
function keyPress(e) {
if (e.keyCode == 13) {
var msg = nickname + ": " + e.currentTarget.value
log('Sent ' + msg)
ion.broadcast(msg)
e.currentTarget.value = "";
return false;
}
}
// Make a regular IOTA object for initiating ION
var iota = new IOTA({
provider: 'https://nodes.testnet.iota.org:443/'
})
// The tag is more like your temporary ID. This way we know who is who.
var myTag = ION.utils.randomTag()
// Prefix makes sure that you the same room name / seed will result in different keys
// compared to other services using another prefix. It's mainly conflict-prevention.
var prefix = "rApx0uKKpRyTY8aP"
var ion = new ION(iota, prefix, window.location.hash, myTag)
ion.connect({})
log('ION started! Share "' + window.location.href + '" with another person or put it in second browser tab.')
ion.on('connect', function(obj) {
log('ION connected with '+ obj.user +'!')
document.getElementById('chat').disabled = false
})
ion.on('data', function(obj) {
log(obj.data)
})
</script>
</body>
</html>