From 72ef91b3cd482b3d5347553a34a044213a9f5e19 Mon Sep 17 00:00:00 2001 From: Max Ogden Date: Thu, 26 Mar 2015 21:59:45 -0700 Subject: [PATCH] hook up more UI stuff and lobby server --- index.html | 8 +++++ index.js | 83 ++++++++++++++++++++++++++++++++++++++++--------- limit-stream.js | 19 ----------- lobby.js | 74 ------------------------------------------- package.json | 9 ++---- 5 files changed, 78 insertions(+), 115 deletions(-) delete mode 100644 limit-stream.js delete mode 100644 lobby.js diff --git a/index.html b/index.html index 2908632..b861bac 100644 --- a/index.html +++ b/index.html @@ -13,6 +13,14 @@ +
+
+

+ You are now sharing your screen. Press CMD+Q to Quit. +

+
+
+
diff --git a/index.js b/index.js index 43bc4f7..d5ff834 100644 --- a/index.js +++ b/index.js @@ -5,8 +5,11 @@ var SimplePeer = require('simple-peer') var clipboard = require('clipboard') var request = require('request') +var ssejson = require('ssejson') var DEV = process.env.LOCALDEV || false +var server = 'http://catlobby.maxogden.com' +// var server = 'http://localhost:5005' var video, videoSize, robot @@ -15,7 +18,8 @@ var containers = { join: document.querySelector('.join-container'), content: document.querySelector('.content-container'), choose: document.querySelector('.choose-container'), - video: document.querySelector('.video-container') + video: document.querySelector('.video-container'), + sharing: document.querySelector('.sharing-container') } var buttons = { @@ -65,6 +69,7 @@ function startHandshake(remote) { navigator.webkitGetUserMedia(constraints, function(stream) { var peer = new SimplePeer({ initiator: true, stream: stream, trickle: false }) console.log('host peer', peer) + inputs.copy.value = 'Loading...' handleSignal(peer, remote) }, function(e) { if (e.code == e.PERMISSION_DENIED) { @@ -77,27 +82,67 @@ function startHandshake(remote) { function handleSignal(peer, remote) { window.peer = peer + var pingName + peer.on('signal', function (data) { // sdp is ~2.5k usually, that's too big for a URL, so we zlib deflate it - zlib.deflate(JSON.stringify(data), function(err, deflated) { + var stringified = JSON.stringify(data) + zlib.deflate(stringified, function(err, deflated) { var connectionString = deflated.toString('base64') var code = encodeURIComponent(connectionString) - inputs.copy.value = code - buttons.copy.addEventListener('click', function(e) { - e.preventDefault() - clipboard.writeText(code) - }) + console.log('sdp length', code.length) + + // upload pong sdp + if (remote) { + if (!pingName) return inputs.paste.value = 'Error! Please Quit' + request.post({body: code, uri: server + '/pong/' + pingName}, function resp (err, resp, body) { + if (err) return inputs.paste.value = err.message + }) + } + + // upload initial sdp + if (!remote) { + request.post({body: code, uri: server + '/ping'}, function resp (err, resp, body) { + if (err) return inputs.copy.value = 'Error! ' + err.message + var ping = JSON.parse(body) + inputs.copy.value = ping.name + buttons.copy.addEventListener('click', function(e) { + e.preventDefault() + clipboard.writeText(ping.name) + }) + + // listen for sdp pongs + var req = request(server + '/pongs/' + ping.name) + .pipe(ssejson.parse()) + .on('data', function data (pong) { + console.log('pong sdp length', pong.length) + inflate(pong, function inflated (err, stringified) { + if (err) return inputs.copy.value = 'Error! Please Quit' + peer.signal(JSON.parse(stringified.toString())) + req.end() + }) + }) + .on('error', function error (err) { + inputs.copy.value = err.message + }) + }) + } }) }) buttons.paste.addEventListener('click', function(e) { e.preventDefault() - var code = inputs.paste.value - if (!code) return - code = decodeURIComponent(code) - zlib.inflate(new Buffer(code, 'base64'), function(err, inflated) { - if (err) return - peer.signal(JSON.parse(inflated.toString())) + var ping = inputs.paste.value + inputs.paste.value = 'Connecting...' + if (!ping) return + request({uri: server + '/ping/' + ping}, function resp (err, resp, data) { + if (err) return inputs.paste.value = 'Error! ' + err.message + console.log('sdp response length', data.length) + inflate(data, function inflated (err, stringified) { + if (err) return + pingName = ping + peer.signal(JSON.parse(stringified.toString())) + }) }) }) @@ -112,7 +157,10 @@ function handleSignal(peer, remote) { function onConnect() { containers.content.className += ' dn' // hide ui - if (!remote) return + if (!remote) { + containers.sharing.className += ' db' // show + return + } console.log('start sending...') window.addEventListener('mousedown', function mousedown (e) { @@ -150,7 +198,12 @@ function handleSignal(peer, remote) { return data } } - + + function inflate (data, cb) { + data = decodeURIComponent(data.toString()) + zlib.inflate(new Buffer(data, 'base64'), cb) + } + peer.on('stream', function (stream) { video = document.createElement('video') video.src = window.URL.createObjectURL(stream) diff --git a/limit-stream.js b/limit-stream.js deleted file mode 100644 index ddaff06..0000000 --- a/limit-stream.js +++ /dev/null @@ -1,19 +0,0 @@ -var through = require('through2') - -module.exports = function limiter (limit) { - var stream = through(write) - - var len = 0 - - return stream - - function write (ch, enc, next) { - if (Buffer.isBuffer(ch)) len += ch.length - else len += 1 - - if (len >= limit) this.destroy(new Error('Limit exceeded')) - else this.push(ch) - - next() - } -} diff --git a/lobby.js b/lobby.js deleted file mode 100644 index d619d09..0000000 --- a/lobby.js +++ /dev/null @@ -1,74 +0,0 @@ -var http = require('http') - -var HttpHashRouter = require('http-hash-router') -var catNames = require('cat-names') -var concat = require('concat-stream') -var pumpify = require('pumpify') - -var limitStream = require('./limit-stream.js') - -var router = HttpHashRouter() -var port = process.env.PORT || 5005 - -var rooms = {} - -router.set('/room', function room (req, res, opts, cb) { - if (req.method !== "POST") { - var err = new Error('Only POST is allowed') - err.statusCode = 405 - return cb(err) - } - - var limiter = limitStream(1024 * 5) // 5kb max - limiter.on('error', cb) - - var concatter = concat(function concatted (buff) { - var room = makeName() - rooms[room] = buff - setTimeout(function expire () { - delete rooms[room] - }, 1000 * 60 * 30) // 30 mins - res.setHeader('content-type', 'application/json') - res.end(JSON.stringify({name: room})) - }) - - pumpify(req, limiter, concatter) -}) - -router.set('/rooms/:id', function room (req, res, opts, cb) { - var code = rooms[opts.params.id] - if (!code) { - var err = new Error('Doesnt exist or expired') - err.statusCode = 404 - return cb(err) - } - res.setHeader('content-type', 'application/json') - res.end(JSON.stringify({code: code.toString()})) - cb() -}) - -var server = http.createServer(function handler (req, res) { - router(req, res, {}, onError) - - function onError (err) { - if (err) { - res.statusCode = err.statusCode || 500 - res.end(err.message) - } - } -}) - -server.listen(port, function listening (err) { - if (err) return console.error(err) - console.log('Listening on port', port) -}) - -function makeName () { - var n = [rnd(), rnd(), rnd()].join('-') - if (rooms[n]) return makeName() - return n -} - -function rnd () { - return catNames.random().toLowerCase().replace(/\s/g, '-') -} diff --git a/package.json b/package.json index daeec16..cfb299c 100644 --- a/package.json +++ b/package.json @@ -5,23 +5,18 @@ "main": "app.js", "scripts": { "app": "atom-shell app.js", - "build": "atom-shell-packager . Screenshare", - "start": "node lobby.js" + "build": "atom-shell-packager . Screenshare" }, "author": "max ogden", "license": "BSD", "dependencies": { - "cat-names": "^1.0.2", - "concat-stream": "^1.4.7", "debug": "^2.1.0", - "http-hash-router": "^1.1.0", "menubar": "^2.0.3", - "pumpify": "^1.3.3", "request": "^2.54.0", "robotjs": "maxogden/robotjs#keyupdown", "simple-peer": "^4.0.4", + "ssejson": "^1.2.0", "throttleit": "^1.0.0", - "through2": "^0.6.3", "vkey": "^1.0.0" }, "devDependencies": {