node-srt/examples/epoll.js
Stephan Hesse 7973c63f2a
TypeScript support (definition files) + a few small enhancement proposals (#5)
* add typescript definition files for this JS API

* server.js: replace console.log by debug() call

* server.js: set "iface" and "port" properties on SRTServer listen call
+ and init them with null in constructor

* stream.js: SRTReadStream: init fd property with null in constructor

* stream.js: SRTReadStream: add close method (to free the fd aquired with
connect()), likewise the writable stream methods

* example files: add "use strict"; directive on all

* examples/srt.js: correctly state variable fhandle (this would crash in strict mode
otherwise)

* NAPI: NodeSRT::GetSockOpt: fix error handling to really reach ThrowAsJavaScriptException

* add example: srt.ts (TypeScript usage)

* add .editoconfig to project root (helps keeping files free of whitespaces
and maintain indentation etc)

* package.json: improve metadata quality (via npm init call)

* package.json: add dev-deps for tsc/ts-node and node type-defs

* package.json: add check-tsc script to compile typescript example

* gitignore: add tsc-lib (typescript compiler test output)

* add tsconfig.json (typescript compiler config)

* some end-of-file newline and whitespace fixes

* stream.js: add const decls for all the magic numbers in SRTReadStream

* enable eslint static analysis to find possible issues upfront
+ and maintain codestyle

* update package-lock

* stream.js: fix whitespace and eof

* add lint script for src, examples and types modules

* types: lint error fix
2020-07-20 11:41:15 +02:00

50 lines
1.2 KiB
JavaScript

"use strict";
const { SRT } = require('../index.js');
const srt = new SRT();
const socket = srt.createSocket();
if (socket !== -1) {
console.log("Created socket: " + socket);
}
let result;
result = srt.bind(socket, "0.0.0.0", 1234);
if (!result) {
console.log("Bind success");
} else {
console.log(result);
}
result = srt.listen(socket, 10);
if (!result) {
console.log("Listen success");
} else {
console.log(result);
}
const epid = srt.epollCreate();
srt.epollAddUsock(epid, socket, SRT.EPOLL_IN | SRT.EPOLL_ERR);
while (true) {
console.log("Waiting for action");
const events = srt.epollUWait(epid, 1000);
events.forEach(event => {
const status = srt.getSockState(event.socket);
if (status === SRT.SRTS_BROKEN || status === SRT.SRTS_NONEXIST || status === SRT.SRTS_CLOSED) {
console.log("Client disconnected");
srt.close(event.socket);
} else if (event.socket === socket) {
const fhandle = srt.accept(socket);
console.log("New connection");
srt.epollAddUsock(epid, fhandle, SRT.EPOLL_IN | SRT.EPOLL_ERR);
} else {
while (true) {
const chunk = srt.read(event.socket, 1316);
console.log("Read chunk: " + chunk.length);
}
}
});
}