Pārlūkot izejas kodu

Added examples to validate peerjs in node

- Added dthe examples dir with a simple echo server and client setup
- Changed the serialization of the data from Binary to JSON because
Binary refuses to work
    - It would have been nice to get Binary working though, I will have
      to look into it more
- Removed the contents of `footer_patch.js` because they were not having
the desired effect
- Added the `index.js` as the main entry point of the module to prevent
users from having to do `require('node-peerjs').default` because
`parcel` does not package for nodejs right now
Satnam Singh Brar 5 gadi atpakaļ
vecāks
revīzija
f51aad859a
6 mainītis faili ar 69 papildinājumiem un 5 dzēšanām
  1. 2 2
      build.sh
  2. 35 0
      examples/echo_client.js
  3. 18 0
      examples/echo_server.js
  4. 0 3
      footer_patch.js
  5. 1 0
      index.js
  6. 13 0
      patch.diff

+ 2 - 2
build.sh

@@ -16,7 +16,7 @@ git diff -- . ':(exclude)dist/*' > ../patch.diff
 cd ../
 
 # Append some of our modifications onto the built peerjs code
-cat header_patch.js dist/peerjs.min.js footer_patch.js > dist/react-native-peerjs.js
+cat header_patch.js dist/peerjs.min.js footer_patch.js > dist/node-peerjs.js
 rm dist/peerjs.min.js
 
-echo "Done. dist/react-native-peerjs.js"
+echo "Done. dist/node-peerjs.js"

+ 35 - 0
examples/echo_client.js

@@ -0,0 +1,35 @@
+const Peer = require('../index.js');
+
+// FUNCTIONS --------------------------------------------------------------------------------------
+
+const readline = require('readline').createInterface({
+    input: process.stdin,
+    output: process.stdout
+});
+
+async function askForInput(promptStr){
+    return new Promise((resolve, reject) => {
+        readline.question(promptStr, (input) => {
+            resolve(input);
+        });
+    })
+}
+
+// ------------------------------------------------------------------------------------------------
+
+const peer = new Peer({debug: 2});
+
+peer.on('open', async (localId) => {
+    console.log(localId);
+
+    const conn = peer.connect('abcdefghijklmnopqrstuvwxyz');
+    conn.on('data', (data) => {
+        console.log(data);
+    });
+
+    while (true) {
+        const data = await askForInput('>');
+        conn.send(data);
+    }
+})
+

+ 18 - 0
examples/echo_server.js

@@ -0,0 +1,18 @@
+const Peer = require('../index.js');
+
+// MAIN -------------------------------------------------------------------------------------------
+
+const peer = new Peer('abcdefghijklmnopqrstuvwxyz', {debug: 2});
+
+peer.on('open', (localId) => {
+    console.log(localId);
+})
+
+peer.on('connection', (conn) => {
+    console.log('Got a connection');
+    conn.on('data', (data) => {
+        console.log(data);
+        conn.send(`echo: ${data}`);
+    });
+    conn.send('Hello, I am the echo server');
+});

+ 0 - 3
footer_patch.js

@@ -1,3 +0,0 @@
-
-
-exports = exports.default;

+ 1 - 0
index.js

@@ -0,0 +1 @@
+module.exports = require('./dist/node-peerjs.js').default;

+ 13 - 0
patch.diff

@@ -1,3 +1,16 @@
+diff --git a/lib/dataconnection.ts b/lib/dataconnection.ts
+index aec3c05..f0ca925 100644
+--- a/lib/dataconnection.ts
++++ b/lib/dataconnection.ts
+@@ -58,7 +58,7 @@ export class DataConnection extends BaseConnection implements IDataConnection {
+       this.options.connectionId || DataConnection.ID_PREFIX + util.randomToken();
+ 
+     this.label = this.options.label || this.connectionId;
+-    this.serialization = this.options.serialization || SerializationType.Binary;
++    this.serialization = this.options.serialization || SerializationType.JSON;
+     this.reliable = !!this.options.reliable;
+ 
+     this._encodingQueue.on('done', (ab: ArrayBuffer) => {
 diff --git a/lib/exports.ts b/lib/exports.ts
 index 5772d02..63a57e3 100644
 --- a/lib/exports.ts