Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
remove shoe, use ws
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jun 29, 2017
1 parent 28e65f2 commit fefaf88
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 9,204 deletions.
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ node_modules

# Bower #
#####################
#bower_components
bower_components

# Vim files to ignore #
#######################
.VimballRecord
.netrwhist

# Folders to ignore #
#####################
Expand All @@ -97,7 +102,6 @@ _archive
uploads
tmp

# Vim files to ignore #
#######################
.VimballRecord
.netrwhist
# Files to ignore #
###################
cli.js
4 changes: 4 additions & 0 deletions bin/streamhut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

const path = require(`path`);
//require(path.join(`../cli`))
49 changes: 49 additions & 0 deletions info
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!doctype html>
<html>
<head>
<title>streamhut</title>
<meta name="description" content="stream stuff" />
<meta name="viewport" content="initial-scale=1, user-scalable=yes, width=device-width" />
<link rel="stylesheet" href="./styles/main.css" />
</head>
<body>
<header id="header">
<h1>streamhut</h1>
<hgroup>
<small>
nothing is stored, only streamed. originally created this to quickly share data and files between devices.<br />
path names map to channels. anyone in the same channel can view what's streamed.</small>
<div>
<input id="share-url" type="text" placeholder="share url" />
</div>
</hgroup>
</header>

<pre id="log"></pre>

<form id="form">
<div class="form-group">
<label>Text</label>
<textarea id="text" rows="10" cols="20" placeholder="text"></textarea>
<input type="text" id="input" placeholder="message" />
</div>
<div class="form-group">
<label>File</label>
<div><input type="file" multiple id="file" /></div>
</div>

<div class="form-group">
<div><button type="submit">Submit</button></div>
</div>
</form>

<output id="output"></output>

<footer id="footer">
<a href="https://github.com/miguelmota/streamhut" target="_blank" title="fork this project on github">Fork me on github</a>
</footer>

<script src="./scripts/zeroclipboard.min.js"></script>
<script src="./scripts/bundle.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions info.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"websocket":true,"origins":["*:*"],"cookie_needed":false,"entropy":861548189}
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "streamlet",
"name": "streamhut",
"version": "0.0.1",
"description": "Stream stuff",
"main": "server.js",
"bin": {
"btc-dash": "bin/index.js"
},
"dependencies": {
"base64mime": "^0.0.1",
"base64toblob": "^0.0.1",
Expand All @@ -11,8 +14,8 @@
"hyperlinkify": "0.0.3",
"is-base64": "0.0.2",
"randomstring": "^1.0.3",
"shoe": "^0.0.15",
"through": "^2.3.6"
"sha3": "^1.2.0",
"ws": "^3.0.0"
},
"devDependencies": {
"watchify": "^3.9.0"
Expand All @@ -25,15 +28,19 @@
},
"repository": {
"type": "git",
"url": "https://github.com/miguelmota/streamlet"
"url": "https://github.com/miguelmota/streamhut"
},
"keywords": [
"stream"
],
"author": "Miguel Mota <hello@miguelmota.com> (http://www.miguelmota.com/)",
"author": {
"name": "Miguel Mota",
"email": "hello@miguelmota.com",
"url": "https://miguelmota.com/"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/miguelmota/streamlet/issues"
"url": "https://github.com/miguelmota/streamhut/issues"
},
"homepage": "https://github.com/miguelmota/streamlet"
"homepage": "https://github.com/miguelmota/streamhut"
}
69 changes: 44 additions & 25 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

const fs = require(`fs`);
const randomstring = require(`randomstring`);
const shoe = require(`shoe`);
const http = require(`http`);
const through = require(`through`);
const WebSocket = require('ws');
const SHA3Lib = require('sha3');

function sha3(data) {
const d = new SHA3Lib.SHA3Hash(256);
d.update(data);
return d.digest('hex');
}

process.setMaxListeners(0);

Expand All @@ -29,7 +35,7 @@ function callback(req, res) {
do {
const randString = genRandString();
Location = `/${randString}`;
} while (socks[Location] && socks[Location].clients.length);
} while (socks[Location] && socks[Location]._clients.length);

res.writeHead(301, {Location});
res.end();
Expand All @@ -51,52 +57,65 @@ function callback(req, res) {

function createSock(path) {
const clients = [];
const sock = shoe(stream => {
stream
.pipe(through((data, bar) => {
//console.log(`\n${path}\n---${data}---`);
clients.forEach(client => {
console.log(`streaming to ${client.id} ${path}`);
client.write(data);
});
}))
.on(`end`, () => {
console.log(`end`);
});

//stream.pipe(process.stdout, {end:false});
const sock = new WebSocket.Server({
server,
path
});

sock.on(`connection`, conn => {
if (!conn.id) {
conn.id = sha3(`${clients.length + 1}_${Date.now()}_${Math.random()}`)
}

clients.push(conn);

console.log(`connected ${conn.id} ${path}`);

const sendConnections = () => {
clients.forEach(client => {
client.write(JSON.stringify({
client.send(JSON.stringify({
__server_message__: {
data: {
connectionId: conn.id,
connections: clients.map(client => {
return {id: client.id}
})
.filter(x => (x.id !== conn.id))
connectionId: client.id,
connections: clients.filter(x => (x.id !== client.id))
.map(x => ({id: x.id}))
}
}}));
});
}

sendConnections();

conn.on('message', data => {
console.log('received: %s', data);
//console.log(`\n${path}\n---${data}---`);
clients.forEach(client => {
console.log(`streaming to ${client.id} ${path}`);
client.send(data);
});
});

conn.on(`close`, function() {
console.log(`close ${conn.id}`);
clients.splice(clients.indexOf(conn), 1);

const index = clients.reduce((index, client, i) => {
if (conn.id === client.id) {
return i;
}

return index
}, -1)

if (index > -1) {
clients.splice(index, 1);
}

sendConnections();
});
});

sock.install(server, `${path}___`);
sock.clients = clients;
sock._clients = clients;

return sock;
}
Expand Down
6 changes: 3 additions & 3 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<!doctype html>
<html>
<head>
<title>streamlet</title>
<title>streamhut</title>
<meta name="description" content="stream stuff" />
<meta name="viewport" content="initial-scale=1, user-scalable=yes, width=device-width" />
<link rel="stylesheet" href="./styles/main.css" />
</head>
<body>
<header id="header">
<h1>streamlet</h1>
<h1>streamhut</h1>
<hgroup>
<small>
nothing is stored, only streamed. originally created this to quickly share data and files between devices.<br />
Expand Down Expand Up @@ -40,7 +40,7 @@ <h1>streamlet</h1>
<output id="output"></output>

<footer id="footer">
<a href="https://github.com/miguelmota/streamlet" target="_blank" title="fork this project on github">Fork me on github</a>
<a href="https://github.com/miguelmota/streamhut" target="_blank" title="fork this project on github">Fork me on github</a>
</footer>

<script src="./scripts/zeroclipboard.min.js"></script>
Expand Down

0 comments on commit fefaf88

Please sign in to comment.