From 91d986127d6aae2d93065cef8de7534e715dd28d Mon Sep 17 00:00:00 2001 From: birme Date: Sat, 13 Jun 2020 09:43:20 +0200 Subject: [PATCH] Create socket binding --- binding.gyp | 8 ++++---- example.js | 6 ++++++ index.js | 7 +------ src/node-srt.cc | 19 +++++++++++++------ src/node-srt.h | 3 ++- 5 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 example.js diff --git a/binding.gyp b/binding.gyp index 21f0ae1..b6a8daf 100644 --- a/binding.gyp +++ b/binding.gyp @@ -7,13 +7,13 @@ "src/binding.cc", "src/node-srt.cc" ], - 'include_dirs': [ + "include_dirs": [ " #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(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); } \ No newline at end of file diff --git a/src/node-srt.h b/src/node-srt.h index d53af28..92d9ccc 100644 --- a/src/node-srt.h +++ b/src/node-srt.h @@ -4,8 +4,9 @@ class NodeSRT : public Napi::ObjectWrap { 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); }; \ No newline at end of file