-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathesm.mjs
More file actions
39 lines (31 loc) · 1.08 KB
/
esm.mjs
File metadata and controls
39 lines (31 loc) · 1.08 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
/**
* OSC Client and Server Example (ESM with Callbacks)
*
* This example demonstrates using node-osc with ES modules and callback-based API.
* It shows how to create both a client and server, send messages, and handle events.
*
* To run this example:
* node examples/esm.mjs
*/
import { Client, Server } from 'node-osc';
// Create a client connected to localhost on port 3333
const client = new Client('127.0.0.1', 3333);
// Create a server listening on port 3333, bound to all interfaces
var server = new Server(3333, '0.0.0.0');
// Listen for when the server is ready
server.on('listening', () => {
console.log('OSC Server is Listening');
});
// Listen for incoming messages
server.on('message', (msg, rinfo) => {
// msg is an array: [address, ...arguments]
console.log(`Message: ${msg}\nReceived from: ${rinfo.address}:${rinfo.port}`);
// Close the server after receiving a message
server.close();
});
// Send a message with callback-based API
client.send('/hello', 'world', (err) => {
if (err) console.error(err);
// Close the client after sending
client.close();
});