A "multiplayer" HTML5 <canvas> to which people can connect and make changes over time.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
2.4 KiB

// multiplayer-canvas.js
// Copyright (C) 2022 Rob Colbert @[email protected]
// License: Apache-2.0
'use strict';
import 'dotenv/config'; // reads .env into process.env
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url)); // jshint ignore:line
import { createRequire } from 'module';
const require = createRequire(import.meta.url); // jshint ignore:line
import express from 'express';
import winston from 'winston';
import expressWinston from 'express-winston';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import WEBPACK_CONFIG from './webpack.config.js';
const APP_CONFIG = {
pkg: require('./package.json'),
niceGameSdk: require('./node_modules/dtp-nice-game/package.json'),
};
class MultiplayerCanvasApp {
constructor ( ) {
this.app = express();
this.app.locals.config = APP_CONFIG;
this.app.locals.pkg = APP_CONFIG.pkg;
this.app.locals.niceGameSdk = APP_CONFIG.niceGameSdk;
this.app.set('view engine', 'pug');
this.app.set('views', path.join(__dirname, 'app', 'views'));
this.app.use(expressWinston.logger({
transports: [
new winston.transports.Console(),
],
format: winston.format.combine(
winston.format.colorize(),
// winston.format.json(),
),
meta: false,
msg: "HTTP ",
expressFormat: true,
colorize: false,
ignoreRoute: (/*req, res*/) => { return false; },
}));
this.app.use('/dist/assets', express.static(path.join(__dirname, 'app', 'assets')));
/*
* Webpack integration
*/
this.compiler = webpack(WEBPACK_CONFIG);
/*
* Webpack dev server middleware
*/
this.webpackDevMiddleware = webpackDevMiddleware(this.compiler, {
publicPath: WEBPACK_CONFIG.output.publicPath,
});
this.app.use(this.webpackDevMiddleware);
/*
* Application routes
*/
this.app.get('/', this.getCanvasView.bind(this));
}
async start ( ) {
return new Promise((resolve, reject) => {
this.app.listen(3000, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
async getCanvasView (req, res) {
res.locals.appModuleUrl = '/dist/canvas-app.bundle.js';
res.render('multiplayer-canvas');
}
}
(async ( ) => {
const app = new MultiplayerCanvasApp();
await app.start();
})();