-
Notifications
You must be signed in to change notification settings - Fork 102
/
index.html
73 lines (58 loc) · 2.01 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
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
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Tahoma, Geneva, sans-serif; }
</style>
<script src="colyseus.js"></script>
</head>
<body>
<strong>Messages</strong><br>
<form id="form">
<input type="text" id="input" value="" />
<input type="submit" value="send" />
</form>
<div id="messages"></div>
<script>
var host = window.document.location.host.replace(/:.*/, '');
var client = new Colyseus.Client('ws://' + host + ':8080');
var room;
function request () {
var http = new XMLHttpRequest();
var params = '{"lorem":"ipsum","name":"binny"}';
http.open("POST", "http://localhost:2657/something", true);
// Send the proper header information along with the request
// http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-type", "application/json");
// Call a function when the state changes.
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
}
client.onOpen.add(function() {
console.log("onOpen")
room = client.join('chat');
room.onJoin.add(function() {
console.log(room.id);
console.log('joined!');
})
room.onLeave.add(function() {
room.connection.reconnectEnabled = false;
console.log("LEFT ROOM", arguments);
});
room.onUpdate.add(function(data) {
console.log("chat update: ", data)
});
});
document.getElementById('form').onsubmit = function(e) {
e.preventDefault()
room.send(document.getElementById('input').value);
// room.send(document.getElementById('input').value);
document.getElementById('input').value = null
}
</script>
</body>
</html>