'use strict'; 

//
//mqtt
var mqtt = require('mqtt');
var opt = {
  port:1883,
  clientId: 'phonetest'
};
 
//

var fs = require('fs'); 

var cfg = { 
ssl: true, 
port: 443, 
ssl_key: 'ssl.key', ssl_cert: 'ssl.crt' 
}; 

var httpServ = (cfg.ssl) ? require('https') : require('http');
 var WebSocketServer = require('ws').Server;
 var app = null; 

 var processRequest = function(req, res)
 { 
res.writeHead(200);
 res.end('All glory to WebSockets!\n');
 }; 

if (cfg.ssl) {
 app = httpServ.createServer({ 
 key: fs.readFileSync(cfg.ssl_key),
 cert: fs.readFileSync(cfg.ssl_cert) 
}, processRequest).listen(cfg.port); 
} else {
 app = httpServ.createServer(processRequest).listen(cfg.port);
 } 

 var wss = new WebSocketServer({ server: app });
 wss.on('connection', function(wsConnect)
 {


 wsConnect.on('message', function(message)
 { 
	 	 wsConnect.send(message);
 }); 
});


