diff --git a/lib/editor/tunnel.js b/lib/editor/tunnel.js index 3e48d48..464eeaa 100644 --- a/lib/editor/tunnel.js +++ b/lib/editor/tunnel.js @@ -22,6 +22,7 @@ class EditorTunnel { this.port = config.port this.config = config this.options = options || {} + this.affinity = undefined // How long to wait before attempting to reconnect. Start at 500ms - back // off if connect fails @@ -57,9 +58,31 @@ class EditorTunnel { info(`Connecting editor tunnel to ${forgeWSEndpoint}`) // * Enable Device Editor (Step 8) - (device->forge:WS) Initiate WS connection (with token) + const headers = { + 'x-access-token': this.options.token + } + if (this.affinity) { + headers.cookie = `FFSESSION=${this.affinity}` + } const socket = newWsConnection(forgeWSEndpoint, { - headers: { - 'x-access-token': this.options.token + headers + }) + socket.on('upgrade', (evt) => { + if (evt.headers && evt.headers['set-cookie']) { + const cookies = evt.headers['set-cookie'] + if (Array.isArray(cookies)) { + cookies.forEach(cookie => { + const parts = cookie.split(';')[0].split(['=']) + if (parts === 'FFSESSION') { + this.affinity = parts[1] + } + }) + } else { + const parts = cookies.split(';')[0].split(['=']) + if (parts === 'FFSESSION') { + this.affinity = parts[1] + } + } } }) socket.onopen = (evt) => {