mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 09:31:20 -04:00
modularize code, add dummy version of roverd
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
port: process.env.PORT || 8080,
|
||||
staticDir: path.join(__dirname, '..', '..', 'public'),
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
const path = require('path');
|
||||
const http = require('http');
|
||||
const express = require('express');
|
||||
const morgan = require('morgan');
|
||||
const config = require('./config');
|
||||
|
||||
const app = express();
|
||||
app.use(morgan('dev'));
|
||||
app.use(express.json());
|
||||
app.use(express.static(config.staticDir));
|
||||
|
||||
const httpServer = http.createServer(app);
|
||||
|
||||
module.exports = { app, httpServer };
|
||||
@@ -0,0 +1,8 @@
|
||||
const { Server: SocketIOServer } = require('socket.io');
|
||||
const { httpServer } = require('./http');
|
||||
|
||||
const io = new SocketIOServer(httpServer, {
|
||||
cors: { origin: '*' },
|
||||
});
|
||||
|
||||
module.exports = io;
|
||||
@@ -0,0 +1,9 @@
|
||||
function stamp(level, args) {
|
||||
return [new Date().toISOString(), `[${level}]`, ...args];
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
info: (...args) => console.log(...stamp('INFO', args)),
|
||||
warn: (...args) => console.warn(...stamp('WARN', args)),
|
||||
error: (...args) => console.error(...stamp('ERROR', args)),
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
const { WebSocketServer } = require('ws');
|
||||
const { httpServer } = require('./http');
|
||||
const logger = require('./logger');
|
||||
|
||||
const roverWSS = new WebSocketServer({ noServer: true });
|
||||
|
||||
httpServer.on('upgrade', (req, socket, head) => {
|
||||
if (req.url.startsWith('/rover')) {
|
||||
roverWSS.handleUpgrade(req, socket, head, (ws) => {
|
||||
roverWSS.emit('connection', ws, req);
|
||||
});
|
||||
} else {
|
||||
socket.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
roverWSS.on('connection', () => {
|
||||
logger.info('Rover websocket connected');
|
||||
});
|
||||
|
||||
module.exports = roverWSS;
|
||||
Reference in New Issue
Block a user