How to run Wade on a node.js server (headless)
Arkam

Thread title says it all, is it possible and how?

All 5 Comments
Gio

Hi Arkam and welcome to the forum

Sure it's possible, but obviously you won't get any of the client-side aspects of Wade - no rendering, sprite management, etc.

However it's potentially useful to run Wade on a node server, in case you have a multiplayer game and want to reuse the same codebase that you've used for your single-player game as part of the server logic. You may want to use Wade for pathfinding, serialization of Scene Objects, and other things that do not require a screen / window to display stuff.

Wade itself relies on a couple (literally just 2) things that exist in the DOM but won't exist in a node.js process. As long as you do not use those things explicitly, you can just redefine them at the top of your node app / module before including wade:

Image = function() {};
window = {};
require('./wade');

That's all you have to do. Do not call wade.init(), as that will try to create the default set of procedural sprites (among other things), initialize audio, etc. and all those things will fail on a node server. But feel free to call wade.findPath, wade.worldPositionToScreen, and generally any wade functions that do not directly do any Sprite drawing.

Let me know if you encounter any problems with this, always happy to help.

prajwal.be.21

Hi Akram,

I use Node.js for my full stack web application development and also for running WADE games (Instead of Apache/ PHP).

So with your folder structure setup like Wade's default structure you can use the following code for your severside app.js (Node.js) to host a WADE game.

const path = require('path');
const express = require('express');

const app = express();
const port = process.env.PORT || 4200;
const publicPath = path.join(__dirname, '../public');
app.use('/assets', express.static(publicPath));

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, '../public/index.html'));
});

app.listen(port, () => {
  console.log(`WADE game running on  localhost:${port}`);
});


 And here's the folder structure I use post downloading the source code and to run it through Node.js

Node Modules/
Public/
Server/
Package.JSON

Get all your WADE assets in Public, Note instead of a views folder with pages, built with templating engines like ejs or handlebars or jade, I simply serve up the .html file itself. You can also use a template engine and set it up in views folder but I find this redundant.

Hope this helps. Please do ask if something is not clear.
 

prajwal.be.21

Hey Akram, sorry I missed the headless server app part in question and proceeded to answer how to serve a wade game on node server. Still might be useful for someone who wants to do that.

Arkam

Well thank you both! I needed both answers :D

I want to serve the game file + I also want some server logic that uses wade.

I am working on a multiplayer game, that you can play single player too. So when you play single player you create an instance of my Server class locally. But then I want to reuse the same Server class for remote games, so I want to run it on node.js. And of course I need to send all the client files to my players too from my node.js app.

Your answers help me a lot

prajwal.be.21

Glad I could help Arkam :)

PS: So sorry I thought your username was Akram all this while :P Lol

Post a reply
Add Attachment
Submit Reply
Login to Reply