This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d11402
commit 56d1fa9
Showing
8 changed files
with
150 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,4 +104,3 @@ tmp | |
|
||
# Files to ignore # | ||
################### | ||
cli.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env node | ||
|
||
const path = require(`path`); | ||
//require(path.join(`../cli`)) | ||
require(path.join(__dirname, `../cli`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
const program = require('commander') | ||
const { | ||
arrayBufferWithMime, | ||
arrayBufferMimeDecouple | ||
} = require('arraybuffer-mime') | ||
const WebSocket = require('ws') | ||
|
||
const packageJson = require('./package.json') | ||
|
||
program | ||
.version(packageJson.version) | ||
.option('-h, --host <host>', 'host URL', null, null) | ||
.option('--not-secure', 'not using SSL.', null, false) | ||
.option('-c, --channel <id>', 'channel ID', null, null) | ||
.option('-t, --text <text>', 'text to send', null, null) | ||
.option('-f, --file <filepath>', 'file to send', null, null) | ||
.command('post', 'post to a channel') | ||
.command('listen', 'listen on a channel') | ||
.action((cmd) => { | ||
const { | ||
host, | ||
notSecure, | ||
channel, | ||
text, | ||
file | ||
} = program | ||
|
||
if (!host || !channel) { | ||
program.outputHelp() | ||
return false | ||
} | ||
|
||
if (cmd === 'listen') { | ||
listen({ | ||
channel, | ||
host, | ||
notSecure | ||
}) | ||
} else if (cmd === 'post') { | ||
} | ||
}) | ||
|
||
program.parse(process.argv) | ||
|
||
function listen(props) { | ||
const ws = new WebSocket(constructWebsocketUrl(props)) | ||
ws.binaryType = 'arraybuffer' | ||
|
||
ws.on('open', () => { | ||
console.log('connected.') | ||
}) | ||
|
||
ws.on('error', (error) => { | ||
console.error(error) | ||
}) | ||
|
||
ws.on('message', (data) => { | ||
if (isArrayBuffer(data)) { | ||
const {mime, arrayBuffer} = arrayBufferMimeDecouple(data) | ||
const buffer = Buffer.from(arrayBuffer) | ||
|
||
// TODO | ||
// check mime type | ||
var str = buffer.toString('utf8') | ||
if (str) { | ||
console.log(`received ${new Date()}:`) | ||
console.log(str) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
function post(props) { | ||
const {host, channel, notSecure} = props | ||
const scheme = notSecure ? 'ws' : 'wss' | ||
|
||
|
||
const ws = new WebSocket(constructWebsocketUrl(props)) | ||
ws.binaryType = 'arraybuffer' | ||
|
||
ws.on('open', () => { | ||
const mime = 'text/plain' | ||
const arrayBuffer = str2ab('yabababb') | ||
const abWithMime = arrayBufferWithMime(arrayBuffer, mime) | ||
ws.send(abWithMime) | ||
}) | ||
|
||
ws.on('error', (error) => { | ||
if (/400|ECONNREFUSED/gi.test(error.message)) { | ||
console.error('no on is listening on ', path) | ||
} | ||
}) | ||
} | ||
|
||
function constructWebsocketUrl(props) { | ||
const {host, channel, notSecure} = props | ||
const scheme = notSecure ? 'ws' : 'wss' | ||
|
||
if (!host || !channel) { | ||
program.outputHelp() | ||
return false | ||
} | ||
|
||
const path = (channel||'').replace(/^\/?/, '/') | ||
|
||
return `${scheme}://${host}${path}` | ||
} | ||
|
||
//https://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers | ||
function str2ab(str) { | ||
var buf = new ArrayBuffer(str.length*2) // 2 bytes for each char | ||
var bufView = new Uint16Array(buf) | ||
for (var i=0, strLen=str.length; i<strLen; i++) { | ||
bufView[i] = str.charCodeAt(i) | ||
} | ||
return buf | ||
} | ||
|
||
function isArrayBuffer(data) { | ||
return /ArrayBuffer/gi.test(Object.prototype.toString.call(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
( ) | ||
( ) | ||
( ) | ||
( ) | ||
) ) | ||
( ( /\ | ||
(_) / \ /\ | ||
________[_]________ /\/ \/ \ | ||
/\ /\ ______ \ / /\/\ /\/\ | ||
/ \ //_\ \ /\ \ /\/\/ \/ \ | ||
/\ / /\/\ //___\ \__/ \ \/ | ||
/ \ /\/ \//_____\ \ |[]| \ | ||
/\/\/\/ //_______\ \|__| \ | ||
/ \ /XXXXXXXXXX\ \ | ||
\ /_I_II I__I_\__________________\ | ||
I_I| I__I_____[]_|_[]_____I | ||
I_II I__I_____[]_|_[]_____I | ||
I II__I I XXXXXXX I | ||
~~~~~" "~~~~~~~~~~~~~~~~~~~~~~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters