Platforms
This client works on these platforms:
- Major browsers environments (Electron, Chrome, Firefox, Safari, Opera, etc)
- React Native (with some caveats)
- Cocos Creator
Usage
Connecting to server:
; ;
Joining to a room:
;room.onJoin.add;
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/removed from the room:
room.listen"entities/:id",;
Listening to entity attributes being added/replaced/removed:
room.listen"entities/:id/:attribute",;
Other room events
Room state has been updated:
room.onStateChange.add
Message broadcasted from server or directly to this client:
room.onMessage.add;
Server error occurred:
room.onError.add;
The client left the room:
room.onLeave.add;
Useful links
React Native compatibility
This client works with React Native. You need to install some aditional
dependencies for compatibility and assign window.localStorage
to
AsyncStorage
.
- Install
react-native-browser-polyfill
(npm install react-native-browser-polyfill
) - Install and follow
rn-nodeify
installation instructions (npm install rn-nodeify
)
// App.js
import 'react-native-browser-polyfill';
import './shim';
import { AsyncStorage } from 'react-native';
window.localStorage = AsyncStorage;
Another caveat is that you can only join rooms after the first connection open.
var client = new Colyseus.Client('ws://localhost:2657');
client.onOpen.add(() => {
let room = client.join("your_room");
})
License
MIT