@webgamekit/multiplayer-server 0.0.0-stage → 0.1.0
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/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +11 -0
- package/dist/server-O9Ueo7hO.js +58 -0
- package/dist/server.d.ts +19 -0
- package/dist/types.d.ts +33 -0
- package/package.json +34 -3
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { m as s } from "./server-O9Ueo7hO.js";
|
|
2
|
+
const t = 3e3, e = Number(process.env.PORT ?? t), { httpServer: o, cleanup: n } = s();
|
|
3
|
+
o.listen(e, () => {
|
|
4
|
+
process.stdout.write(`multiplayer-server listening on port ${e}
|
|
5
|
+
`);
|
|
6
|
+
});
|
|
7
|
+
const r = () => {
|
|
8
|
+
n(), process.exit(0);
|
|
9
|
+
};
|
|
10
|
+
process.on("SIGTERM", r);
|
|
11
|
+
process.on("SIGINT", r);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createServer as u } from "node:http";
|
|
2
|
+
import { Server as d } from "socket.io";
|
|
3
|
+
const s = (n, e) => {
|
|
4
|
+
n.emit("user:list", Object.values(e));
|
|
5
|
+
}, l = (n, e, t, o, a) => {
|
|
6
|
+
const c = {
|
|
7
|
+
id: n.id,
|
|
8
|
+
name: n.id,
|
|
9
|
+
position: { x: 0, y: 0, z: 0 },
|
|
10
|
+
rotation: { x: 0, y: 0, z: 0 }
|
|
11
|
+
};
|
|
12
|
+
if (o({ ...t(), [n.id]: c }), a.onConnect) {
|
|
13
|
+
const r = a.onConnect(n.id);
|
|
14
|
+
Object.entries(r).forEach(([i, m]) => {
|
|
15
|
+
n.emit(i, m);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
s(e, t()), n.on("user:updated", (r) => {
|
|
19
|
+
const i = t()[n.id] ?? c;
|
|
20
|
+
o({
|
|
21
|
+
...t(),
|
|
22
|
+
[n.id]: {
|
|
23
|
+
...i,
|
|
24
|
+
name: r.name ?? i.name,
|
|
25
|
+
position: r.position,
|
|
26
|
+
rotation: r.rotation
|
|
27
|
+
}
|
|
28
|
+
}), e.emit("user:list", Object.values(t()));
|
|
29
|
+
}), n.on("disconnect", () => {
|
|
30
|
+
const { [n.id]: r, ...i } = t();
|
|
31
|
+
o(i), s(e, t());
|
|
32
|
+
});
|
|
33
|
+
}, p = (n, e = {}) => {
|
|
34
|
+
let t = {};
|
|
35
|
+
const o = () => t, a = (r) => {
|
|
36
|
+
t = r;
|
|
37
|
+
}, c = (r) => {
|
|
38
|
+
l(r, n, o, a, e);
|
|
39
|
+
};
|
|
40
|
+
return n.on("connection", c), {
|
|
41
|
+
cleanup: () => {
|
|
42
|
+
n.off("connection", c), t = {};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}, b = (n = {}) => {
|
|
46
|
+
const e = u(), t = new d(e, { cors: n.cors ?? { origin: "*" } }), { cleanup: o } = p(t, n);
|
|
47
|
+
return {
|
|
48
|
+
server: t,
|
|
49
|
+
httpServer: e,
|
|
50
|
+
cleanup: () => {
|
|
51
|
+
o(), t.close(), e.close();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
export {
|
|
56
|
+
p as a,
|
|
57
|
+
b as m
|
|
58
|
+
};
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Server } from 'socket.io';
|
|
2
|
+
import type { MultiplayerServerConfig, MultiplayerServerHandle } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Attach multiplayer session handling to a Socket.IO server the caller already owns.
|
|
5
|
+
* Use this when the application has its own HTTP server; otherwise use multiplayerServerCreate.
|
|
6
|
+
* @param io - A Socket.IO Server instance
|
|
7
|
+
* @param config - Optional server configuration
|
|
8
|
+
* @returns cleanup function that removes the connection listener
|
|
9
|
+
*/
|
|
10
|
+
export declare const multiplayerServerAttach: (io: Server, config?: MultiplayerServerConfig) => {
|
|
11
|
+
cleanup: () => void;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Create a Socket.IO server together with the HTTP server it rides on, with multiplayer
|
|
15
|
+
* session handling already attached. The caller chooses the port via httpServer.listen().
|
|
16
|
+
* @param config - Optional server configuration
|
|
17
|
+
* @returns The Socket.IO server, its HTTP server, and a cleanup function
|
|
18
|
+
*/
|
|
19
|
+
export declare const multiplayerServerCreate: (config?: MultiplayerServerConfig) => MultiplayerServerHandle;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Server, ServerOptions } from 'socket.io';
|
|
3
|
+
import type { Server as HttpServer } from 'node:http';
|
|
4
|
+
export interface PlayerPosition {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
z: number;
|
|
8
|
+
}
|
|
9
|
+
export interface PlayerRotation {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
z: number;
|
|
13
|
+
}
|
|
14
|
+
export interface PlayerState {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
position: PlayerPosition;
|
|
18
|
+
rotation: PlayerRotation;
|
|
19
|
+
}
|
|
20
|
+
export interface MultiplayerServerConfig {
|
|
21
|
+
/** Called when a player connects, returns initial data to send them */
|
|
22
|
+
onConnect?: (socketId: string) => Record<string, unknown>;
|
|
23
|
+
/** CORS policy handed to Socket.IO, default: `{ origin: '*' }` */
|
|
24
|
+
cors?: ServerOptions['cors'];
|
|
25
|
+
}
|
|
26
|
+
export interface MultiplayerServerHandle {
|
|
27
|
+
/** The Socket.IO server */
|
|
28
|
+
server: Server;
|
|
29
|
+
/** The HTTP server it rides on — call `listen()` on this to start accepting players */
|
|
30
|
+
httpServer: HttpServer;
|
|
31
|
+
/** Stop listening, drop every handler, and empty the registry */
|
|
32
|
+
cleanup: () => void;
|
|
33
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webgamekit/multiplayer-server",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/cnotv/generative-art.git",
|
|
7
|
+
"directory": "packages/multiplayer-server"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"socket.io": "^4.8.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^22.0.0",
|
|
29
|
+
"typescript": "^5.0.0",
|
|
30
|
+
"vite": "^5.0.0",
|
|
31
|
+
"vitest": "^4.0.18"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "vite build && tsc",
|
|
35
|
+
"start": "node dist/main.js"
|
|
36
|
+
}
|
|
6
37
|
}
|
package/README.md
DELETED