Fixed so io is being sent in a better manner, and listening on the same port as the rest

This commit is contained in:
Kasper Rynning-Tønnesen
2020-03-13 17:02:28 +01:00
parent b35dd5a971
commit bfbfa5a8bc
3 changed files with 10 additions and 7 deletions

View File

@@ -5,7 +5,7 @@ const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/vinlottis", {
useNewUrlParser: true
});
const io = require("socket.io")(8080);
let io;
const mustBeAuthenticated = require(path.join(
__dirname + "/../middleware/mustBeAuthenticated"
));
@@ -68,7 +68,6 @@ router.route("/winner").get(mustBeAuthenticated, async (req, res) => {
colorToChoseFrom = "yellow";
break;
}
io.emit("color_winner", { color: colorToChoseFrom });
findObject[colorToChoseFrom] = { $gt: 0 };
@@ -169,7 +168,6 @@ router.route("/attendee").post(mustBeAuthenticated, async (req, res) => {
yellow,
phoneNumber: attendee.phoneNumber
});
console.log(newAttendee);
await newAttendee.save();
io.emit("new_attendee", {});
@@ -177,4 +175,7 @@ router.route("/attendee").post(mustBeAuthenticated, async (req, res) => {
res.send(true);
});
module.exports = router;
module.exports = function(_io) {
io = _io;
return router;
};

View File

@@ -1,5 +1,7 @@
const express = require("express");
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
const path = require("path");
const session = require("express-session");
const User = require(path.join(__dirname + "/schemas/User"));
@@ -83,7 +85,7 @@ app.use("/", loginApi);
app.use("/api/", updateApi);
app.use("/api/", retrieveApi);
app.use("/api/", wineinfoApi);
app.use("/api/virtual/", virtualApi);
app.use("/api/virtual/", virtualApi(io));
app.use("/subscription", subscriptionApi);
app.get("/dagens", function(req, res) {
@@ -94,4 +96,4 @@ app.use("/service-worker.js", function(req, res) {
res.sendFile(path.join(__dirname, "public/sw/serviceWorker.js"));
});
app.listen(30030);
server.listen(30030);

View File

@@ -66,7 +66,7 @@ export default {
mounted() {
this.getAttendees();
this.getWinners();
this.socket = io(window.location.hostname + ":8080");
this.socket = io(`${window.location.hostname}:${window.location.port}`);
this.socket.on("color_winner", msg => {
this.currentWinnerDrawn = true;
this.currentWinnerColor = msg.color;