implement connection between servers
This commit is contained in:
@@ -2,73 +2,91 @@ import { ServerWebSocket, write } from "bun";
|
||||
import {
|
||||
addConnection,
|
||||
audit,
|
||||
connectionCount,
|
||||
connectServer,
|
||||
connectServers,
|
||||
connectedClientCount,
|
||||
forgetConnection,
|
||||
processMessage,
|
||||
subscribeChannel,
|
||||
subscriptions,
|
||||
} from "./websocket-handler";
|
||||
import { writeInfo, writeStat } from "./utility";
|
||||
import { writeInfo, writeStat, writeSuccess } from "./utility";
|
||||
|
||||
import Colors from "colors";
|
||||
import IP from "ip";
|
||||
import Path from "path";
|
||||
import { getConfig } from "./config-handler";
|
||||
|
||||
// CONFIG
|
||||
const staticDir = "static/dist";
|
||||
const defaultPage = "index.html";
|
||||
|
||||
// TYPES
|
||||
export type WS = ServerWebSocket<unknown>;
|
||||
export type WS = ServerWebSocket<unknown> | WebSocket;
|
||||
|
||||
// SERVER
|
||||
const server = Bun.serve({
|
||||
fetch(request, server) {
|
||||
// allow websockets
|
||||
server.upgrade(request);
|
||||
// MAIN
|
||||
async function main() {
|
||||
// CONFIG
|
||||
const config = await getConfig();
|
||||
|
||||
// respond
|
||||
const requestPath = new URL(request.url).pathname;
|
||||
switch (requestPath) {
|
||||
case "/":
|
||||
return createFileResponse(defaultPage);
|
||||
default:
|
||||
return createFileResponse(requestPath);
|
||||
}
|
||||
},
|
||||
// SERVER
|
||||
const server = Bun.serve({
|
||||
port: config.port,
|
||||
|
||||
websocket: {
|
||||
open() {
|
||||
addConnection();
|
||||
fetch(request, server) {
|
||||
// allow websockets
|
||||
server.upgrade(request);
|
||||
|
||||
// respond
|
||||
const requestPath = new URL(request.url).pathname;
|
||||
switch (requestPath) {
|
||||
case "/":
|
||||
return createFileResponse(defaultPage);
|
||||
default:
|
||||
return createFileResponse(requestPath);
|
||||
}
|
||||
},
|
||||
message(ws, message) {
|
||||
processMessage(ws, message.toString());
|
||||
},
|
||||
close(ws) {
|
||||
forgetConnection(ws);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
function createFileResponse(requestPath: string): Response {
|
||||
const fullPath = Path.join(staticDir, requestPath);
|
||||
return new Response(Bun.file(fullPath));
|
||||
websocket: {
|
||||
open() {
|
||||
addConnection();
|
||||
},
|
||||
message(ws, message) {
|
||||
processMessage(ws, message.toString(), config);
|
||||
},
|
||||
close(ws) {
|
||||
forgetConnection(ws);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
function createFileResponse(requestPath: string): Response {
|
||||
const fullPath = Path.join(staticDir, requestPath);
|
||||
return new Response(Bun.file(fullPath));
|
||||
}
|
||||
|
||||
// OTHER SERVERS
|
||||
connectServers(config);
|
||||
|
||||
// CLI
|
||||
writeSuccess(`###\nstarted up ${new Date().toLocaleString()}`);
|
||||
|
||||
function updateCLI() {
|
||||
console.clear();
|
||||
console.log(Colors.bold.bgWhite("UNIVERSAL DECENTRALIZED NETWORK"));
|
||||
console.log();
|
||||
|
||||
console.log(new Date().toLocaleString());
|
||||
console.log();
|
||||
|
||||
writeInfo("server url", Colors.bold.green(server.url.toString()));
|
||||
writeInfo("server ip address", Colors.bold.green(IP.address()));
|
||||
console.log();
|
||||
|
||||
audit().forEach((entry) => writeStat(...entry));
|
||||
}
|
||||
|
||||
setInterval(updateCLI, 5000);
|
||||
}
|
||||
|
||||
// CLI
|
||||
function updateCLI() {
|
||||
console.clear();
|
||||
console.log(Colors.bold.bgWhite("UNIVERSAL DECENTRALIZED NETWORK"));
|
||||
console.log();
|
||||
|
||||
console.log(new Date().toLocaleString());
|
||||
console.log();
|
||||
|
||||
writeInfo("server url", Colors.bold.green(server.url.toString()));
|
||||
writeInfo("server ip address", Colors.bold.green(IP.address()));
|
||||
console.log();
|
||||
|
||||
audit().forEach((entry) => writeStat(...entry));
|
||||
}
|
||||
|
||||
updateCLI();
|
||||
setInterval(updateCLI, 5000);
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user