'use strict'; // configuration values (these are the only things that should need to be changed) var config = { 'MCE_PORT': 5150, // port MCE Controller is listening on 'WEB_PORT': 6224, // port for webserver and websocket 'STATIC_DIR': 'MouseHelper', // subdirectory name for static web files 'RETRY_INTERVAL': 2, // interval (in s) to retry connecting to MCE Controller 'DEBUG': false, // turn on debugging output }; var VERSION = "2023-05-15" console.log('Starting MouseHelper interface (version: ' + VERSION + ')...'); // pull in required modules var express = require('express'); var net = require('net'); var webSocket = require('ws'); // webserver variables var app; var webserver; // websocket server var wss; // MCE Controller client connection var mceClient; var mceClientConnecting; // for drag and drop button state, cleaner to maintain here than on webpage var dragging = false; // set up webserver and websocket server var app = express(); app.use(express.static(config.STATIC_DIR)); // serve static pages from this directory var webserver = app.listen(config.WEB_PORT); var wss = new webSocket.Server({ server: webserver }); // create socket for connecting to MCE Controller var mceClient = new net.Socket(); // handlers for connecting to MCE Controller function mceClientConnected() { // check to prevent multiple connection messages on retries if (mceClientConnecting) { console.log('Socket connection established with MCE Controller...'); mceClientConnecting = false; } } function mceClientTryConnect() { // attempting to connect... mceClientConnecting = true; // only provide the connection listener on the first try, not retries mceClient.connect(config.MCE_PORT, '127.0.0.1', mceClientConnected); } // handle retrying to connect to MCE Controller (in case it isn't up yet or we get dropped) mceClient.on('error', function(e) { console.log('Unable to connect to MCE Controller, will try again in ' + config.RETRY_INTERVAL + ' seconds...'); setTimeout(mceClientTryConnect, config.RETRY_INTERVAL * 1000); }); // connect to MCE Controller mceClientTryConnect(); // handle websocket connection inputs wss.on('connection', function(ws) { console.log('WebSocket connection established with web interface...'); ws.on('message', function(cmdstr) { var cmdArray; // parsed array of commands/data var cmd; // extracted command name var cmdData; // extracted command data (optional) if (config.DEBUG) { console.log('received: %s', cmdstr); } // parse command string cmdArray = cmdstr.toString().split(':'); cmd = cmdArray[0] if (cmdArray.length > 1) { cmdData = cmdArray[1]; } switch (cmd) { // handle mouse button clicks; if we're already dragging, // turn off drag rather than register button click case 'lefttap': if (dragging) { mceClient.write('mouse:lbu\r\n'); dragging = false; } else { mceClient.write('mouse:lbc\r\n'); } break; case 'leftdoubletap': if (dragging) { mceClient.write('mouse:lbu\r\n'); dragging = false; } else { mceClient.write('mouse:lbdc\r\n'); } break; case 'righttap': if (dragging) { mceClient.write('mouse:lbu\r\n'); dragging = false; } else { mceClient.write('mouse:rbc\r\n'); } break; case 'rightdoubletap': if (dragging) { mceClient.write('mouse:lbu\r\n'); dragging = false; } else { mceClient.write('mouse:rbdc\r\n'); } break; // handle button presses (for easier drag and drop) case 'press': if (dragging) { // already dragging, so turn it off mceClient.write('mouse:lbu\r\n'); dragging = false; } else { dragging = true; mceClient.write('mouse:lbd\r\n'); } break; // handle mouse movement case 'mousemove': mceClient.write('mouse:mm,' + cmdData + '\r\n'); break; // handle scrolling case 'scroll': mceClient.write('mouse:vs,' + cmdData + '\r\n'); break; default: console.log('ERROR: unrecognized command: "' + cmd + '", ignoring...') } // done with that command }); // done with that message });