Skip to content

Commit

Permalink
add websocket example
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-dogan committed Jul 4, 2024
1 parent 6c90b6b commit 4db7b70
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# WebRTC For Node.js and Electron
# WebRTC For Node.js and Electron ( with Websocket)

![Linux CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Linux/badge.svg) ![Windows CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Win/badge.svg) ![Mac x64 CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Mac%20x64/badge.svg) ![Mac M1 CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Mac%20M1/badge.svg)

- Lightweight
- No need to deal with WebRTC stack!
- Small binary sizes (~8MB for Linux x64)
- Type infos for Typescript
- Integrated WebSocket Client & Server Implementation

This project is Node.js bindings for [libdatachannel](https://github.com/paullouisageneau/libdatachannel) library.

Expand Down Expand Up @@ -37,6 +38,12 @@ WebRTC polyfills to be used for libraries like `simple-peer`.

Please check [here](/polyfill)

## WebSocket Client & Server

Integrated WebSocket Client & Server is available, which can be used separately or for signaling.

For an example usage, [check here](/examples/websocket)

## Example Usage

```js
Expand All @@ -45,6 +52,9 @@ import nodeDataChannel from 'node-datachannel';
// Log Level
nodeDataChannel.initLogger('Debug');

// Integrated WebSocket available and can be used for signaling etc
// const ws = new nodeDataChannel.WebSocket();

let dc1 = null;
let dc2 = null;

Expand Down
20 changes: 20 additions & 0 deletions examples/websocket/websocket-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as nodeDataChannel from '../../lib/index.js';

const clientSocket = new nodeDataChannel.WebSocket();

clientSocket.open('ws://127.0.0.1:3000');

clientSocket.onOpen(() => {
console.log('Client socket opened');
clientSocket.sendMessage('Echo this!');
});

clientSocket.onMessage((message) => {
console.log('Message from server: ' + message);
});

clientSocket.onClosed(() => {
console.log('Client socket closed');
});

// clientSocket.close();
22 changes: 22 additions & 0 deletions examples/websocket/websocket-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as nodeDataChannel from '../../lib/index.js';

const ws = new nodeDataChannel.WebSocketServer({ bindAddress: '127.0.0.1', port: 3000 });

ws.onClient((clientSocket) => {
clientSocket.onOpen(() => {
console.log('Client socket opened');
clientSocket.sendMessage('Hello from server');
});

clientSocket.onMessage((message) => {
console.log('Message from client: ' + message);
clientSocket.sendMessage(message);
});

clientSocket.onClosed(() => {
console.log('Client socket closed');
});
});

// When done, close the server
// ws.stop();

0 comments on commit 4db7b70

Please sign in to comment.