### Start ScriptBridge Server (JavaScript) Source: https://github.com/tutinoko2048/scriptbridge/blob/main/server/README.md A basic example demonstrating how to start a ScriptBridge server in JavaScript. It requires importing the `ScriptBridgeServer` class and initializing it with a port. ```javascript const { ScriptBridgeServer } = require('@script-bridge/server'); const server = new ScriptBridgeServer({ port: 8000 }); server.start(); ``` -------------------------------- ### Start ScriptBridge Server (TypeScript) Source: https://github.com/tutinoko2048/scriptbridge/blob/main/server/README.md A basic example demonstrating how to start a ScriptBridge server in TypeScript. It requires importing the `ScriptBridgeServer` class and initializing it with a port. ```typescript import { ScriptBridgeServer } from '@script-bridge/server'; const server = new ScriptBridgeServer({ port: 8000 }); server.start(); ``` -------------------------------- ### Install @script-bridge/server Source: https://github.com/tutinoko2048/scriptbridge/blob/main/server/README.md Instructions for installing the ScriptBridge server package using npm, yarn, pnpm, and bun. ```bash npm install @script-bridge/server ``` ```bash yarn add @script-bridge/server ``` ```bash pnpm add @script-bridge/server ``` ```bash bun install @script-bridge/server ``` -------------------------------- ### Example ScriptBridge Server Configurations Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Provides example configurations for ScriptBridgeServer, illustrating high-performance, conservative, and no-auto-disconnect setups. Each example demonstrates setting the port, request interval, and timeout threshold. ```typescript // High performance setup (faster but more resource intensive) const fastServer = new ScriptBridgeServer({ port: 8000, requestIntervalTicks: 4, // 200ms interval timeoutThresholdMultiplier: 10 // 2 second timeout }); // Conservative setup (slower but more stable) const stableServer = new ScriptBridgeServer({ port: 8000, requestIntervalTicks: 20, // 1 second interval timeoutThresholdMultiplier: 30 // 30 second timeout }); // No auto-disconnect (manual session management) const persistentServer = new ScriptBridgeServer({ port: 8000, requestIntervalTicks: 8, timeoutThresholdMultiplier: Infinity }); ``` -------------------------------- ### Install @script-bridge/server with bun Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Installs the ScriptBridge server package using bun. This package provides the Node.js/Express-based HTTP server for communication. ```bash bun install @script-bridge/server ``` -------------------------------- ### Install @script-bridge/server with npm Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Installs the ScriptBridge server package using npm. This package provides the Node.js/Express-based HTTP server for communication. ```bash npm install @script-bridge/server ``` -------------------------------- ### Start ScriptBridge Server (JavaScript) Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Demonstrates basic usage of the ScriptBridge server in JavaScript. It initializes a server instance with specified port and configuration options, then starts the server. ```javascript const { ScriptBridgeServer } = require('@script-bridge/server'); const server = new ScriptBridgeServer({ port: 8000, requestIntervalTicks: 8, // Client query interval in ticks (default: 8) timeoutThresholdMultiplier: 20 // Timeout threshold multiplier (default: 20) }); server.start().then(() => { console.log('ScriptBridge server started on port 8000'); }); ``` -------------------------------- ### Start ScriptBridge Server (TypeScript) Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Demonstrates basic usage of the ScriptBridge server in TypeScript. It initializes a server instance with specified port and configuration options, then starts the server asynchronously. ```typescript import { ScriptBridgeServer } from '@script-bridge/server'; const server = new ScriptBridgeServer({ port: 8000, requestIntervalTicks: 8, timeoutThresholdMultiplier: 20 }); await server.start(); console.log('ScriptBridge server started on port 8000'); ``` -------------------------------- ### Install @script-bridge/server with yarn Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Installs the ScriptBridge server package using yarn. This package provides the Node.js/Express-based HTTP server for communication. ```bash yarn add @script-bridge/server ``` -------------------------------- ### Install @script-bridge/server with pnpm Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Installs the ScriptBridge server package using pnpm. This package provides the Node.js/Express-based HTTP server for communication. ```bash pnpm add @script-bridge/server ``` -------------------------------- ### Send Data to Server with ScriptBridge Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Demonstrates sending data to a server using the ScriptBridge client. Includes a simple request-response example and a periodic heartbeat mechanism to send player count and server load. ```typescript // Simple request-response const response = await client.send('myapp:get-config', { section: 'gameplay' }); if (!response.error) { console.log('Config received:', response.data); } else { console.error('Error:', response.message); } // Send player data periodically system.runInterval(async () => { if (!client.isConnected) return; const playerCount = world.getPlayers().length; try { await client.send('myapp:heartbeat', { playerCount, timestamp: Date.now(), serverLoad: system.currentTick }); } catch (error) { console.warn('Failed to send heartbeat:', error.message); } }, 200); // Every 10 seconds (200 ticks) ``` -------------------------------- ### TypeScript: ScriptBridge Client Connection Status and Ping Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Provides examples of how to check the connection status and average ping of a ScriptBridge client, useful for monitoring the connection health. ```typescript import { ScriptBridgeClient } from './path/to/script-bridge-client'; import { system } from '@minecraft/server'; const client = new ScriptBridgeClient({ url: 'http://localhost:8000' }); // Check connection status console.log('Connected:', client.isConnected); console.log('Average ping:', client.averagePing, 'ms'); ``` -------------------------------- ### TypeScript: Basic ScriptBridge Client Connection Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Shows how to initialize and connect a ScriptBridge client to a server. It includes setting up the client with a URL and an optional client ID. ```typescript import { ScriptBridgeClient } from './path/to/script-bridge-client'; const client = new ScriptBridgeClient({ url: 'http://localhost:8000', clientId: 'my-minecraft-addon', // Optional: Custom client identifier }); // Connect to server await client.connect(); console.log('Connected to ScriptBridge server!'); ``` -------------------------------- ### Configure ScriptBridge Server Options Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Defines the ServerOptions interface for configuring a ScriptBridgeServer. Explains each option: port, requestIntervalTicks, and timeoutThresholdMultiplier, along with their types, default values, and descriptions. ```typescript interface ServerOptions { port: number; requestIntervalTicks?: number; timeoutThresholdMultiplier?: number; } ``` -------------------------------- ### TypeScript: Handle Server Events and Custom Actions Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Demonstrates how to use ScriptBridgeServer to listen for server events like client connections and disconnections, handle errors, and register custom action handlers for communication between server and clients. ```typescript import { ScriptBridgeServer } from '@script-bridge/server'; const server = new ScriptBridgeServer({ port: 8000 }); // Listen to server events server.on('serverOpen', () => { console.log('Server is now listening'); }); server.on('clientConnect', (session) => { console.log(`Client connected: ${session.id}`); }); server.on('clientDisconnect', (session, reason) => { console.log(`Client disconnected: ${session.id}, reason: ${reason}`); }); server.on('error', (error) => { console.error('Server error:', error.message); }); // Register custom action handlers server.registerHandler('myapp:chat', (action) => { const { message, playerName } = action.data; console.log(`${playerName}: ${message}`); // Respond to the client action.respond({ success: true, timestamp: Date.now() }); }); // Broadcast messages to all connected clients server.registerHandler('myapp:broadcast', async (action) => { const { message } = action.data; try { const responses = await server.broadcast('myapp:receive-message', { message, timestamp: Date.now() }); action.respond({ success: true, clientCount: responses.length }); } catch (error) { action.respond({ success: false, error: error.message }); } }); await server.start(); ``` -------------------------------- ### TypeScript: ScriptBridge Client Event Handling and Actions Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Demonstrates advanced client-side usage of ScriptBridge, including listening for connection/disconnection events, registering handlers for server-sent actions, and sending messages to the server via chat events. ```typescript import { ScriptBridgeClient, DisconnectReason } from './path/to/script-bridge-client'; import { world, system } from '@minecraft/server'; const client = new ScriptBridgeClient({ url: 'http://localhost:8000', clientId: 'chat-bridge-addon' }); // Listen to connection events client.on('connect', ({ sessionId }) => { console.log(`Connected with session: ${sessionId}`); world.sendMessage('§aConnected to server!'); }); client.on('disconnect', ({ reason }) => { console.log(`Disconnected: ${DisconnectReason[reason]}`); world.sendMessage('§cDisconnected from server'); }); // Register action handlers client.registerHandler('myapp:receive-message', (action) => { const { message, timestamp } = action.data; world.sendMessage(`§7[Server] §f${message}`); // Respond to acknowledge receipt action.respond({ received: true }); }); client.registerHandler('myapp:player-request', (action) => { const players = world.getPlayers().map(player => ({ name: player.name, id: player.id, location: player.location })); action.respond({ players }); }); // Send messages to server world.afterEvents.chatSend.subscribe(async (event) => { try { const response = await client.send('myapp:chat', { message: event.message, playerName: event.sender.name, timestamp: Date.now() }); if (response.error) { console.error('Failed to send chat message:', response.message); } } catch (error) { console.error('Error sending message:', error.message); } }); // Connect with retry logic try { await client.connect(); } catch (error) { console.error('Failed to connect:', error.message); } ``` -------------------------------- ### TypeScript: Graceful Server Shutdown Source: https://github.com/tutinoko2048/scriptbridge/blob/main/README.md Implements a SIGINT handler to gracefully shut down the ScriptBridge server when the process receives an interrupt signal, ensuring a clean exit. ```typescript process.on('SIGINT', async () => { console.log('Shutting down server...'); await server.stop(); process.exit(0); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.