Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comment
  • views

Project: rpg

Frameworks, tooling and other drugs (II)



I wrote a first post recently describing how I started with Phaser and Webpack. That would be the Client side in my game idea of a #browser #multiplayer #RPG.Now let's see the needed Server side:

Node & Websockets
I decided to start with Node.js as it offers speed, scalability, a non-blocking API and continue with the Javascript stack. It has an easy and fast learning curve for anyone familiar to Javascipt and it's a very good and simple start. There are a lot of simple HTTP server examples made in Node but what we need for our game are Websockets.

A game server needs rapid request/response communication. The fastest way would be using UDP network protocol, but as UDP doesn't guarantee an order in responses it is needed a TCP. Websockets are the fastest TCP approach. Looking for a good library to work with Node and Websockets I found ws and I liked it.

See a very basic server example:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 1234 });
wss.on('connection', function connection(ws) {
  console.log('new client connected');
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  ws.send('text from server');

And a very basic browser client to complete the comunication:

wsc = new WebSocket('ws://localhost:1234');
wsc.onopen = () => console.log('connected to server');
wsc.onmessage = (message) => console.log('received: %s', message);
wsc.send('text from client');

Now you can see how easy is to evolve these basic examples in a simple chat or just for sending/receiving players positions. A good pair of hints for the simple server example would be:
1) push and keep client connections (players) in array data
2) create a broadcast function in order to send data for other clients


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!