Git Product home page Git Product logo

cherry's Introduction

cherry-pit

cherry-pitπŸ’ is a light-weight, scalable, distributed game server engine.

δΈ­ζ–‡ζ–‡ζ‘£

Installation

npm install cherry-pit -g

Usage

Initialization

cherry init

Install dependencies

cd cherry
npm install

Start server examples

npm run cherry-connector
npm run cherry-chat
npm run cherry-pk

same as above:

node lib/connector.js 8080
node app.js chat
node app.js pk

or start multi process:

node lib/connector.js 8080
node lib/connector.js 8081
node app.js chat
node app.js chat
node app.js pk
node app.js pk

The same type of process will automatically achieve load balancing, you can also use pm2 for process management.

Start client examples

node test/helper/wsClient2.js
node test/helper/wsClient1.js

Configuration

config/dev/config.json, busOptions configuration reference here, add your logical server type to gameServerTypes.

{
  "busOptions": {
    "driver": "ioredis",
    "layout": "",
    "redis": [
      "redis://127.0.0.1:6379"
    ]
  },
  "gameServerTypes": [
    "chat",
    "pk"
  ]
}

Add the service directory under service/.

β”œβ”€β”€ service
β”‚Β Β  β”œβ”€β”€ chat
β”‚Β Β  β”‚Β Β  └── chat.js
β”‚Β Β  └── pk
β”‚Β Β      └── pk.js

service/chat/chat.js, in the specific service directory to provide the interface.

function talk (gameServer, args) {
  return args;
}

function talk2all (gameServer, args) {
  gameServer.broadcast(args[0]);
  return;
}

function talk2multi (gameServer, args) {
  gameServer.multicast(...args);
  return;
}

module.exports = {
  talk,
  talk2all,
  talk2multi
};

app.js is the entrance, import interface. The return value can be either promise or primitive type.

const chat = require('./service/chat/chat');
const pk = require('./service/pk/pk');

let result;
try {
  switch (serverType) {
    case 'chat':
      result = chat[func](gameServer, args);
      break;
    case 'pk':
      result = pk[func](gameServer, args);
      break;
  }
}
catch (err) {
  console.error(err);
}

API

gameServer

Event: 'message'

  • msg {String}

gameServer.send(userId, msg)

  • userId {String}
  • msg {Number|String|Array|Object|Promise}

gameServer.multicast(userIds, msg)

  • userIds {Array}
  • msg {Number|String|Array|Object|Promise}

gameServer.broadcast(msg)

  • msg {Number|String|Array|Object|Promise}

License

MIT License

Copyright (c) 2017 dongdonggun

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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.