linting, editorconfig
This commit is contained in:
parent
ee66b16be2
commit
2f4ab521b5
9 changed files with 2267 additions and 146 deletions
114
src/oscClient.ts
114
src/oscClient.ts
|
|
@ -1,44 +1,40 @@
|
|||
import * as dgram from 'dgram';
|
||||
import {OSCArgument, OSCType, toBuffer, fromBuffer, OSCArgumentType} from 'osc-min';
|
||||
import { OSCArgument, OSCType, toBuffer, fromBuffer, OSCArgumentType } from 'osc-min';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
|
||||
export interface OSCClientOptions {
|
||||
/** host to output OSC data to */
|
||||
outHost: string;
|
||||
/** port to output OSC data to */
|
||||
outPort: number;
|
||||
/** Local port for incoming OSC messages */
|
||||
inPort?: number;
|
||||
/** host to output OSC data to */
|
||||
outHost: string;
|
||||
/** port to output OSC data to */
|
||||
outPort: number;
|
||||
/** Local port for incoming OSC messages */
|
||||
inPort?: number;
|
||||
}
|
||||
|
||||
export interface ReceivedOSCMessage<T extends OSCType> {
|
||||
address: string;
|
||||
type: T;
|
||||
value: OSCArgumentType<T>
|
||||
address: string;
|
||||
type: T;
|
||||
value: OSCArgumentType<T>;
|
||||
}
|
||||
|
||||
// declare types for events
|
||||
export declare interface OSCClient {
|
||||
/**
|
||||
/**
|
||||
Emitted whenever an OSC message is received on inPort
|
||||
@event
|
||||
*/
|
||||
on(event: 'message', listener: (message: ReceivedOSCMessage<OSCType>) => void): this
|
||||
on(event: 'message', listener: (message: ReceivedOSCMessage<OSCType>) => void): this;
|
||||
|
||||
|
||||
/**
|
||||
Emitted when OSC client is listening on inPort
|
||||
/**
|
||||
Emitted when OSC client is listening on inPort
|
||||
@event
|
||||
*/
|
||||
on(event: 'listening', listener: ()=>void): this
|
||||
on(event: 'listening', listener: () => void): this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* OSC Client base class, handles sending and receiving OSC messages
|
||||
|
||||
|
||||
```
|
||||
const osc = new OSCClient({
|
||||
outHost: "192.168.0.68",
|
||||
|
|
@ -47,54 +43,54 @@ export declare interface OSCClient {
|
|||
})
|
||||
```
|
||||
*/
|
||||
export class OSCClient extends EventEmitter{
|
||||
private socket: dgram.Socket;
|
||||
private options: OSCClientOptions;
|
||||
constructor(options: OSCClientOptions){
|
||||
super();
|
||||
this.socket = dgram.createSocket({type: "udp4", reuseAddr: true});
|
||||
this.options = options;
|
||||
export class OSCClient extends EventEmitter {
|
||||
private socket: dgram.Socket;
|
||||
private options: OSCClientOptions;
|
||||
constructor(options: OSCClientOptions) {
|
||||
super();
|
||||
this.socket = dgram.createSocket({ type: 'udp4', reuseAddr: true });
|
||||
this.options = options;
|
||||
|
||||
if(options.inPort){
|
||||
this.socket.bind(options.inPort);
|
||||
}
|
||||
|
||||
this.socket.on('listening', ()=>{
|
||||
this.emit('listening');
|
||||
})
|
||||
|
||||
this.socket.on('message', (msg)=>{
|
||||
const decoded = fromBuffer(msg)
|
||||
if(decoded.oscType === "message"){
|
||||
decoded.args.forEach((arg)=>{
|
||||
this.emit('message', {
|
||||
address: decoded.address,
|
||||
type: arg.type,
|
||||
value: arg.value
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
if (options.inPort) {
|
||||
this.socket.bind(options.inPort);
|
||||
}
|
||||
|
||||
private sendPacket(packet: Buffer): void{
|
||||
this.socket.send(packet, 0, packet.length, this.options.outPort, this.options.outHost)
|
||||
}
|
||||
this.socket.on('listening', () => {
|
||||
this.emit('listening');
|
||||
});
|
||||
|
||||
/**
|
||||
this.socket.on('message', (msg) => {
|
||||
const decoded = fromBuffer(msg);
|
||||
if (decoded.oscType === 'message') {
|
||||
decoded.args.forEach((arg) => {
|
||||
this.emit('message', {
|
||||
address: decoded.address,
|
||||
type: arg.type,
|
||||
value: arg.value,
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private sendPacket(packet: Buffer): void {
|
||||
this.socket.send(packet, 0, packet.length, this.options.outPort, this.options.outHost);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an OSC message
|
||||
```
|
||||
osc.send("/osc/url", OSCType.String, "hello")
|
||||
```
|
||||
*/
|
||||
|
||||
public send<T extends OSCType>(address: string, type: T, value: OSCArgumentType<T>){
|
||||
const arg: OSCArgument<T> = {
|
||||
type,
|
||||
value
|
||||
}
|
||||
public send<T extends OSCType>(address: string, type: T, value: OSCArgumentType<T>): void {
|
||||
const arg: OSCArgument<T> = {
|
||||
type,
|
||||
value,
|
||||
};
|
||||
|
||||
const encoded = toBuffer(address, [arg]);
|
||||
this.sendPacket(encoded);
|
||||
}
|
||||
const encoded = toBuffer(address, [arg]);
|
||||
this.sendPacket(encoded);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue