NetClient.js 815 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
const DEFAULT_GAME_ROOM = "star_gazer_lab";

class NetClient {

  connect(url) {
    this.client = new Colyseus.Client('ws://localhost:2567');
  }

  joinGame() {
    this.client.joinOrCreate(DEFAULT_GAME_ROOM).then(this.onConnect.bind(this)).catch(
      this.onError.bind(this));
  }

  onConnect(room) {
    console.log(room.sessionId, "joined", room.name);
    room.onStateChange(this.onUpdate);
    room.onMessage(this.onMessage);
    room.onError(this.onError);
    room.onLeave(this.onLeave);
  }

  onUpdate(state) {
    console.log("received new state from server");
    console.log(state);
  }

  onMessage(message) {
    console.log("Received new message from server")
  }

  onError(error) {
    console.log(error);
  }

  onLeave() {
    console.log("Client left room")
  }
}

export default NetClient;