Skip to content

Commit

Permalink
add binary message support
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-dogan committed Dec 10, 2020
1 parent 5bc0f6c commit 7c22b67
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
project(node_datachannel VERSION 0.0.17)
project(node_datachannel VERSION 0.0.18)

include_directories(${CMAKE_JS_INC})

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ peer2.onDataChannel((dc) => {
dc1 = peer1.createDataChannel("test");
dc1.onOpen(() => {
dc1.sendMessage("Hello from Peer1");
// Binary message: Use sendMessageBinary(Buffer)
});
dc1.onMessage((msg) => {
console.log('Peer1 Received Msg:', msg);
Expand Down
3 changes: 2 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class DataChannel {
close: () => void;
getLabel: () => string;
sendMessage: (msg: string) => boolean;
sendMessageBinary: (buffer: Buffer) => boolean;
isOpen: () => boolean;
availableAmount: () => Number;
bufferedAmount: () => Number;
Expand All @@ -56,7 +57,7 @@ export class DataChannel {
onError: (cb: (err: string) => void) => void;
onAvailable: (cb: () => void) => void;
onBufferedAmountLow: (cb: () => void) => void;
onMessage: (cb: (msg: string) => void) => void;
onMessage: (cb: (msg: string | Buffer) => void) => void;
}

export class PeerConnection {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-datachannel",
"version": "0.0.17",
"version": "0.0.18",
"description": "libdatachannel node bindings",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
41 changes: 38 additions & 3 deletions src/data-channel-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Napi::Object DataChannelWrapper::Init(Napi::Env env, Napi::Object exports)
{InstanceMethod("close", &DataChannelWrapper::close),
InstanceMethod("getLabel", &DataChannelWrapper::getLabel),
InstanceMethod("sendMessage", &DataChannelWrapper::sendMessage),
InstanceMethod("sendMessageBinary", &DataChannelWrapper::sendMessageBinary),
InstanceMethod("isOpen", &DataChannelWrapper::isOpen),
InstanceMethod("availableAmount", &DataChannelWrapper::availableAmount),
InstanceMethod("bufferedAmount", &DataChannelWrapper::bufferedAmount),
Expand Down Expand Up @@ -87,9 +88,14 @@ DataChannelWrapper::DataChannelWrapper(const Napi::CallbackInfo &info) : Napi::O
// This will run in main thread and needs to construct the
// arguments for the call
Napi::Object payload = Napi::Object::New(env);
// FIX ME
// Binary Message?
args = {Napi::String::New(env, std::get<std::string>(message))};
if (std::holds_alternative<std::string>(message))
{
args = {Napi::String::New(env, std::get<std::string>(message))};
}
else
{
args = {Napi::Buffer<std::byte>::Copy(env, std::get<rtc::binary>(message).data(), std::get<rtc::binary>(message).size())};
}
});
});
}
Expand Down Expand Up @@ -171,6 +177,35 @@ Napi::Value DataChannelWrapper::sendMessage(const Napi::CallbackInfo &info)
}
}

Napi::Value DataChannelWrapper::sendMessageBinary(const Napi::CallbackInfo &info)
{
if (!mDataChannelPtr)
{
Napi::Error::New(info.Env(), "It seems data-channel is destroyed!").ThrowAsJavaScriptException();
return info.Env().Null();
}

Napi::Env env = info.Env();
int length = info.Length();

if (length < 1 || !info[0].IsBuffer())
{
Napi::TypeError::New(env, "Buffer expected").ThrowAsJavaScriptException();
return info.Env().Null();
}

try
{
Napi::Uint8Array buffer = info[0].As<Napi::Uint8Array>();
return Napi::Boolean::New(info.Env(), mDataChannelPtr->send((std::byte *)buffer.Data(), buffer.ByteLength()));
}
catch (std::exception &ex)
{
Napi::Error::New(env, std::string("libdatachannel error while sending dataChannel msg# ") + ex.what()).ThrowAsJavaScriptException();
return Napi::Boolean::New(info.Env(), false);
}
}

Napi::Value DataChannelWrapper::isOpen(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Expand Down
1 change: 1 addition & 0 deletions src/data-channel-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class DataChannelWrapper : public Napi::ObjectWrap<DataChannelWrapper>
void close(const Napi::CallbackInfo &info);
Napi::Value getLabel(const Napi::CallbackInfo &info);
Napi::Value sendMessage(const Napi::CallbackInfo &info);
Napi::Value sendMessageBinary(const Napi::CallbackInfo &info);
Napi::Value isOpen(const Napi::CallbackInfo &info);
Napi::Value availableAmount(const Napi::CallbackInfo &info);
Napi::Value bufferedAmount(const Napi::CallbackInfo &info);
Expand Down
1 change: 1 addition & 0 deletions test/connectivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ peer2.onDataChannel((dc) => {
dc1 = peer1.createDataChannel("test");
dc1.onOpen(() => {
dc1.sendMessage("Hello from Peer1");
// Binary message: Use sendMessageBinary(Buffer)
});
dc1.onMessage((msg) => {
console.log('Peer1 Received Msg:', msg);
Expand Down

0 comments on commit 7c22b67

Please sign in to comment.