mcollina / mows

Using MQTT.js in the browser over WebSocket -- Built with browserify!
72 stars 12 forks source link

Https #3

Closed ldstein closed 10 years ago

ldstein commented 10 years ago

This patch is a resubmit of PR https://github.com/mcollina/mows/pull/2 minus all the weird commit issues I had earlier. It adds support for secure connections.

I have included updated unit tests for Node clients. Note I haven't included any browser-side tests. Not quite sure how to achieve this as the server relies on self-signed certificates. This requires the user to install/approve the certificate in their browser.

It includes a new method, createSecureServer:


var clientHandler = function(client) {
 // Handle client
}

var secureOpts = {
    key:  fs.readFileSync('./cert/my.webserver.com.key'),
    cert: fs.readFileSync('./cert/my.webserver.com.cert')
}

mows.createSecureServer(secureOpts, clientHandler).listen(666);

To create a secure browser client, specify the 'wss' protocol:

// ws is unsecure, wss is secure:
var unsecureClient = mows.createClient(665, 'ws://my.webserver.com');
var secureClient    = mows.createClient(666, 'wss://my.webserver.com');

To create a secure Node client, define a protocol object and pass appropriate certificates, authentication options, etc:


// See http://nodejs.org/api/https.html#https_https_request_options_callback
var secureClientOpts =
{
    protocol:
    {
        ca: fs.readFileSync('./cert/24485013-my.webserver.com.cert')
    }
};

var secureClient = mows.createClient(666, 'wss://my.webserver.com', secureClientOpts);
mcollina commented 10 years ago

Thanks! Much better. I'll see what I can do for automatic browser testing.

mcollina commented 10 years ago

Thanks! I published it as 0.0.2!