-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-server.ts
More file actions
307 lines (260 loc) · 8.39 KB
/
example-server.ts
File metadata and controls
307 lines (260 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* Example WebSocket Server for OBS Bridge
*
* This is a sample Node.js/TypeScript server that demonstrates how to:
* - Accept connections from OBS bridge clients
* - Send commands to OBS
* - Receive events and status updates from OBS
*/
import WebSocket, { WebSocketServer } from 'ws';
import express from 'express';
import { createServer } from 'http';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
interface OBSClient {
ws: WebSocket;
clientId: string;
connected: Date;
}
interface OBSMessage {
type: 'register' | 'obs_event' | 'command_response' | 'pong';
clientId?: string;
event?: string;
command?: string;
success?: boolean;
data?: Record<string, unknown>;
error?: string;
}
interface OBSCommand {
type: 'command' | 'ping';
command?: string;
params?: Record<string, unknown>;
}
class OBSControlServer {
private clients: Map<string, OBSClient> = new Map();
private wss: WebSocketServer;
private app: express.Application;
private server: ReturnType<typeof createServer>;
constructor(port: number = 8000) {
this.app = express();
this.server = createServer(this.app);
// WebSocket server for OBS clients
this.wss = new WebSocketServer({
server: this.server,
path: '/obs'
});
this.setupWebSocket();
this.setupHTTPRoutes();
this.server.listen(port, () => {
console.log(`OBS Control Server running on port ${port}`);
console.log(`WebSocket endpoint: ws://localhost:${port}/obs`);
console.log(`Demo control panel: http://localhost:${port}`);
console.log(`HTTP API: http://localhost:${port}/api`);
});
}
private setupWebSocket(): void {
this.wss.on('connection', (ws: WebSocket) => {
console.log('New OBS client attempting to connect...');
ws.on('message', (message: Buffer) => {
try {
const data: OBSMessage = JSON.parse(message.toString());
this.handleMessage(ws, data);
} catch (error) {
console.error('Failed to parse message:', error);
}
});
ws.on('close', () => {
// Remove client from registry
for (const [clientId, client] of this.clients.entries()) {
if (client.ws === ws) {
console.log(`Client disconnected: ${clientId}`);
this.clients.delete(clientId);
break;
}
}
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
});
}
private handleMessage(ws: WebSocket, message: OBSMessage): void {
const { type, clientId } = message;
switch (type) {
case 'register':
if (clientId) {
this.clients.set(clientId, {
ws,
clientId,
connected: new Date()
});
console.log(`✓ Client registered: ${clientId}`);
// Send initial status request
this.sendCommand(clientId, 'GetStreamingStatus');
this.sendCommand(clientId, 'GetSceneList');
}
break;
case 'obs_event':
console.log(`Event from ${clientId}: ${message.event}`, message.data);
// Handle OBS events (stream started, scene changed, etc.)
this.handleOBSEvent(clientId!, message);
break;
case 'command_response':
console.log(`Response from ${clientId}:`, {
command: message.command,
success: message.success,
data: message.data,
error: message.error
});
break;
case 'pong':
console.log(`Pong from ${clientId}`);
break;
default:
console.log('Unknown message type:', type);
}
}
private handleOBSEvent(clientId: string, message: OBSMessage): void {
// You can implement custom logic here based on events
// For example, update a database, trigger webhooks, notify users, etc.
switch (message.event) {
case 'StreamStarted':
console.log(`🔴 Stream started on ${clientId}`);
break;
case 'StreamStopped':
console.log(`⏹️ Stream stopped on ${clientId}`);
break;
case 'RecordingStarted':
console.log(`⏺️ Recording started on ${clientId}`);
break;
case 'RecordingStopped':
console.log(`⏹️ Recording stopped on ${clientId}`);
break;
case 'SwitchScenes':
console.log(`🎬 Scene changed on ${clientId}:`, message.data);
break;
}
}
private setupHTTPRoutes(): void {
this.app.use(express.json());
// Serve demo control panel
this.app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'demo.html'));
});
// Get list of connected clients
this.app.get('/api/clients', (req, res) => {
const clients = Array.from(this.clients.values()).map(client => ({
clientId: client.clientId,
connected: client.connected
}));
res.json({ clients });
});
// Send a command to a specific OBS client
this.app.post('/api/command/:clientId', (req, res) => {
const { clientId } = req.params;
const { command, params } = req.body;
if (!this.clients.has(clientId)) {
return res.status(404).json({ error: 'Client not found' });
}
try {
this.sendCommand(clientId, command, params);
res.json({ success: true, message: 'Command sent' });
} catch (error) {
res.status(500).json({ error: 'Failed to send command' });
}
});
// Broadcast command to all clients
this.app.post('/api/broadcast', (req, res) => {
const { command, params } = req.body;
let sent = 0;
for (const clientId of this.clients.keys()) {
try {
this.sendCommand(clientId, command, params);
sent++;
} catch (error) {
console.error(`Failed to send to ${clientId}:`, error);
}
}
res.json({ success: true, clientsSent: sent });
});
// Quick actions
this.app.post('/api/action/:clientId/:action', (req, res) => {
const { clientId, action } = req.params;
if (!this.clients.has(clientId)) {
return res.status(404).json({ error: 'Client not found' });
}
const commandMap: Record<string, string> = {
'start-stream': 'StartStreaming',
'stop-stream': 'StopStreaming',
'start-recording': 'StartRecording',
'stop-recording': 'StopRecording',
'status': 'GetStreamingStatus',
'scenes': 'GetSceneList'
};
const command = commandMap[action];
if (!command) {
return res.status(400).json({ error: 'Invalid action' });
}
try {
this.sendCommand(clientId, command);
res.json({ success: true, message: `Action '${action}' sent` });
} catch (error) {
res.status(500).json({ error: 'Failed to send action' });
}
});
// Health check
this.app.get('/health', (req, res) => {
res.json({
status: 'ok',
clients: this.clients.size,
uptime: process.uptime()
});
});
}
public sendCommand(
clientId: string,
command: string,
params: Record<string, unknown> = {}
): void {
const client = this.clients.get(clientId);
if (!client) {
throw new Error(`Client ${clientId} not found`);
}
const message: OBSCommand = {
type: 'command',
command,
params
};
console.log(`→ Sending command to ${clientId}: ${command}`, params);
client.ws.send(JSON.stringify(message));
console.log(`✓ Command sent via WebSocket to ${clientId}`);
}
public ping(clientId: string): void {
const client = this.clients.get(clientId);
if (!client) {
throw new Error(`Client ${clientId} not found`);
}
client.ws.send(JSON.stringify({ type: 'ping' }));
}
public getConnectedClients(): string[] {
return Array.from(this.clients.keys());
}
}
// Start the server
const server = new OBSControlServer(8000);
// Example: Send periodic pings to all clients
setInterval(() => {
for (const clientId of server.getConnectedClients()) {
server.ping(clientId);
}
}, 30000); // Every 30 seconds
// Example: Schedule a stream start (demonstration)
// setTimeout(() => {
// const clients = server.getConnectedClients();
// if (clients.length > 0) {
// console.log('Starting stream on first client...');
// server.sendCommand(clients[0], 'StartStreaming');
// }
// }, 10000);