Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable WebSocket Affinity in the device agent #213

Merged
merged 5 commits into from
Jan 17, 2024
Merged
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
27 changes: 25 additions & 2 deletions lib/editor/tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down