forked from jeromeetienne/EasyWebsocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
212 lines (201 loc) · 8.37 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>EasyWebSocket - a WebSocket client to broadcast messages to webpages</title>
<style type="text/css">
body {
margin-top : 1.0em;
background-color: #22252A;
font-family : "Helvetica,Arial,FreeSans";
color : #fff;
}
#container {
margin : 0 auto;
width : 960px;
}
h1 {
font-size : 3.8em;
text-align : center;
margin-top : 20px;
margin-bottom : 20px;
}
h1 .small { font-size: 0.4em; }
h1 a {
color : #cd5;
text-decoration: none
}
h2 {
font-size: 1.5em; color: lightSteelBlue;
}
h3 {
text-align: center; color: #b523ab;
}
a {
color: #cd5;
}
.description {
font-size : 1.2em;
margin-bottom : 20px;
margin-top : 10px;
font-style : italic;
text-align : center;
}
pre {
font-size : 130%;
background : #020202;
color : #ccc;
padding : 15px;
border-radius : 20px;
border : solid;
border-width : 2px;
border-color : #888;
text-shadow : 0px 1px 0px #333;
}
pre span.change {
color : #B43;
}
hr { border : 0;
width : 80%;
border-bottom : 1px solid #aaa;
margin-bottom : 3em;
}
.tutorial {
font-weight : bolder;
}
.tutorial b {
font-size : 1.2em;
}
.footer { text-align:center; padding-top:30px; font-style: italic; }
p {
font-size : 1.2em;
letter-spacing : 0.05em;
word-spacing : 0.1em;
/* text-shadow : 0px 1px 0px #FFF;
*/ }
</style>
</head>
<body>
<div id="container">
<h1>
<a href="http://easywebsocket.org">EasyWebSocket</a>
</h1>
<div class="description">
a WebSocket client to broadcast messages to webpages
</div>
<hr>
<p>
You want to use websockets ? You want to broadcast messages to all connected clients ?
You don't want to worry about browser compatiblity ?
You don't want to set up a server ? Here's all the code you need.
</p>
<pre>
<span class="change"><script src="http://easywebsocket.org/easyWebSocket.min.js"></script></span>
<script>
var socket = new <span class="change">Easy</span>WebSocket("ws://example.com/resource");
socket.onopen = function(){
socket.send("hello world!")
}
socket.onmessage = function(event){
alert("received "+ event.data)
}
</script></pre>
<p>
<b>EasyWebSocket</b> is simple websockets broadcasting. It is perfect
for a quick prototype.. getting it going quick.
90% this is all you need if you're doing websocket something.
The API is copied on the <a href="http://dev.w3.org/html5/websockets/">WebScocket standard API</a>, thus
compatible and easy to learn.
</p>
<p class="tutorial">
<b>Step 1:</b> You connect the socket to a given url
</p>
<p class="tutorial">
<b>Step 2:</b> What you send() thru this socket is sent to all sockets connected to the same url
</p>
<p>
Just include this code in your webpage and it works. See it <a href="example/example.html">live</a>.
For more information, look at <a href="https://docs.google.com/present/view?id=dhng4bgf_47gb6txzds">slides</a> of
a talk i did about it.
Additionnaly you can look at a little <a href="contrib/chat">chat demo</a> on top of it or
another one which <a href="contrib/monitor/">monitor latency in real time</a>.
</p>
<p>
EasyWebSocket is written by <a href="http://jetienne.com">Jerome Etienne</a>.
The code is available on github at <a href="http://github.com/jeromeetienne/EasyWebsocket">EasyWebSocket</a>
under <a href="https://github.com/jeromeetienne/EasyWebsocket/raw/master/MIT-LICENSE.txt">MIT license</a>.
</p>
</div>
<!-- multiple mouse cursor by @stagas -->
<style>
.cursor {
position : absolute;
width : 24px;
height : 24px;
background-image:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAB3RJTUUH1wYXBxAQ01sIAQAAAAlwSFlzAAAewQAAHsEBw2lUUwAAAARnQU1BAACxjwv8YQUAAAT1SURBVHja7VZLSFxnFD4zOqPjOxqbOqNRfEUTrVQLBaHBghA0ER9BpHSnqAu7kWQhSlK0UrCUunUhriyCYhvUhY+gImpQic9WZBqjUaPGGcfXOA9nnNvvXO8EGywdjd154GPu3Lnz/9//ne+cc4mI5EDgzZs3v6qpqflxf39/z+FwCIeHh6sjIyPfPX36VNPU1OSRlpYmo/8heFEPICY0NDS1v7+/EkTCQYAEQSB3d3fH7u5uV0dHxw8HBwd/+fv7G4uKio4vk4Ab4AfEYfG4kpKSu56enqry8nLq7u6muLg4WXBw8K3ExMSHgYGB5uHh4eWCggKbt7f38fz8/KUQYPkVgMpmsynX19cN+KStrS1qbW2le/fuUWNjI5nN5qDo6OifQOxXEPoShK719fW5XZYCnAK1UqlUp6amhsTExITy6V68eEF7e3s0NDREODnhviwsLCwCqjxEmuRIy6sHDx7Y8P14fHxc+BgFbMDh0dGRdWFhQSeXyyk+Pp7c3NzIbrcTzEijo6OUl5dHT548IZ1O56vRaKoePXr0++3bt7+GIoFIl/vHKMAkrgEalUrllZ+fn8wmbGlpIYvFIj50fHxMIEgzMzPU2dlJ2JRiY2NZrbyoqKgA3NNmZGRYMzMz7T09PedWgz0Qh5OXRkRE/IITmjc2NgS1Wi3IZDJe7B9QKBSCr6+vkJOTI0xOTgpGo1FAqrQDAwPfVFdXf9LQ0KCkk+pyWQEOFcpOA3dfz83NjQ4ICPB7/vw5ra2tEatxOvg7p2ZpaYna29vF66SkpKDIyMgc+EEzMTExc//+fSOUcKlc5dLJWGsjZLatrq7qnT5AHzjzT0yC0wO1qK6ujrKysmhwcNANpfwton56ejrYVRXk0qcVOLBarUdarVZ/2ojvpcI15BfBvzu9YTKZaHZ2lgoLC6mtrY2g3l2UswY/K8+jgLMSLFhs6ywF+Lq0tJTS09MJJyU0LJEU32dSkJ9QFazKNgj4S976z3DuYOcUoAmZxsbG9JDYAYfLeWEYUWzLvBnuiaW4uLhIzc3N1NXVRTAklZWViWWKdm3A7Him1+vtrprQSYANY4KkJjjaajAYdvz8/ILCw8NpZ2dHlJpJcIdEqVJCQoKttrZW8fjxY/H0UMOBKniJzYc2NzdfoXfoJFVdJsBWNzmNCAm3kcsglnVubk4kwMZbWVkRr9EFR0HSis4Zg+ftKL2x3t7eN1DgLcz5J9ZZB47OQ8BpRJHA69ev9Xfu3IllApxjng+cAlaDU4LB5FtZWTkF086BmA2fe9j4DchpscYysCt5y2UCwikCFswCXXZ2tmgqNhtvmpycTFVVVWIFwAMbSJUOTciI/+wDq8AS8A4wS4qeywPvjQgCZgyXd9hIwKYyNh4aC2FUMxkBg2mqvr5+EA2I5/GmRMAAHEhrXHgwcdncwmlLQkJCfl5eXjbATM5WK8AXBxUVFb/duHHjex8fn1w8GwZ40kk3vfDb0ocznUfzpyARhHreSUlJ0Xh4eCimpqYWiouLn6G9avHK9gr5/gPPvZXS5rLcZ4Xsg2t+O/oc8n/h5eUVjNmg5OGDDS3oeFt4MdGiHNloG3RSNReW+ywCHOwJNfAZEAl40Uk9s7kW6cTh29K9j978LAL8nXv4dU4F4CPJrJNwSCdN69Li38zD3lBIinCOj6SNL+XUV3EVV3EVp+NvS2d/EQYPBEcAAAAASUVORK5CYII=");
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="./easyWebSocket.min.js"></script>
<script>
jQuery(function() {
var socket = new EasyWebSocket("ws://cursors/moving/smooth")
, me = Math.floor(Math.random() * 1000000).toString(32)
, cursors = {}
, tail = [];
cursors[me] = jQuery('<div/>').attr('id', me).addClass('cursor').appendTo('body');
socket.onopen = function() {
setInterval(function(){
if( mouseCurX == mouseOldX && mouseCurY == mouseOldY ) return;
socket.send(me + ' ' + tail.join(' '));
tail = [];
mouseOldX = mouseCurX;
mouseOldY = mouseCurY;
}, 100)
}
socket.onmessage = function(event) {
var data = event.data.split(' ')
, id = data.shift();
if (id === me) return
if (typeof cursors[id] === 'undefined') {
cursors[id] = jQuery('<div/>').attr('id', id).addClass('cursor').appendTo('body');
}
var inc = 0
, mul = Math.floor(100 / (data.length || 1));
data.forEach(function(xy) {
inc++;
setTimeout(function() {
xy = xy.split(',');
cursors[id].css({ left: xy[0] + 'px', top: xy[1] + 'px' });
}, mul * inc);
})
}
var mouseCurX = 0;
var mouseCurY = 0;
var mouseOldX = 0;
var mouseOldY = 0;
$('body').bind('mousemove', function(e){
mouseCurX = e.pageX;
mouseCurY = e.pageY;
tail.push([ mouseCurX, mouseCurY ]);
cursors[me].css({ left: (mouseCurX+2) + 'px', top: (mouseCurY+3) + 'px' });
})
})
</script>
<!-- google analytic support -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-4037844-15']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- follow me on twitter -->
<a href="http://www.twitter.com/jerome_etienne"><img style="position: absolute; top: 10px; right: 10px; border: 0;" src="http://twitter-badges.s3.amazonaws.com/follow_me-c.png" alt="Follow jerome_etienne on Twitter"/></a>
<!-- github ribbon -->
<a href="https://github.com/jeromeetienne/easywebsocket"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://assets0.github.com/img/ce742187c818c67d98af16f96ed21c00160c234a?repo=&url=http%3A%2F%2Fs3.amazonaws.com%2Fgithub%2Fribbons%2Fforkme_left_gray_6d6d6d.png&path=" alt="Fork me on GitHub"></a>
</body>
</html>