UNPKG

nodejs-slikenet

Version:

Node.js native addon for SLikeNet networking library with improved thread management architecture

137 lines (94 loc) 3.03 kB
# NodeJS SLikeNet v2.0.0 Node.js native addon for SLikeNet networking library with **improved thread management architecture**. ## 🚀 New Architecture (v2.0.0) ### Key Improvements - **Smart Thread Management**: Single background thread for all listeners - **Event-Driven API**: `listen(packetID, handler)` for specific packet types - **Automatic Resource Management**: Threads start/stop automatically - **Better Performance**: No unnecessary thread creation/destruction ### How It Works 1. **connect()** - Synchronous call in main Node.js thread 2. **send()** - Synchronous call in main Node.js thread 3. **listen(packetID)** - Creates background thread on first call (if none exists) 4. **disconnect()** - Stops thread and cleans up all subscriptions ## 📦 Installation ```bash npm install nodejs-slikenet ``` ## 🔧 Usage ### Basic Setup ```javascript const { SlikeNetPeer } = require('nodejs-slikenet'); const peer = new SlikeNetPeer(); // Initialize and startup peer.initialize(32); peer.startup(32, 0); // Connect to server peer.connect('127.0.0.1', 61111); ``` ### Event Handling ```javascript // System events peer.on('connect', () => { console.log('Connected to server!'); }); peer.on('disconnect', () => { console.log('Disconnected from server'); }); // Packet-specific listeners peer.listen(100, (data) => { console.log('Received packet 100:', data); }); peer.listen(101, (data) => { console.log('Received packet 101:', data); }); ``` ### Sending Data ```javascript // Send data peer.send('Hello from client!'); ``` ### Cleanup ```javascript // Remove all listeners (stops background thread) peer.removeAllListeners(); // Shutdown peer peer.shutdown(); ``` ## 🏗️ Architecture Details ### Thread Management - **Before v2.0.0**: Multiple threads, manual management - **v2.0.0+**: Single background thread, automatic management ### API Changes | v1.x | v2.0.0 | Description | |------|---------|-------------| | `setMessageCallback()` | `listen(packetID, handler)` | Subscribe to specific packet types | | `processEvents()` | *Automatic* | Background processing handled automatically | | Manual thread control | *Automatic* | Threads start/stop based on listener count | ## 📋 Requirements - Node.js 16+ - Windows (Visual Studio 2019+) - SLikeNet library ## 🔨 Building ```bash npm install npm run build ``` ## 📚 Examples - `example.js` - Basic client/server example - `example-new-architecture.js` - New architecture demonstration ## 🆕 Migration from v1.x ```javascript // Old way (v1.x) peer.setMessageCallback((data) => { console.log('Message received:', data); }); // New way (v2.0.0) peer.listen(100, (data) => { console.log('Packet 100 received:', data); }); ``` ## 📄 License MIT ## 🤝 Contributing Contributions welcome! Please check the build instructions in `BUILD_INSTRUCTIONS.md`.