add events to server, add ability to close client and server

This commit is contained in:
Rik Berkelder 2021-01-30 03:28:46 +01:00
parent 2261bf8b3e
commit 56338f2262
3 changed files with 39 additions and 3 deletions

View file

@ -1,6 +1,6 @@
{
"name": "ts-osc",
"version": "0.2.6",
"version": "0.3.0",
"description": "Fully TypeScript-native OSC Client based on osc-min",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",

View file

@ -39,4 +39,12 @@ export class OSCClient {
const encoded = toBuffer(address, [arg]);
this.sendPacket(encoded);
}
/**
* Close the connection
*/
public close(): void {
this.socket.close();
}
}

View file

@ -22,17 +22,31 @@ export declare interface OSCServer {
@event
*/
on(event: 'listening', listener: () => void): this;
/**
* Emitted when OSC socket is closed
@event
*/
on(event: 'close', listener: () => void): this;
/**
* Emitted whenever an error occurs
@event
*/
on(event: 'error', listener: (exception: Error) => void): this;
}
/**
* OSC Client, handles sending OSC messages
* OSC Server, handles receiving OSC messages
```
const osc = new OSCServer("192.168.0.44", 8000);
```
*/
export class OSCServer extends EventEmitter {
private socket: dgram.Socket;
public socket: dgram.Socket;
constructor(bindAddress: string, port: number) {
super();
@ -43,6 +57,12 @@ export class OSCServer extends EventEmitter {
this.socket.on('listening', () => {
this.emit('listening');
});
this.socket.on('close', () => {
this.emit('close');
});
this.socket.on('error', (e) => {
this.emit('error', e);
});
this.socket.on('message', (msg) => {
const decoded = fromBuffer(msg);
@ -57,4 +77,12 @@ export class OSCServer extends EventEmitter {
}
});
}
/**
* close the server
*/
public close(): void {
this.socket.close();
}
}