Created
May 11, 2020 21:43
-
-
Save flushpot1125/a561f0e9a6d02242793aa1820aab1b97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load required modules | |
const http = require("http"); // http server core module | |
const https = require("https");//Added by limes | |
const fs = require('fs'); | |
const path = require("path"); | |
const express = require("express"); // web framework external module | |
// Set process name | |
process.title = "networked-aframe-server"; | |
// Get port or default to 8080 | |
const port = process.env.PORT || 8080; | |
//Added by limes | |
const https_options ={ | |
key : fs.readFileSync('./certkeys/naf-server.key'), | |
cert : fs.readFileSync('./certkeys/naf-server.crt') | |
}; | |
// Setup and configure Express http server. | |
const app = express(); | |
app.use(express.static(path.resolve(__dirname, "..", "examples"))); | |
// Serve the example and build the bundle in development. | |
if (process.env.NODE_ENV === "development") { | |
const webpackMiddleware = require("webpack-dev-middleware"); | |
const webpack = require("webpack"); | |
const config = require("../webpack.dev"); | |
app.use( | |
webpackMiddleware(webpack(config), { | |
publicPath: "/dist/" | |
}) | |
); | |
} | |
// Start Express http server | |
//const webServer = http.createServer(app); //Comment out by Limes | |
const webServer = https.createServer(https_options,app);//Added by limes | |
const io = require("socket.io")(webServer); | |
const rooms = {}; | |
io.on("connection", socket => { | |
console.log("user connected", socket.id); | |
let curRoom = null; | |
socket.on("joinRoom", data => { | |
const { room } = data; | |
if (!rooms[room]) { | |
rooms[room] = { | |
name: room, | |
occupants: {}, | |
}; | |
} | |
const joinedTime = Date.now(); | |
rooms[room].occupants[socket.id] = joinedTime; | |
curRoom = room; | |
console.log(`${socket.id} joined room ${room}`); | |
socket.join(room); | |
socket.emit("connectSuccess", { joinedTime }); | |
const occupants = rooms[room].occupants; | |
io.in(curRoom).emit("occupantsChanged", { occupants }); | |
}); | |
socket.on("send", data => { | |
io.to(data.to).emit("send", data); | |
}); | |
socket.on("broadcast", data => { | |
socket.to(curRoom).broadcast.emit("broadcast", data); | |
}); | |
socket.on("disconnect", () => { | |
console.log('disconnected: ', socket.id, curRoom); | |
if (rooms[curRoom]) { | |
console.log("user disconnected", socket.id); | |
delete rooms[curRoom].occupants[socket.id]; | |
const occupants = rooms[curRoom].occupants; | |
socket.to(curRoom).broadcast.emit("occupantsChanged", { occupants }); | |
if (occupants == {}) { | |
console.log("everybody left room"); | |
delete rooms[curRoom]; | |
} | |
} | |
}); | |
}); | |
webServer.listen(port, () => { | |
// console.log("listening on http://localhost:" + port); | |
console.log("listening on https://localhost:" + port); //(optional) Modified by limes | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment