Add launchable
This commit is contained in:
@@ -9,6 +9,7 @@ if (typeof window !== "undefined") {
|
||||
WebSocketServer = wsModule.WebSocketServer;
|
||||
}
|
||||
import { Instance } from "./Instance.js";
|
||||
import { BaseService } from "./BaseService.js";
|
||||
import { BO3ScriptSignal } from "../core/BO3ScriptSignal.js";
|
||||
|
||||
/**
|
||||
@@ -17,7 +18,7 @@ import { BO3ScriptSignal } from "../core/BO3ScriptSignal.js";
|
||||
* - Binary framed packets: [uint32 length][ptype][psub][payload]
|
||||
* - Fires PacketReceived signal for incoming packets
|
||||
*/
|
||||
export class NetworkService extends Instance {
|
||||
export class NetworkService extends BaseService {
|
||||
constructor() {
|
||||
super("NetworkService");
|
||||
|
||||
@@ -132,6 +133,17 @@ export class NetworkService extends Instance {
|
||||
console.warn("[NetworkService] Disconnected from server.")
|
||||
alert("Disconnected from server! If this was unintentional, please refresh the page. You might have been kicked by the server.\n\n(If you are the server host, check if the servers running.)");
|
||||
};
|
||||
// Register 0xFF kick handler
|
||||
this.registerHandler(0x00, 0xFF, (payload) => {
|
||||
let reason = "Kicked by server.";
|
||||
try {
|
||||
reason = new TextDecoder().decode(payload);
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
console.warn("[NetworkService] Kicked by server:", reason);
|
||||
alert("You have been kicked by the server:\n\n" + reason);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -151,6 +163,7 @@ export class NetworkService extends Instance {
|
||||
|
||||
this.wss.on("connection", (ws) => {
|
||||
this.clients.set(ws, {});
|
||||
ws.isAuthed = false; // Determine whether to send any data except auth packets
|
||||
console.log("[NetworkService] New client connected. Total clients:", this.clients.size);
|
||||
ws.binaryType = "arraybuffer";
|
||||
|
||||
@@ -167,10 +180,12 @@ export class NetworkService extends Instance {
|
||||
});
|
||||
}
|
||||
|
||||
broadcast(ptype, psub, payload) {
|
||||
broadcast(ptype, psub, payload,ignoreUnauthed,ignoreCheck) {
|
||||
if (!this.isServer || !this.wss) return;
|
||||
const packet = this.encodePacket(ptype, psub, payload);
|
||||
for (const ws of this.clients.keys()) {
|
||||
if(ignoreUnauthed && !ws.isAuthed) continue;
|
||||
if (ignoreCheck && ignoreCheck(ws)) continue;
|
||||
if (ws.readyState === WebSocket.OPEN) ws.send(packet);
|
||||
}
|
||||
}
|
||||
@@ -181,6 +196,17 @@ export class NetworkService extends Instance {
|
||||
if (ws.readyState === WebSocket.OPEN) ws.send(packet);
|
||||
}
|
||||
|
||||
KickClientLater(ws,code=4000,reason="") {
|
||||
// Send a kick packet
|
||||
this.sendToClient(ws, 0x00, 0xFF, new TextEncoder().encode(reason));
|
||||
setTimeout(() => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.close(code,reason);
|
||||
}
|
||||
}, 500); // Slight delay to ensure any pending packets are sent
|
||||
}
|
||||
|
||||
|
||||
Broadcast(ptype, psub, payload) {
|
||||
console.warn("[NetworkService] Broadcast is deprecated, use broadcast() instead.");
|
||||
this.broadcast(ptype, psub, payload);
|
||||
|
||||
Reference in New Issue
Block a user