Skip to content

Commit

Permalink
Fix Console and add better retrying to the api
Browse files Browse the repository at this point in the history
  • Loading branch information
ricochet1k committed Sep 5, 2017
1 parent d68a83e commit b788b88
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 32 deletions.
57 changes: 31 additions & 26 deletions src/components/Console.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ export default {
data() {
return {
lines: [],
connected: false,
}
},
watch: {
'api': function(api, oldApi) {
this.disconnect(oldApi);
this.connect(api);
}
},
},
created() {
Expand All @@ -40,39 +41,43 @@ export default {
},
methods: {
reconnect(api) {
},
connect(api) {
if (api && api.on)
api.on("message", this.onMessage);
if (api && api.socketAuth && api.subscribe){
console.log('Console.connect', `user:${api.user._id}/console`, api)
api.subscribe(`user:${api.user._id}/console`, this.onMessage);
this.connected = true;
}
},
disconnect(api) {
if (api && api.off)
api.off("message", this.onMessage);
this.connected = false;
if (api && api.unsubscribe)
api.unsubscribe(`user:${api.user._id}/console`, this.onMessage);
},
onMessage(msg) {
if (msg[0].match(/\/console$/)) {
const con = this.$refs && this.$refs.consoleWrapper;
let isAtBottom = false;
if (con) {
isAtBottom = con.scrollHeight - con.clientHeight <= con.scrollTop + 1;
}
if (msg[1].messages && msg[1].messages.log) {
for (let m of msg[1].messages.log) {
this.lines.push(m);
}
} else if (msg[1].error) {
this.lines.push(msg[1].error);
} else {
console.log('wierd console', msg[1]);
onMessage(key, msg) {
const con = this.$refs && this.$refs.consoleWrapper;
let isAtBottom = false;
if (con) {
isAtBottom = con.scrollHeight - con.clientHeight <= con.scrollTop + 1;
}
if (msg.messages && msg.messages.log) {
for (let m of msg.messages.log) {
this.lines.push(m);
}
if (isAtBottom)
this.$nextTick(() => {
con.scrollTop = con.scrollHeight;
})
} else if (msg.error) {
this.lines.push(msg.error);
} else {
console.log('wierd console', msg);
}
if (isAtBottom)
this.$nextTick(() => {
con.scrollTop = con.scrollHeight;
})
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/pages/RoomView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ export default {
computed: {
api() {
return eventBus.api;
if (eventBus.api && eventBus.api.socketAuth)
return eventBus.api;
},
client() {
return eventBus.client;
if (eventBus.client && eventBus.client.me)
return eventBus.client;
},
money() {
Expand Down
25 changes: 21 additions & 4 deletions src/scripts/screepsAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class ScreepsAPI extends EventEmitter {
this.user = null;
this.ws = null;
this.connected = false;
this.socketAuth = false;
}

async rawreq(method, path, body) {
Expand All @@ -49,14 +50,25 @@ export class ScreepsAPI extends EventEmitter {
return await this.req(method, path, body);
}

let res = await this.rawreq(method, path, body);
let res;

try {
res = await this.rawreq(method, path, body);
} catch (e) {
console.log('req err:', e);
if (path.match(/auth/))
throw e;
await this.getToken();
res = await this.rawreq(method, path, body);
}

if (res.status == 200) {
if (res.headers['x-token'])
this.token = res.headers['x-token']
}
if (res.status == 401) {
return this.getToken().then(() => this.req(method, path, body));
await this.getToken();
return await this.rawreq(method, path, body);
}

return res;
Expand Down Expand Up @@ -85,6 +97,7 @@ export class ScreepsAPI extends EventEmitter {
this.ws.close();
this.ws = null;
this.connected = false;
this.socketAuth = false;
}
}
auth(email, password) {
Expand Down Expand Up @@ -159,8 +172,12 @@ export class ScreepsAPI extends EventEmitter {
subs[i](msg[0], msg[1], msg);
}

} else
} else {
if (msg.match(/^auth ok/)) {
this.socketAuth = true;
}
this.emit('message', msg);
}

// if (msg[0].match(/console/))
// this.emit('console', msg)
Expand Down Expand Up @@ -215,7 +232,7 @@ export class ScreepsAPI extends EventEmitter {
console.log("cannot send", data);
return;
}
this.ws.send(...data)
this.ws.send([...data])
}

get memory () {
Expand Down

0 comments on commit b788b88

Please sign in to comment.