HTML and CSS Reference
In-Depth Information
header
.
writeUInt32LE
(
36
+
samples
.
length
,
4
,
true
);
header
.
writeUInt32LE
(
samples
.
length
,
40
,
true
);
var
data
=
new
Buffer
(
header
.
length
+
samples
.
length
);
header
.
copy
(
data
);
samples
.
copy
(
data
,
header
.
length
);
return
data
;
}
function
makeSamples
(
frequency
,
duration
)
{
var
samplespercycle
=
44100
/
frequency
;
var
samples
=
new
Uint16Array
(
44100
*
duration
);
var
da
=
2
*
Math
.
PI
/
samplespercycle
;
for
(
var
i
=
0
,
a
=
0
;
i
<
samples
.
length
;
i
++
,
a
+=
da
)
{
samples
[
i
]
=
Math
.
floor
(
Math
.
sin
(
a
/
300000
)
*
32768
);
}
return
getSoundBuffer
(
new
Buffer
(
Array
.
prototype
.
slice
.
call
(
samples
,
0
)));
}
app
.
use
(
express
.
static
(
__dirname
+
'/public'
));
app
.
listen
(
8080
);
var
wss
=
new
WebSocketServer
({
server
:
app
,
path
:
'/data'
});
var
samples
=
makeSamples
(
20000
,
10
);
wss
.
on
(
'connection'
,
function
(
ws
)
{
ws
.
on
(
'message'
,
function
(
message
)
{
ws
.
send
(
'pong'
);
});
ws
.
send
(
samples
,
{
binary
:
true
});
});
Managing Proxies
With new technology comes a new set of problems. In the case of WebSockets, the
challenges relate to compatibility with the proxy servers that mediate HTTP connections
in most company networks. A firewall, proxy server, or switch always is the lynchpin of
an enterprise, and these devices and servers limit the kind of traffic you're allowed to
send to and from the server.
The WebSocket protocol uses the HTTP upgrade system (which is normally used for
HTTPS/SSL) to “upgrade” an HTTP connection to a WebSocket connection. Some
proxy servers are not able to handle this handshake and will drop the connection. So,
even if a given client uses the WebSocket protocol, it may not be possible to establish a
connection.