Giter Club home page Giter Club logo

colyseus.js's Introduction

colyseus.js

Colyseus.js - Multiplayer Game Client for the Browser.

Join the chat at https://gitter.im/gamestdio/colyseus Build Status

JavaScript/TypeScript client for Colyseus Multiplayer Game Server.

The version 0.6.0 is still in beta. Expect some changes before the final release.

Usage

Connecting to server:

import * as Colyseus from "colyseus.js";

var client = new Colyseus.Client('ws://localhost:2657');

Joining to a room:

var room = client.join("room_name");
room.onJoin.add(function() {
    console.log(client.id, "joined", room.name);
});

Listening to room state change:

Here comes the most powerful feature of the client. You can listen to every state update in the server-side, and bind them into client-side functions.

The first parameter is the path of the variable you want to listen to. When you provide placeholders (such as :number, :id, :string) to the path, they will populate the function with the value found on it. See examples below.

Listening to entities being added in the room:

room.state.listen("entities/:id", "add", (entityId: string, value: any) => {
    console.log(`new entity ${entityId}`, value);
});

Listening to entity attributes being replaced:

room.state.listen("entities/:id/:attribute", "replace", (entityId: string, attribute: string, value: any) => {
    console.log(`entity ${entityId} changed attribute ${attribute} to ${value}`);
});

Listening to entities being removed:

room.state.listen("entities/:id", "remove", (entityId: string) => {
    console.log(`entity ${entityId} has been removed`);
});

Other room events

Room state has been updated:

room.onUpdate.add(function(state) {
  console.log(room.name, "has new state:", state)
})

Data coming from server directly to this client:

room.onData.add(function(data) {
  console.log(client.id, "received on", room.name, data)
});

Server error occurred:

room.onError.add(function() {
  console.log(client.id, "couldn't join", room.name)
});

The client left the room:

room.onLeave.add(function() {
  console.log(client.id, "left", room.name)
});

Useful links

License

MIT

colyseus.js's People

Contributors

endel avatar

Watchers

James Cloos avatar David Brotz avatar

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.