HTML and CSS Reference
In-Depth Information
var
sock
=
new
SockJS
(
'http://mydomain.com/my_prefix'
);
sock
.
onopen
=
function
()
{
console
.
log
(
'open'
);
};
sock
.
onmessage
=
function
(
e
)
{
console
.
log
(
'message'
,
e
.
data
);
};
sock
.
onclose
=
function
()
{
console
.
log
(
'close'
);
};
Socket.IO
Specifically built for use with a
Node.js
server, Socket.IO (
http://socket.io
)
has the capa‐
bility to be used with any backend after you set fallback capabilities via Flash. Socket.IO
aims to make real-time apps possible in every browser and mobile device, blurring the
differences between the different transport mechanisms. Specifically, Socket.IO sup‐
ports iOS, Android, WebOs, and WebKit License, and is under MIT license.
The page setup for Socket.IO is simple:
<!DOCTYPE html>
<html>
<head>
<title>
my app
</title>
</head>
<body>
<script
src=
"http://cdn.socket.io/stable/socket.io.js"
></script>
</body>
</html>
To set up a server, use:
var
io
=
require
(
'socket.io'
).
listen
(
80
);
io
.
sockets
.
on
(
'connection'
,
function
(
socket
)
{
socket
.
emit
(
'news'
,
{
hello
:
'world'
});
socket
.
on
(
'my other event'
,
function
(
data
)
{
console
.
log
(
data
);
});
});
Finally, set up your client with:
var
socket
=
io
.
connect
(
'http://localhost'
);
socket
.
on
(
'news'
,
function
(
data
)
{
console
.
log
(
data
);
socket
.
emit
(
'my other event'
,
{
my
:
'data'
});
});