-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.mjs
69 lines (60 loc) · 1.5 KB
/
server.mjs
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
import { createRequestHandler } from "@remix-run/express";
import { broadcastDevReady } from "@remix-run/node";
import express from "express";
import { ExpressPeerServer } from "peer";
// notice that the result of `remix build` is "just a module"
import * as build from "./build/index.js";
const app = express();
app.use(express.static("public"));
const server = app.listen(3000, () => {
if (process.env.NODE_ENV === "development") {
broadcastDevReady(build);
}
console.log("App listening on http://localhost:3000");
});
const peerServer = ExpressPeerServer(server, {
debug: true,
path: "/myapp",
});
/**
* @typedef {import("peer").IClient} IClient
*
* @typedef {Object} ClientInfo
* @property {IClient} client
* @property {string} currentRoom
*/
/** @type {Map<string, ClientInfo>} */
export const peerClientsById = new Map();
peerServer.on(
"connection",
/**
* @param {IClient} client
*/
(client) => {
peerClientsById.set(client.getId(), {
client,
currentRoom: undefined,
});
console.log("New peer connected", client.getId());
// client.send({ type: "TEST", payload: "mock" });
},
);
peerServer.on(
"disconnect",
/**
* @param {IClient} client
*/
(client) => {
peerClientsById.delete(client.getId());
console.log("Peer disconnected", client.getId());
},
);
app.use("/peerjs", peerServer);
// the Remix app is "just a request handler"
app.all(
"*",
createRequestHandler({
build,
getLoadContext: () => ({ peerClientsById }),
}),
);