Skip to content

Commit e46883e

Browse files
louisgvColdSauce
authored andcommitted
Full WebRTC Support (#40)
* WIP * Adding data event handler on browser * WIP * WIP * Implement simple if-else on join * derp * Need to investigate where did browser got data * Got the data * Adding clean up events * Remove unnecessary code. * Remove hard coded process env local. * Remove TODOS and commented out code.
1 parent 3d701c2 commit e46883e

File tree

7 files changed

+93
-34
lines changed

7 files changed

+93
-34
lines changed

packages/hyperproxy-browser/public/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ <h2>
6868

6969
<section id="browser"
7070
class="enabled">
71-
<div class="hidden">
72-
<h2>UID-<span id="swarm-id" class="light-font"></span></h2>
71+
<div>
72+
<h2 class="hidden">UID-<span id="swarm-id" class="light-font"></span></h2>
7373
<!-- <h3><span class="bold-font">URL</span><span id="active-daturl" class="light-font"></span></h3> -->
7474
</div>
7575

packages/hyperproxy-browser/src/index.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ function init() {
1919
const urlFormEl = document.querySelector('#url-form');
2020
const contentEl = document.querySelector('#content');
2121

22+
const originalOnMessage = client.hub.onMessage;
23+
24+
window.addEventListener('unload', ()=>{
25+
client.hub.broadcast(activeHash, {
26+
from: client.swarm.me,
27+
type: HUB_MSG_TYPE.LEAVE
28+
});
29+
});
30+
31+
client.swarm.on('peer', function(peer) {
32+
peer.on('data', function(data) {
33+
renderOutput(data);
34+
});
35+
});
36+
37+
function renderOutput(data) {
38+
const {body, type, from} = JSON.parse(Buffer.from(data).toString('utf8'));
39+
const value = body.type === 'Buffer'
40+
? Buffer.from(body.data).toString('utf8')
41+
: body.data;
42+
43+
contentEl.innerHTML = value;
44+
}
45+
2246
document.querySelector('#swarm-id').innerHTML = client.swarm.me;
2347

2448
document.querySelector('#subscribe').addEventListener('click', () => {
@@ -62,7 +86,7 @@ function init() {
6286
});
6387
});
6488

65-
document.querySelector('#try-out').addEventListener('click', ()=>{
89+
document.querySelector('#try-out').addEventListener('click', () => {
6690
introEl.classList.remove('enabled');
6791
});
6892
}

packages/hyperproxy-config/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const HUB_NAME = 'hyperproxy';
66

77
const HUB_IP = process.env.HUB_IP || 'localhost';
88
const HUB_PORT = process.env.HUB_PORT || 9999;
9-
109
const HUB_URL = process.env.LOCAL
1110
? `ws://${HUB_IP}:${HUB_PORT}/`
1211
: `wss://hub.hyperproxy.network/`;
@@ -18,8 +17,8 @@ module.exports = Object.freeze({
1817
HUB_URL,
1918
HUB_MSG_TYPE: {
2019
JOIN: 'JOIN',
20+
LEAVE: 'LEAVE',
2121
REQUEST: 'REQUEST',
2222
RESPONSE: 'RESPONSE',
23-
DISCONNECT: 'DISCONNECT'
2423
}
2524
});

packages/hyperproxy-hub-client/src/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import swarm from 'webrtc-swarm';
22
import signalhub from 'signalhubws';
33

44
import HyperProxyLogger from 'hyperproxy-logger';
5-
import {HUB_NAME, HUB_URL} from 'hyperproxy-config';
5+
import { HUB_NAME, HUB_URL } from 'hyperproxy-config';
66

77
const logger = new HyperProxyLogger('hyperproxy-hub');
88

@@ -11,10 +11,10 @@ export default class HyperproxyHubClient {
1111
constructor(wsClass, wrtc = null, name = HUB_NAME, url = HUB_URL) {
1212
this.hub = signalhub(name, [url], wsClass);
1313
this.swarm = wrtc
14-
? swarm(this.hub, {wrtc})
14+
? swarm(this.hub, { wrtc })
1515
: swarm(this.hub);
1616

17-
this.handleSwarmEvent();
17+
this.handleSwarmEvent();
1818
}
1919

2020
/* Waiting Coroutine */
@@ -33,20 +33,20 @@ export default class HyperproxyHubClient {
3333
const sw = this.swarm;
3434

3535
/* Swarm Event */
36-
sw.on('close', function(e) {
36+
sw.on('close', function (e) {
3737
sw.close();
3838
});
3939

40-
sw.on('message', function(m) {
40+
sw.on('message', function (m) {
4141
logger.info(m, 'new swarm message');
4242
});
4343

44-
sw.on('peer', function(peer, id) {
44+
sw.on('peer', function (peer, id) {
4545
logger.info(id, 'peer connected');
4646
logger.info(sw.peers.length, 'total peers');
4747
});
4848

49-
sw.on('disconnect', function(peer, id) {
49+
sw.on('disconnect', function (peer, id) {
5050
logger.info(id, 'peer disconnected');
5151
logger.info(sw.peers.length, 'total peers');
5252
});

packages/hyperproxy-hub/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
"hyperproxy-hub": "./index.js"
88
},
99
"scripts": {
10+
"dev": "devtool -r babel-register src/index.js",
1011
"start": "node lib/index.js",
11-
"watch": "babel-watch src/index.js",
12+
"watch": "babel-watch --inspect src/index.js",
1213
"build": "babel src -d lib",
1314
"test": "echo \"Error: no test specified\" && exit 1"
1415
},

packages/hyperproxy-hub/src/index.js

+39-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import HyperProxyNode from 'hyperproxy-node';
55
import HyperProxyLogger from 'hyperproxy-logger';
66

77
const datMap = new Map();
8+
const nodeMap = new Map();
89

910
const wss = new WebSocket.Server({port});
1011

@@ -28,24 +29,49 @@ function onConnection(ws) {
2829
return;
2930
}
3031

31-
logger.info(jsond, 'new websocket message');
32-
3332
// if not all channel and no HyperProxy-node has been assigned
34-
if (jsond.channel !== 'all' && jsond.message.type === HUB_MSG_TYPE.JOIN) {
35-
if (!datMap[jsond.channel]) {
36-
logger.info(jsond.channel, 'spawn new hyperproxy-node');
37-
datMap[jsond.channel] = new HyperProxyNode(jsond.channel);
33+
if (jsond.channel !== 'all') {
34+
logger.info(jsond, 'Got message');
35+
36+
if (jsond.message.type === HUB_MSG_TYPE.JOIN) {
37+
logger.info('received a JOIN event');
38+
39+
if (!datMap[jsond.channel]) {
40+
logger.info(jsond.channel, 'spawn new hyperproxy-node');
41+
const nodeInstance = new HyperProxyNode(jsond.channel);
42+
43+
//TODO: Extends this to be a more generic object that has peer ID
44+
datMap[jsond.channel] = {
45+
[nodeInstance.client.swarm.me]: true,
46+
[jsond.message.from]: true
47+
};
48+
nodeMap[jsond.channel] = nodeInstance;
49+
} else {
50+
datMap[jsond.channel][jsond.message.from] = true;
51+
return;
52+
}
3853
}
3954

40-
return;
41-
}
55+
if (jsond.message.type === HUB_MSG_TYPE.LEAVE) {
56+
logger.info('received a LEAVE event');
57+
delete datMap[jsond.channel][jsond.message.from];
58+
if (Object.keys(datMap[jsond.channel]).length === 1) {
59+
nodeMap[jsond.channel].close();
60+
delete nodeMap[jsond.channel];
61+
delete datMap[jsond.channel];
62+
}
4263

43-
wss.clients.forEach((client) => {
44-
if (jsond.app === client.app) {
45-
logger.info(client.app, 'broadcasting');
46-
client.send(data);
4764
}
48-
});
65+
}
66+
67+
wss
68+
.clients
69+
.forEach((client) => {
70+
if (jsond.app === client.app) {
71+
logger.info(client.app, 'broadcasting');
72+
client.send(data);
73+
}
74+
});
4975
});
5076
}
5177

packages/hyperproxy-node/src/index.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export default class HyperproxyNode {
2121
}
2222
}
2323

24+
close(){
25+
this.client.swarm.close();
26+
this.client.hub.close();
27+
}
28+
2429
/*
2530
Read a file from the subscribed dat archive asyncornously
2631
*/
@@ -40,11 +45,11 @@ export default class HyperproxyNode {
4045
/*
4146
Connect to the hub under the specified key.
4247
*/
43-
async _connectToHub(key) {
44-
const client = new HyperproxyHubClient(WebSocket, Wrtc);
45-
client.hub.subscribe(key).on('data', (data) => {
48+
_connectToHub(key) {
49+
this.client = new HyperproxyHubClient(WebSocket, Wrtc);
50+
this.client.hub.subscribe(key).on('data', (data) => {
4651
try {
47-
this._handleData(client, key, data);
52+
this._handleData(key, data);
4853
} catch (e) {
4954
logger.error(e, 'cannot handle data');
5055
}
@@ -54,18 +59,21 @@ export default class HyperproxyNode {
5459
/*
5560
Handle any peer data by getting them from TCP/UDP dat
5661
*/
57-
async _handleData(client, key, {from, body, type}) {
62+
async _handleData(key, {from, body, type}) {
5863
if (type === HUB_MSG_TYPE.RESPONSE) {
5964
return;
6065
}
6166

6267
if (type === HUB_MSG_TYPE.REQUEST) {
68+
6369
const file = await this.readFile(body);
64-
client.hub.broadcast(key, {
65-
from: client.swarm.me,
70+
const payload = JSON.stringify({
71+
from: this.client.swarm.me,
6672
type: HUB_MSG_TYPE.RESPONSE,
6773
body: file
6874
});
75+
76+
this.client.swarm.remotes[from].send(Buffer.from(payload));
6977
}
7078
}
7179

@@ -92,4 +100,5 @@ export default class HyperproxyNode {
92100
});
93101
});
94102
}
95-
}
103+
}
104+

0 commit comments

Comments
 (0)