class Network{ constructor(adress){ this.adress = adress; this.connected = false; } message(data){ switch(data.type){ case 'connect': console.log("connected"); break; case 'update': console.log("updated"); break; } } init(){ this.socket.send(JSON.stringify({type: "connect"})); } connect(){ this.socket = new WebSocket(this.adress); this.socket.addEventListener('open', (e)=>{ //connected to server this.connected = true; this.init(); }); this.socket.addEventListener('message', (e)=>{ this.message(JSON.parse(e.data)); }) } }