UNPKG

meta-client

Version:
131 lines (91 loc) 2.64 kB
/* * Created by cheesyeyes on 6/21/16. */ /* * this basicaly handles all communication with the server */ import io from 'socket.io-client'; import {log, Events} from './Utilities'; const NAME = 'META Space'; class Space extends Events{ constructor(url){ super(); let that = this; this.name = 'Space'; this._on=false; this.data = {}; this.com = io.connect(url); this.on('get', (data) => { if(data==undefined) data = {}; data.space = that.data._id; that.com.emit('ask', data); log(that.name, 'send request for objects in space '+that.data._id); }) this.com.on('open', (data) => { that.go('open', data); log(that.name, 'received confirmation for open space'+JSON.stringify(data)); }); this.com.on('connected', (data) => { log(that.name, 'connected '+JSON.stringify(data)); that.go('connected', data); }) this.com.on('enter', (data) => { that.data = data; //TODO internaly! log(that.name, 'received confirmation for entering space '+JSON.stringify(that.data)); that.go('enter', data); }) //create this.com.on('create', (data) => { log(that.name, 'received object '+JSON.stringify(data)); that.go('create', data); }); //update this.com.on('update', (objectId, data) => { log(that.name, 'received update '); that.go('update', objectId, data); }) } set(url){ if(url!=undefined) this.server = url; } open(data){ if(data==undefined) data = {}; this.com.emit('open', data); log(this.name, 'send request to open space '+JSON.stringify(data)); } //send requests to server get(specific){ //get aviable spaces this.com.emit('get', specific); } enter(data){ //join existing space this.com.emit('enter', data); this.data._id = data._id; this._on=true; log(this.name, 'send request to join space '+JSON.stringify(data)); } //objects create(data){ //attach space id; data.space = this.data._id; //send this.com.emit('create', data); //log(this.name, 'create: '+JSON.stringify(data)); } update(objectId, data){ //attach space id; data.space = this.data._id; this.com.emit('update', objectId, data); //log(this.name, 'send update for '+objectId+'-'+JSON.stringify(data)+'in space '+data.space); } //messages message(msg){ let data = {}; if(this.data._id) data.space=this.data._id; data.text=msg; this.com.emit('message', data); log(this.name, 'send message '+JSON.stringify(data)) } } export default Space;