nodejs-slikenet
v2.0.0
Published
Node.js native addon for SLikeNet networking library with improved thread management architecture
Maintainers
Readme
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
- connect() - Synchronous call in main Node.js thread
- send() - Synchronous call in main Node.js thread
- listen(packetID) - Creates background thread on first call (if none exists)
- disconnect() - Stops thread and cleans up all subscriptions
📦 Installation
npm install nodejs-slikenet🔧 Usage
Basic Setup
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
// 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
// Send data
peer.send('Hello from client!');Cleanup
// 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
npm install
npm run build📚 Examples
example.js- Basic client/server exampleexample-new-architecture.js- New architecture demonstration
🆕 Migration from v1.x
// 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.
