From 1b42d866fbfd34db8ee77d565e69bf8b792d133c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 22 Apr 2019 19:50:52 -0600 Subject: [PATCH] add socket.io example --- examples/socket.io.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 examples/socket.io.js diff --git a/examples/socket.io.js b/examples/socket.io.js new file mode 100644 index 0000000..6d4cb0e --- /dev/null +++ b/examples/socket.io.js @@ -0,0 +1,32 @@ +// First and foremost: +// I'm not a fan of `socket.io` because it's huge and complex. +// I much prefer `ws` because it's very simple and easy. +// That said, it's popular....... +'use strict'; + +//var greenlock = require('greenlock-express'); +var greenlock = require('../'); +var options = require('./greenlock-options.js'); +var socketio = require('socket.io'); +var server; +var io; + +// Any node http app will do - whether express, raw http or whatever +options.app = require('express')().use('/', function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=utf-8'); + res.end('Hello, World!\n\nšŸ’š šŸ”’.js'); +}); + +// The server that's handed back from `listen` is a raw https server +server = greenlock.create(options).listen(80, 443); +io = socketio(server); + +// Then you do your socket.io stuff +io.on('connection', function (socket) { + console.log('a user connected'); + socket.emit('Welcome'); + + socket.on('chat message', function (msg) { + socket.broadcast.emit('chat message', msg); + }); +});