D-Back WebSocket Server Loading...
Status: Server is running and serving static files!
WebSocket URL: ws://localhost:3000
🚀 How to Run This Module
Method 1: Command Line
pip install d_back
d_back
Method 2: Python Script
from d_back.server import WebSocketServer
import asyncio
# Basic usage
server = WebSocketServer(port=3000)
server.run_sync()
# Or with custom callbacks
async def main():
server = WebSocketServer(port=3000)
# Custom server data callback
def get_servers():
return {"your_server_id": {"id": "custom", "name": "My Server"}}
# Custom user data callback
def get_users(server_id):
return {"user123": {"uid": "user123", "username": "TestUser"}}
server.on_get_server_data(get_servers)
server.on_get_user_data(get_users)
await server.run_forever()
asyncio.run(main())
Method 3: With Custom Static Files
from d_back.server import WebSocketServer
def custom_static_handler(path):
if path == "/custom":
return "text/html", "<h1>Custom Page</h1>"
return None # Let default handler take over
server = WebSocketServer(port=3000)
server.on_static_request(custom_static_handler)
server.run_sync()
📡 WebSocket API
Connect to Server
// JavaScript WebSocket client example
const ws = new WebSocket('ws://localhost:3000');
ws.onopen = function() {
// Connect to a server
ws.send(JSON.stringify({
type: 'connect',
data: {
server: 'default', // or specific server ID
password: null // if server requires password
}
}));
};
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log('Received:', data);
// Handle different message types:
// - server-list: List of available servers
// - server-join: Successfully joined server + user list
// - message: Chat message from another user
// - presence: User status update
// - error: Error message
};
🔧 Features
- WebSocket Server: Real-time bidirectional communication
- Static File Serving: Serve web pages on the same port
- Multi-Server Support: Connect to different Discord-like servers
- User Management: Track online users and their status
- Message Broadcasting: Send messages to all connected clients
- Presence Updates: Real-time user status changes
- Custom Callbacks: Override default behavior with your own logic
- Mock Data: Built-in test data for development
📦 Package Information
- Package: d_back
- Version: Loading...
- Author: Tin Nguyen
- Python: >=3.8
- Dependencies: websockets
🔗 Related Projects
Note: This server combines WebSocket and HTTP functionality on the same port.
WebSocket connections are handled at the root path, while static files are served for regular HTTP requests.