@canmingir/link-express 1.7.1 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/event-listener.ts +32 -4
- package/package.json +1 -1
- package/src/event/server/server.ts +22 -20
package/bin/event-listener.ts
CHANGED
|
@@ -1,10 +1,38 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
require(
|
|
3
|
+
require("ts-node").register({
|
|
4
4
|
transpileOnly: true,
|
|
5
5
|
compilerOptions: {
|
|
6
|
-
module:
|
|
7
|
-
|
|
6
|
+
module: "commonjs",
|
|
7
|
+
esModuleInterop: true,
|
|
8
|
+
},
|
|
8
9
|
});
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
const command = args[0];
|
|
13
|
+
|
|
14
|
+
function parseArgs(args: string[]) {
|
|
15
|
+
const options: Record<string, string> = {};
|
|
16
|
+
for (let i = 1; i < args.length; i++) {
|
|
17
|
+
if (args[i].startsWith("-p") || args[i].startsWith("--port")) {
|
|
18
|
+
options.port = args[i + 1];
|
|
19
|
+
i++;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return options;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (command === "start") {
|
|
26
|
+
const options = parseArgs(args);
|
|
27
|
+
if (options.port) {
|
|
28
|
+
process.env.PORT = options.port;
|
|
29
|
+
}
|
|
30
|
+
require("../src/event/server/server.ts");
|
|
31
|
+
} else {
|
|
32
|
+
console.log("Usage: event-listener start [-p <port>]");
|
|
33
|
+
console.log("\nCommands:");
|
|
34
|
+
console.log(" start Start the event server");
|
|
35
|
+
console.log("\nOptions:");
|
|
36
|
+
console.log(" -p, --port Port to listen on (default: 8080)");
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
package/package.json
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { Server } from
|
|
4
|
-
import
|
|
5
|
-
import http from 'http';
|
|
3
|
+
import { Server, Socket } from "socket.io";
|
|
4
|
+
import http = require("http");
|
|
6
5
|
|
|
7
6
|
const server = http.createServer();
|
|
8
7
|
const io = new Server(server, {
|
|
9
|
-
cors: { origin:
|
|
8
|
+
cors: { origin: "*" },
|
|
10
9
|
});
|
|
11
10
|
|
|
12
11
|
type Subscriptions = Record<string, Set<string>>;
|
|
13
12
|
const subscriptions: Subscriptions = {};
|
|
14
13
|
|
|
15
|
-
io.on(
|
|
16
|
-
console.log(
|
|
14
|
+
io.on("connection", (socket: Socket) => {
|
|
15
|
+
console.log("Client connected:", socket.id);
|
|
17
16
|
|
|
18
|
-
socket.on(
|
|
17
|
+
socket.on("subscribe", (type: string) => {
|
|
19
18
|
if (!subscriptions[type]) subscriptions[type] = new Set();
|
|
20
19
|
subscriptions[type].add(socket.id);
|
|
21
20
|
console.log(`Socket ${socket.id} subscribed to ${type}`);
|
|
22
21
|
});
|
|
23
22
|
|
|
24
|
-
socket.on(
|
|
23
|
+
socket.on("unsubscribe", (type: string) => {
|
|
25
24
|
if (subscriptions[type]) {
|
|
26
25
|
subscriptions[type].delete(socket.id);
|
|
27
26
|
if (subscriptions[type].size === 0) delete subscriptions[type];
|
|
@@ -29,27 +28,30 @@ io.on('connection', (socket: Socket) => {
|
|
|
29
28
|
}
|
|
30
29
|
});
|
|
31
30
|
|
|
32
|
-
socket.on(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
socket.on(
|
|
32
|
+
"publish",
|
|
33
|
+
({ type, payload }: { type: string; payload: object }) => {
|
|
34
|
+
console.log(`Publish: ${type}`, payload);
|
|
35
|
+
if (subscriptions[type]) {
|
|
36
|
+
subscriptions[type].forEach((sid) => {
|
|
37
|
+
if (sid !== socket.id) {
|
|
38
|
+
io.to(sid).emit("event", { type, payload });
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
40
42
|
}
|
|
41
|
-
|
|
43
|
+
);
|
|
42
44
|
|
|
43
|
-
socket.on(
|
|
45
|
+
socket.on("disconnect", () => {
|
|
44
46
|
Object.keys(subscriptions).forEach((type) => {
|
|
45
47
|
subscriptions[type].delete(socket.id);
|
|
46
48
|
if (subscriptions[type].size === 0) delete subscriptions[type];
|
|
47
49
|
});
|
|
48
|
-
console.log(
|
|
50
|
+
console.log("Client disconnected:", socket.id);
|
|
49
51
|
});
|
|
50
52
|
});
|
|
51
53
|
|
|
52
54
|
const PORT = process.env.PORT || 8080;
|
|
53
55
|
server.listen(PORT, () => {
|
|
54
56
|
console.log(`Event server listening on port ${PORT}`);
|
|
55
|
-
});
|
|
57
|
+
});
|