-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (37 loc) · 1.33 KB
/
server.js
File metadata and controls
48 lines (37 loc) · 1.33 KB
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
const express = require('express');
const socket = require("./express-socket");
const path = require("path");
const router = express.Router();
const app = express();
const userIds = [];
router.socket("/test1/:fname/:lname", (req, res, next) => {
// global.sendToAll = res.send.broadcast;
if (!userIds.some(uid => uid === req.socketId)) {
userIds.push(req.socketId);
}
const otherIds = userIds.filter(uid => uid !== req.socketId);
res.send.to(...otherIds)("Hi others");
res.send.broadcast("Hi evryone");
res.send({ method: req.method, query: req.query, body: req.body, param: req.params, socketId: req.socketId });
next();
});
router.socket("/test1/:fname/:lname", (req, res, next) => {
res.send("Hi");
});
router.get("/test1/:fname/:lname", (req, res) => {
const socket = express().socketResponse;
if (userIds.length > 0) socket("/test2", "He he", userIds);
if (!!socket) socket("/test2", "Ho ho");
res.send({ method: req.method, query: req.query, body: req.body, param: req.params, socketId: req.socketId });
});
router.get("/", (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
const server = app.listen(8000, () => {
console.log(`🚀 Server running at port:8000`);
});
app.use(router);
app.use(socket(app, server));
app.use((req, res) => {
res.status(400).send({ result: "not found" });
})