-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress-socket.js
More file actions
executable file
·134 lines (106 loc) · 3.01 KB
/
express-socket.js
File metadata and controls
executable file
·134 lines (106 loc) · 3.01 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const express = require("express");
const Router = express.Router;
const ExpressRouter = options => {
const router = Router(options);
Router.socket = (path, ...callbacks) => {
router.use(path, ...callbacks.map(callback => (req, res, next) => {
if (req.method === "SOCKET") {
const pathPrefixFinder = path.replace(/\/:\w+/g, "/.*") + "$";
const prefix = req.path.replace(new RegExp(pathPrefixFinder), "");
const fullPath = prefix + path.replace(/\/:\w+/g, "/*");
callback(req, { ...res, send: res.send.socketSend(fullPath) }, next);
} else {
next();
}
}));
};
return router;
};
// Router.socket = () => { };
ExpressRouter();
express.Router = ExpressRouter;
module.exports = (app, server) => (req, res, next) => {
const sendToUsers = (ids, path, p) => {
if (ids.length > 0) {
let sto = io;
ids.forEach(id => {
sto = sto.to(id);
})
sto.emit(path, p);
}
}
const sendToAll = (path, p) => {
io.emit(path, p);
}
const send = socket => {
let toSocketId;
let broadcast = false;
let reqPath = req.path;
const mainSend = p => {
if (!!broadcast) {
sendToAll(reqPath, p);
broadcast = false;
} else {
if (!!toSocketId) {
sendToUsers(toSocketId, reqPath, p);
toSocketId = undefined;
} else {
socket.emit(reqPath, p);
}
}
};
const socketSend = path => {
reqPath = path;
return mainSend;
};
const toUser = (...id) => {
toSocketId = id;
return mainSend;
};
const toAll = p => {
broadcast = true;
mainSend(p);
};
mainSend.socketSend = socketSend;
mainSend.to = toUser;
mainSend.broadcast = toAll;
return mainSend;
};
const getQueryString = (queryString = "") => {
let result = queryString;
result = result.split("?")?.[1];
result = result?.split("&");
result = result?.reduce((res, cur) => ({ ...res, [cur.split("=")[0]]: cur.split("=")[1] }), {});
return result;
};
const io = require('socket.io')(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
credentials: true,
// allowedHeaders: ["my-custom-header"],
},
});
io.on('connection', socket => {
console.log(`User connected: ${socket.id}`);
express.application.socketResponse = (path, p, ...ids) => {
if (ids.length === 0) {
sendToAll(path, p);
} else {
sendToUsers(ids, path, p);
}
};
socket.onAny((eventName, data) => {
const path = "/" + eventName.replace(/^\/+/, "").replace(/\/+$/, "").trim();
req.method = "SOCKET";
req.url = path;
req.socketId = socket.id;
// console.log(path);
const newReq = { ...req, query: getQueryString(eventName), body: data, path };
const newRes = { ...res, setHeader: () => { }, send: send(socket) };
newRes.status = () => newRes;
app._router.handle(newReq, newRes, next);
});
});
next();
};