forked from Flynsarmy/PHPWebSocket-Chat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
87 lines (77 loc) · 2.24 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!doctype html>
<html>
<head>
<meta charset='UTF-8' />
<title>PHPWebSocket and TinyMCE</title>
<style>
body {margin:20px;font-family:Verdana, Geneva, sans-serif;}
#editor {height: 240px;}
</style>
<script src="https://cdn.tinymce.com/4/tinymce.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="fancywebsocket.js"></script>
<script>
var Server;
function send(text) {
Server.send( 'message', text );
}
$(document).ready(function() {
Server = new FancyWebSocket('ws://127.0.0.1:9300');
tinymce.init({
selector:"#editor",
setup: function(ed) {
ed.on("change", function(e) {
send(ed.getContent());
});
ed.on("keyup", function(e) {
send(ed.getContent());
});
}
});
// let the user know we're connected
Server.bind("open", function() {
$("#status").text("connected");
});
// disconnection occurred
Server.bind("close", function(data) {
$("#status").text("disconnected");
});
// process messages sent from server
Server.bind("message", function(payload) {
// process new user
if (payload.indexOf("[CLIENT:JOINED]") === 0) {
var p = payload.split(" ");
$("#users").append("<li id='"+ p[1] + "'>" + p[2] + "</li>");
// user left
} else if (payload.indexOf("[CLIENT:LEFT]") === 0) {
var p = payload.split(" ");
$("#"+ p[1]).remove();
// show active users when joined
} else if (payload.indexOf("[CLIENTS:LIST]") === 0) {
var p = payload.split(" ");
if (p[1] && p[1].length !== 0) {
var client = p[1].split(";");
for (var i = 0; i < client.length; i++) {
if (client[i] && client[i].length !== 0) {
var c = client[i].split(",");
$("#users").append("<li id='"+ c[0] + "'>" + c[1] + "</li>");
}
}
}
} else {
tinymce.activeEditor.setContent(payload);
}
});
Server.connect();
});
</script>
</head>
<body>
<div id="status-wrapper">
Status: <span id="status"></span>
</div>
<textarea id="editor"></textarea>
<h3>Users:</h3>
<ul id="users"></ul>
</body>
</html>