@nanoforge-dev/network-server
v1.0.0
Published
NanoForge Engine - Network Server
Readme
About
@nanoforge-dev/network-server is a network lib with tcp and udp support for server-side usage.
Installation
Node.js 24.11.0 or newer is required.
npm install @nanoforge-dev/network-server
yarn add @nanoforge-dev/network-server
pnpm add @nanoforge-dev/network-server
bun add @nanoforge-dev/network-serverExample usage
Initilize the library in your main file.
import { type IRunServerOptions } from "@nanoforge-dev/common";
import { NanoforgeFactory } from "@nanoforge-dev/core";
import { NetworkServerLibrary } from "@nanoforge-dev/network-server";
export async function main(options: IRunServerOptions) {
const app = NanoforgeFactory.createServer();
const network = new NetworkServerLibrary();
app.useNetwork(network);
await app.init(options);
await app.run();
}Use to receive packets
export function myReceiveSystem(registry: Registry, ctx: Context) {
const network = ctx.libs.getNetwork<NetworkServerLibrary>();
network.tcp.getReceivedPackets().forEach((packets, client) => {
packets.forEach((packet) => {
const data = JSON.parse(new TextDecoder().decode(packet));
// Do something with the data
});
});
}Use to send packets to everyones
export function mySendSystem(registry: Registry, ctx: Context) {
const network = ctx.libs.getNetwork<NetworkServerLibrary>();
// Do something to get a data
const packet = new TextEncoder().encode(JSON.stringify(data));
network.tcp.sendToEverybody(packet);
}Links
Contributing
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
documentation.
See the contribution guide if you'd like to submit a PR.
Help
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to ask questions in discussions.
