greenlock-express.js/examples/socket.io.js

33 lines
999 B
JavaScript
Raw Normal View History

2019-04-23 01:50:52 +00:00
// 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.......
2019-06-03 09:47:07 +00:00
"use strict";
2019-04-23 01:50:52 +00:00
//var greenlock = require('greenlock-express');
2019-06-03 09:47:07 +00:00
var greenlock = require("../");
var options = require("./greenlock-options.js");
var socketio = require("socket.io");
2019-04-23 01:50:52 +00:00
var server;
var io;
// Any node http app will do - whether express, raw http or whatever
2019-06-03 09:47:07 +00:00
options.app = require("express")().use("/", function(req, res) {
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("Hello, World!\n\n💚 🔒.js");
2019-04-23 01:50:52 +00:00
});
// 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
2019-06-03 09:47:07 +00:00
io.on("connection", function(socket) {
console.log("a user connected");
socket.emit("Welcome");
2019-04-23 01:50:52 +00:00
2019-06-03 09:47:07 +00:00
socket.on("chat message", function(msg) {
socket.broadcast.emit("chat message", msg);
});
2019-04-23 01:50:52 +00:00
});