-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (56 loc) · 2.42 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//@ts-check
/**@module */
const express = require("express");
require('dotenv').config();
const mongoose = require("mongoose");
const routes = require("./routes");
// const app = express(); // todo: remove if socket.io code below works
const PORT = process.env.PORT || 3001;
// sample code for socket.io
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
// const io = socketIO(server); // where does socketIO come from ?
// io.on('connection', () => { console.log("socket.io loggging started") });
// server.listen(3001);
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Add routes, both API and view
app.use(routes);
// Connect to the Mongo DB
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/reactreadinglist", { useNewUrlParser: true, useUnifiedTopology: true },
() => { console.log('connected to Mongo DB') });
// Start the API server
server.listen(PORT, function () { // todo: uncomment if socket.io doesn't work
console.log(`🌎 ==> API Server now listening on PORT ${PORT}!`);
});
var connections = [];
// window.sessionStorage.setItem("userCount", "1");
// var globalVar;
io.on('connection', socket => {
connections.push(socket);
var userCount = `${connections.length}`;
// window.sessionStorage.setItem("userCount", userCount);
io.sockets.emit(`user arrived`, { msg: userCount })
console.log("Connected: %s sockets connected", userCount);
socket.on("disconnect", data => {
connections.splice(connections.indexOf(data));
var userCount = `${connections.length}`;
// globalVar.callback(data);
// window.sessionStorage.setItem("userCount", userCount);
io.sockets.emit("user left", { msg: userCount });
console.log("Disconnected: %s sockets connected", userCount);
});
socket.on('send message', data => {
io.sockets.emit("new message", { msg: data });
}); // listen to the event
// socket.emit('request', "test socket"); // emit an event to the socket
// io.emit('broadcast', "test all sockets"); // emit an event to all connected sockets
// socket.on('reply', () => { /* … */ }); // listen to the event
});
// io.emit('broadcast', "Socket.io test"); // emit an event to all connected sockets