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(send): specify complete HOST instead of just the IP to be able to use ngrok #3

Open
wants to merge 3 commits into
base: main
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
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# ws-midi
<h1>ws-midi</h1>
> Send MIDI using WebSocket communication

<p align="center"><img alt="Animated image of two terminals demonstrating the usage of ws-midi" src="https://github.com/vcync/ws-midi/raw/main/images/ws-midi.gif" width="100%" /></p>

---

<!-- TOC -->

- [Usage](#usage)
- [Usage with ngrok](#usage-with-ngrok)
- [Requirements](#requirements)

<!-- /TOC -->

---

## Usage

* Clone the repo
Expand All @@ -13,6 +25,19 @@
* When it asks for an IP, that's your WAN IP (search "what's my IP" on Google)
* The receiver will have a virtual MIDI device called "ws-midi" to use in MIDI-capable apps


### Usage with ngrok

To get the connection working over network where we don't have access to the network-settings, we can use a service like ngrok to tunnel the connection:

* Setup account at ngrok
* Start ws-midi receiver on the remote host that should receive the signal
* Start ngrok with `ngrok http 5006` (the port used by ws-midi) on the remote host
* Start ws-midi send on the local host that should send the signal to the remote host
* Use the host url as provided by ngrok in this format: `wss://<ngrok-url>` (we don't need the port as this goes over HTTPS)

This should then pipe the signal from a local host to the remote host and you should see the MIDI signals arriving there.

## Requirements

* node.js
Expand Down
22 changes: 21 additions & 1 deletion receive.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const term = require('terminal-kit').terminal;
const midi = require('midi');
const WebSocket = require('ws');
const { VIRTUAL_MIDI_DEVICE_NAME, PORT } = require('./constants.js');
let ip;

function heartbeat() {
this.isAlive = true;
}

module.exports = () => {
term.clear();
Expand Down Expand Up @@ -45,12 +48,29 @@ module.exports = () => {

const wss = new WebSocket.Server({ port: PORT });

const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();

ws.isAlive = false;
ws.ping();
});
}, 10 * 1000);

wss.on('close', function close() {
clearInterval(interval);
});

wss.on('listening', function listening() {
term.black.bgGreen(`websocket server listening on ${PORT}\n`);
});

wss.on('connection', function connection(ws) {
console.log('new connection');

ws.isAlive = true;
ws.on('error', console.error);
ws.on('pong', heartbeat);

ws.on('message', function incoming(wsMessage) {
const message = JSON.parse(wsMessage)
Expand Down
16 changes: 8 additions & 8 deletions send.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const term = require('terminal-kit').terminal;
const midi = require('midi');
const WebSocket = require('ws');
const { VIRTUAL_MIDI_DEVICE_NAME, PORT } = require('./constants.js');
let ip;
let host;

module.exports = () => {
// Set up a new input.
Expand Down Expand Up @@ -37,20 +37,20 @@ module.exports = () => {
}
}

term.cyan(`please enter the IP of the recipient\n`);
term.cyan(`please enter the HOST of the recipient in the format ws://host[:port] or wss://host[:port]\n`);

term.inputField((error, response) => {
term.clear();
ip = response;
host = response;

term.cyan(`opening websocket connection to ${ip}:${PORT}\n`);
const ws = new WebSocket(`ws://${ip}:${PORT}`);
term.cyan(`opening websocket connection to ${host}\n`);
const ws = new WebSocket(`${host}`);

ws.on('open', () => {
term.clear();
term.cyan(`opened websocket connection to ${ip}:${PORT}\n\n`);
term.cyan(`opened websocket connection to ${host}\n\n`);

term.cyan(`please pick a midi input device to send to ${ip}:${PORT}\n`);
term.cyan(`please pick a midi input device to send to ${host}\n`);

term.singleColumnMenu(inputDeviceNames, {
cancelable: true,
Expand All @@ -66,7 +66,7 @@ module.exports = () => {

const midiInputDeviceId = response.selectedIndex;

term.cyan(`opened websocket connection to ${ip}:${PORT}\n\n`);
term.cyan(`opened websocket connection to ${host}\n\n`);

term.cyan(`please pick a midi output device to route locally`);

Expand Down