GithubHelp home page GithubHelp logo

udamir / magx Goto Github PK

View Code? Open in Web Editor NEW
39.0 3.0 5.0 1.62 MB

Multiplayer game server framework

License: MIT License

TypeScript 79.12% JavaScript 1.06% HTML 19.81%
magx websocket multiplayer-game-server multiplayer server game-server nodejs

magx's Introduction

MagX

npm npm npm type definitions GitHub

Multiplayer Game Server Framework for Node.js

What is MagX?

Magx is a Multiplayer Game Server Framework for Node.js: server-authoritative multiplayer approach is supported as well as relayed multiplayer (also known as client-authoritative).

In server-authoritative multiplayer approach room state maintained on the server and clients are just visual representations of the current game state. Each client can send messages to change room state, these messages can be validated by server and state changes broadcast to room clients. This enables you to build:

  1. Asynchronous real-time authoritiative multiplayer: Fast paced realtime multiplayer. Messages are sent to the server, server calculates changes to the environment and players and data is broadcasted to relevant peers. This typically requires a high tick-rate for the gameplay to feel responsive.

  2. Active turn-based multiplayer: Like with Stormbound or Clash Royale mobile games where two or more players are connected and are playing a quick turn-based match. Players are expected to respond to turns immediately. The server receives input, validates them and broadcast to players. The expected tick-rate is quite low as rate of message sent and received is low.

To create Authoritative Multiplayer server you need Flexible State Mangement with change tracking functionality. MosX is default state managment engine, but you have the freedom and flexibility to choose state managment engine without limitations.

In relayed multiplayer approach each client is act as the host of reconcile state changes between peers and perform arbitration on ambiguous or malicious messages sent from bad clients. So each client sends all state changes to server and server broadcasted them to otherr clients without inspection. This approach can be very useful for many types of gameplay but may not suitable for gameplay which depends on central state managed by the game server.

Summary

MagX provides to you:

  • WebSocket-based communication
  • Simple API in the server-side and client-side.
  • Automatic state synchronization between server and client.
  • Scale vertically or horizontally
  • Fully customizable

Getting started

From examples project

The easiest way to try out MagX is using the magx-example project:

Installation

git clone https://github.com/udamir/magx.git
cd magx/examples
npm install

To run the MagX server, run npm start

Build basic Chat server from scratch

  1. Install magx package:
npm install --save magx
  1. Create a simple chat room handler (chatRoom.ts):
import { Room, Client } from "magx"

export class ChatRoom extends Room {

  public onMessage(client: Client, type: string, data: any) {
    console.log("ChatRoom received message from", client.id, ":", data)
    this.broadcast("messages", `(${client.id}) ${data}`)
  }
  
  public onJoin(client: Client) {
    this.broadcast("messages", `${ client.id } joined.`)
  }
  
  public onLeave(client: Client) {
    this.broadcast("messages", `${ client.id } left.`)
  }
  
  public onClose() {
    console.log("ChatRoom closed!")
  }
}
  1. Create MagX server and define chatRoom (index.ts):
import * as http from "http"
import { Server } from "magx"
import { ChatRoom } from "./chatRoom"

const server = http.createServer()

const magx = new Server(server)
magx.define("chat", ChatRoom)

const port = process.env.PORT || 3001
server.listen(port, () => {
  console.log(`Magx server started on http://localhost:${port}`)
})

Chat server is ready!

  1. Create basic index.html page and attach magx-client:
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width" />

    <!-- magx client -->
    <script type="text/javascript" src="/magx"></script>

  </head>
  <body>
    <strong>Messages</strong><br>

    <form id="form">
      <input type="text" id="input" value="" autofocus/>
      <input type="submit" value="send" />
    </form>

    <div id="messages"></div>

    <script>
      // add js code here
    </script>
  </body>
</html>
  1. Connect to MagX server, authenticate and join ChatRoom:
const { host, port, protocol } = window.document.location
var client = new MagX.Client({ address: host.replace(/:.*/, ''), port, secure: protocol === "https:" })

client.authenticate()
  .then(() => client.getRooms("chat"))
  .then(rooms => rooms.length ? client.joinRoom(rooms[0].id) : client.createRoom("chat"))
  .then(room => {
    console.log("joined")
    
    // listen to messages coming from the server
    room.onMessage("messages", (message) => {
      var p = document.createElement("p");
      p.innerText = message;
      document.querySelector("#messages").appendChild(p);
    })
    
    // send message to room on submit
    document.querySelector("#form").onsubmit = function(e) {
      e.preventDefault();
      var input = document.querySelector("#input");
      console.log("input:", input.value);
      
      // send data to room
      room.send("message", input.value);
      
      // clear input
      input.value = "";
    }
  })

Simple chat is ready! You can open several tabs, send and recieve messags.

Documentation

soon...

Status and roadmap

Current project status

When will Magx v1.0 release

License

FOSSA Status

magx's People

Contributors

fossabot avatar udamir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

magx's Issues

Multiple State Objects

I see in your code you create and serial a state. Can you patch track multiple states?

e.g.

createStates () => { Player, House }

createPatchTracker(state: State) {
    // create state change tracker
    return Mosx.createTracker(state, { serializer: SchemaSerializer })
...

or do I need to manage the house state from within the player

class PlayerState {
 this.player  = new Player
this.house = new House

}

400 Bad Request

So I'm using cors and https to run the server( port 2567) while I run the game on a different server( port 3000).

While the logs show I'm able to create a room

Process 70: Get avalible rooms
Find process for room:
Current process load: {}
All cluster pids []
Process 70 requested: Create room pullowar

The client side seems to return a

Request URL: https://localhost:2567/magx/rooms
Request Method: POST
Status Code: 400 Bad Request
Remote Address: [2803:1500:e00:f22f:ba27:ebff:fede:91d0]:2567
Referrer Policy: strict-origin-when-cross-origin

with the following payload.

{"name":"pullowar","options":{}}

Did I need to set additional options for the room to be initialized?

connection timeout

So I'm using the latest magx with the following setup

 gameServer = new Server(server),
gameServer.define('pullowar', PullOWarRoom);
server.listen(port, () => console.log(`Listening on http://localhost:${ port }`));

the PullOWarRoom

class PullOWarRoom extends Room {
  constructor(options) {
   this.maxClients = options.maxClients || 2;
  }

   createState(){
    // create state
    return new State()
  }

  createPatchTracker(state) {
    // create state change tracker
    return mosx.Mosx.createTracker(state, { serializer: mosx.SchemaSerializer })
  }

   onCreate(params ) {
   	console.log('on create', params)
  }


  onLeave() {
  	this.state.removePlayer();
  	this.state.resetGame();
  }

  onJoin() {
    if( this.state.game.players == this.maxClients && this.state.game.state === false )
    {
      this.state.setGameState( GAME_READY)
      this.state.startNextRound()
    } else {
      this.state.addPlayer()
    }
  }

  onClose() {
    delete this.state
  }
}

And I connect to the server using

// http://localhost:2567/magx"
client.authenticate().then((d) => {
                               	console.log(d)
                               return (ROOM_ID ? client.joinRoom(ROOM_ID) : client.createRoom('pullowar'))
    }).then(room => {
    	.....

I'm not sure how auth works but the but i get a "timeout" trying to connect via "http://localhost:2567/magx/auth"

I'm also using the magx-monitor but in accessing the url "http://localhost:2567/magx/monitor" the page loads but I get the error

net::ERR_SSL_PROTOCOL_ERROR
monitor:61 Uncaught ReferenceError: MagX is not defined
at monitor:61

I'm assuming the first error is due to not setting up auth? Can I disable auth at least on localhost?
The second maybe due to not using https? Can I also disable this at least on localhost?

Is this productions ready?

Hi,

I was looking for a simpler alternative to Colyseus and came across your package which seems to have what I am looking for...Is it production ready?

State management

I'm looking at your state management logic. Based on what I'm seeing can I assume the following is how it works:-

  1. Create the state object
  2. Create a tracker with Serializer
  3. Use onSnapshot client side to update/ retrieve state?

But I have the following questions

  • Is it that the "patch tracker" are instantiated on game connection ?
  • Is the tracker listeners created on instantiation ?

Also

  • do I "need" to use MosX state management?
  • If I use my own...do I need to serialize it using the serializer?
  • Do I also need to follow the same patterns for my state management?

Examples would be appreciated. Thanks in advance.

hi

A project with potential, I use first

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.