-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.html
47 lines (43 loc) · 1.39 KB
/
example.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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>SocketIO Web Service Example</title>
</head>
<body>
<div id="status">Offline</div>
<textarea id="message"></textarea><br/>
<button onclick="sendMessage()">Send</button>
<div id="output"></div>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="http://localhost:1337/socket.io/socket.io.js"></script>
<script>
var deviceId = Math.random().toString().substring(2);
var socket = io("http://localhost:1337/local?token=12345&deviceId=" + deviceId);
socket.on("connect", function() { $("#status").text("Connected"); })
socket.on("disconnect", function() { $("#status").text("Offline"); })
socket.on("error", function(data) { $("#status").text(data); })
socket.on("message", message);
socket.on("_batch", batch);
function batch(events, success) {
events.forEach(function(args) {
window[args[0]].apply(this, args.slice(1));
}, this);
if (success) { success(); }
}
function message(text, success) {
$("#output").append(text + "<br/>");
if (success) { success(); }
}
function sendMessage() {
var url = "/send?site=local&apiKey=54321";
$.post(url, {
rooms: ["user_5"],
event: "message",
args: [$("#message").val()]
});
$("#message").val("");
}
</script>
</body>
</html>