Here's the example from before as a test case:

    https://github.com/stephank/node/commit/0339fc0

I went a step further, and experimented with providing a response object
for upgrade and CONNECT requests, also queuing them properly. This is on
a branch at:

    https://github.com/stephank/node/compare/upgrade-response

Notably:

 - The `upgrade` and `connect` event listeners have the same signature
as regular `request` events, namely `(req, res)`.

 - The response can be treated like any other, and `res.end` will treat
it as if with `Connection: close`. (Ie. `shouldKeepAlive = false`).

 - To successfully upgrade, call `res.switchProtocols`, rather than
`res.end`, which takes a callback with the familiar `(sock, head)`.

Example:

    var http = require('http');

    var server = http.createServer();

    server.on('request', function(req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    });

    server.on('upgrade', function(req, res) {
      res.writeHead(101, {
        'Upgrade': 'dummy',
        'Connection': 'Upgrade'
      });
      res.switchProtocols(function(sock, head) {
        /* ... */
      });
    });

    server.listen(3000);

Reply via email to