Repository navigation
Examples HTTP2
This is an example applet implementation with SPDY support. SPDY browser support is still somewhat constrained. Most modern browsers support it as long as it is combined with TLS. Internet Explorer 11 and Edge both support SPDY, but only on Windows 10 and above. Safari supports it on OS X 10.11 and higher. Safari for iOS supports it on iOS 10 and higher.
Within those constraints, SPDY can be swapped in place of HTTPS. However, SPDY provides additional benefits that can be added into an applet.
Change an HTTPS protocol in the server.json configuration file to an SPDY protocol
{
"name": "spdy",
"port": 8443,
"listen": "0.0.0.0",
"websockets": false,
"ssl": true,
"cert": "./encryption/coffeecat.crt",
"key": "./encryption/coffeecat.key"
}
One of the key benefits that SPDY brings is the ability to stream multiple resources in the same connection. This reduces the overhead of creating new connections to fetch each resource. The router can be modified to work with both HTTP/1.1 and SPDY.
First create a function to push a resource into the response stream:
function streamFile(res, filename, contentType) {
return new Promise((resolve, reject) => {
// read the file from the normal "public" file location for this applet
fs.readFile(path.join(__dirname, 'public', filename), (err, data) => {
if (err) {
return reject(err);
}
// push a new stream with the data
var stream = res.push(path.posix.join('/', path.basename(__dirname), '/', filename), {
status: 200,
method: 'GET',
request: {
accept: '*/*'
},
response: {
'content-type': contentType
}
});
stream.end(data);
resolve();
});
});
}
Create an inline route for all requests:
// this route adds the stylesheet and javascript to all requests
router.use(async (req, res, next) => {
// if the protocol is http/2, the response will have a "push" method
if (res.push) {
await streamFile(res, 'style.css', 'text/css');
await streamFile(res, 'main.js', 'application/javascript');
}
// chain to the next handler
next();
});
With these changes, all pages will be delivered with the style sheet and script in-lined.
Copyright (c) 2017 Michael Anderson