import Logger from "../utils/Logger.js"; import Observable from "../utils/Observable.js"; const DEFAULT_GAME_ROOM = "star_gazer_lab"; class NetClient extends Observable { constructor() { super(); } connect(url) { this.client = new Colyseus.Client(url); } joinGame() { this.client.joinOrCreate(DEFAULT_GAME_ROOM).then(this.onConnect.bind(this)) .catch( this.onError.bind(this)); } sendGazeData(gazeData) { this.room.send({ type: "gaze", data: gazeData }); } onConnect(room) { Logger.log(`Player ${room.sessionId} joined ${room.name}`, "NetClient"); this.room = room; this.room.onStateChange(this.onUpdate.bind(this)); this.room.onMessage(this.onMessage.bind(this)); this.room.onError(this.onError.bind(this)); this.room.onLeave(this.onLeave.bind(this)); this.notifyAll({ type: "connect", ownID: this.room.sessionId }); } onUpdate(state) { this.notifyAll({ type: "stateupdate", state: state }); } onMessage(message) { Logger.log("Received new message from server", "NetClient"); } onError(error) { console.log(error); } onLeave() { Logger.log("Local client left room", "NetClient"); } } export default NetClient;