Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RTC connection-v2, related #17 #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,95 @@ web_client/index.html
*-autosave.kra
*.kra~
*.png~

## Generated by vscode-gitignore
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

## End Generated by vscode-gitignore
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "unnamed",
"version": "1.0.0",
"main": "index.js",
"repository": "[email protected]:pastleo/unnamed.git",
"author": "Lego Chiang <[email protected]>",
"license": "MIT",
"dependencies": {
"debug": "^4.1.1",
"uuid": "^3.3.2",
"ws": "^7.0.0"
}
}
3 changes: 3 additions & 0 deletions server/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const debug = require('debug');

exports.websocket = debug('bazaar:websocket');
57 changes: 57 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const uuid = require('uuid/v4');
const wss = require('./wss');
const debug = require('./debug');

/**
* @type {Map<WebSocket, string>} wsToIdMap
*/
const wsToIdMap = new Map();
/**
* @type {Map<string, WebSocket>} idToWsMap
*/
const idToWsMap = new Map();

wss.on('connection', ws => {
const id = uuid();
wsToIdMap.set(ws, id);
idToWsMap.set(id, ws);

debug.websocket('connection', id);

ws.on('message', message => {
const messageJson = JSON.parse(message);
debug.websocket('message', messageJson);

const { type, from, to, payload } = messageJson;

if (to === 'broadcast') {
return wss.clients.forEach(client => {
if (client !== ws) {
client.send(message);
}
});
}

const client = idToWsMap.get(to);
if (wss.clients.has(client)) {
return client.send(message);
}

debug.websocket('This messsage do nothing.');
});

ws.on('close', () => {
wsToIdMap.delete(ws);
idToWsMap.delete(id);
});

ws.send(JSON.stringify({
type: 'initial-information',
payload: {
id,
peers: [...wsToIdMap.values()].filter(
peerId => peerId !== id
),
},
}));
});
29 changes: 29 additions & 0 deletions server/wss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const WebSocket = require('ws');

const port = process.env.PORT || 9527;

// Copy pasta from https://github.com/websockets/ws
const wss = new WebSocket.Server({
port,
perMessageDeflate: {
zlibDeflateOptions: {
// See zlib defaults.
chunkSize: 1024,
memLevel: 7,
level: 3
},
zlibInflateOptions: {
chunkSize: 10 * 1024
},
// Other options settable:
clientNoContextTakeover: true, // Defaults to negotiated value.
serverNoContextTakeover: true, // Defaults to negotiated value.
serverMaxWindowBits: 10, // Defaults to negotiated value.
// Below options specified as default values.
concurrencyLimit: 10, // Limits zlib concurrency for perf.
threshold: 1024 // Size (in bytes) below which messages
// should not be compressed.
}
});

module.exports = wss;
6 changes: 6 additions & 0 deletions web_client/lib/bazaar-connection/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {RTCIceServer[]} iceServers
*/
export const iceServers = [
{ urls: 'stun:stun.l.google.com:19302' },
]
9 changes: 9 additions & 0 deletions web_client/lib/bazaar-connection/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'https://unpkg.com/[email protected]/dist/debug.js';

localStorage.debug = '';

export const websocket = debug('bazaar:websocket');

export const connection = debug('bazaar:connection');

export const rtc = debug('bazaar:rtc');
Loading