Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Prepare for single websocket connection #493

Merged
merged 3 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const util = {
defaultConfig: {
host: 'ile.learn.co',
port: 443,
path: 'v2/terminal',
path: 'environment',
learnCo: 'https://learn.co'
},

Expand Down
24 changes: 18 additions & 6 deletions lib/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class Terminal {
}

connect(token) {
this.socket = new AtomSocket('term', this.url());
this.socket = new AtomSocket('environment', this.url());

this.waitForSocket = new Promise(((resolve, reject) => {
this.socket.on('open', e => {
Expand All @@ -31,8 +31,19 @@ export default class Terminal {
resolve()
})

this.socket.on('message', (msg='') => {
var decoded = new Buffer(msg, 'base64').toString();
this.socket.on('message', (msg) => {
if (msg.slice(2,10) !== 'terminal') { return }

try {
var {terminal} = JSON.parse(msg)
} catch ({message}) {
console.error(`terminal parse error: ${message}`)
return
}

if (terminal == null) { return }

var decoded = new Buffer(terminal, 'base64').toString();
this.emit('message', decoded)
})

Expand Down Expand Up @@ -64,14 +75,15 @@ export default class Terminal {
}

send(msg) {
if (this.waitForSocket === null ) {
this.socket.send(msg)
var preparedMessage = JSON.stringify({terminal: msg})
if (this.waitForSocket != null ) {
this.socket.send(preparedMessage)
return
}

this.waitForSocket.then(() => {
this.waitForSocket = null;
this.socket.send(msg);
this.socket.send(preparedMessage);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"atom-material-syntax-light": "0.4.6",
"learn-ide-material-ui": "1.3.15",
"learn-ide-notifications": "0.66.5",
"learn-ide-tree": "1.0.23"
"learn-ide-tree": "1.0.24"
},
"devDependencies": {
"decompress": "4.0.0",
Expand Down