You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2 KiB

  1. (function(){
  2. "use strict";
  3. var fs = require('fs');
  4. // you'll probably load configuration from config
  5. var cfg = {
  6. ssl: true,
  7. port: 8080,
  8. ssl_key: '/path/to/you/ssl.key',
  9. ssl_cert: '/path/to/you/ssl.crt'
  10. };
  11. var httpServ = ( cfg.ssl ) ? require('https') : require('http');
  12. var WebSocketServer = require('../').Server;
  13. var app = null;
  14. // dummy request processing
  15. var processRequest = function( req, res ) {
  16. res.writeHead(200);
  17. res.end("All glory to WebSockets!\n");
  18. };
  19. if ( cfg.ssl ) {
  20. app = httpServ.createServer({
  21. // providing server with SSL key/cert
  22. key: fs.readFileSync( cfg.ssl_key ),
  23. cert: fs.readFileSync( cfg.ssl_cert )
  24. }, processRequest ).listen( cfg.port );
  25. } else {
  26. app = httpServ.createServer( processRequest ).listen( cfg.port );
  27. }
  28. // passing or reference to web server so WS would knew port and SSL capabilities
  29. var wss = new WebSocketServer( { server: app } );
  30. wss.on( 'connection', function ( wsConnect ) {
  31. wsConnect.on( 'message', function ( message ) {
  32. console.log( message );
  33. });
  34. });
  35. }());