Create socket binding

This commit is contained in:
birme 2020-06-13 09:43:20 +02:00
parent 722db25e6c
commit 91d986127d
5 changed files with 26 additions and 17 deletions

View file

@ -7,13 +7,13 @@
"src/binding.cc",
"src/node-srt.cc"
],
'include_dirs': [
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
'libraries': [ ],
'dependencies': [
"libraries": [ "/usr/local/lib/libsrt.a" ],
"dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ]
}]
}

6
example.js Normal file
View file

@ -0,0 +1,6 @@
const SRT = require('./index.js').SRT;
const srt = new SRT();
const socket = srt.createSocket();
console.log(socket);

View file

@ -1,6 +1 @@
const LIB = require('./build/Release/node_srt.node');
const nsrt = new LIB.NodeSRT();
console.log(nsrt.hello());
module.exports = LIB;
module.exports = require('./build/Release/node_srt.node');

View file

@ -1,3 +1,4 @@
#include <srt/srt.h>
#include "node-srt.h"
Napi::FunctionReference NodeSRT::constructor;
@ -5,26 +6,32 @@ Napi::FunctionReference NodeSRT::constructor;
Napi::Object NodeSRT::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "NodeSRT", {
InstanceMethod("hello", &NodeSRT::Hello)
Napi::Function func = DefineClass(env, "SRT", {
InstanceMethod("createSocket", &NodeSRT::CreateSocket)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("NodeSRT", func);
exports.Set("SRT", func);
return exports;
}
NodeSRT::NodeSRT(const Napi::CallbackInfo& info) : Napi::ObjectWrap<NodeSRT>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
srt_startup();
}
Napi::Value NodeSRT::Hello(const Napi::CallbackInfo& info) {
NodeSRT::~NodeSRT() {
srt_cleanup();
}
Napi::Value NodeSRT::CreateSocket(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::String returnValue = Napi::String::New(env, "Hello World");
return returnValue;
SRTSOCKET socket = srt_create_socket();
return Napi::Number::New(env, socket);
}

View file

@ -4,8 +4,9 @@ class NodeSRT : public Napi::ObjectWrap<NodeSRT> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
NodeSRT(const Napi::CallbackInfo& info);
~NodeSRT();
private:
static Napi::FunctionReference constructor;
Napi::Value Hello(const Napi::CallbackInfo& info);
Napi::Value CreateSocket(const Napi::CallbackInfo& info);
};