Skip to content

Commit

Permalink
Merge pull request #41415 from zedtux/features/actioncable/token
Browse files Browse the repository at this point in the history
Allows passing sub protocols with ActionCable
  • Loading branch information
rafaelfranca committed Jan 18, 2023
2 parents 6d4f9a8 + 6edb465 commit 847cc9f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
9 changes: 7 additions & 2 deletions actioncable/app/assets/javascripts/action_cable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions actioncable/app/assets/javascripts/actioncable.esm.js
Expand Up @@ -157,11 +157,12 @@ class Connection {
logger.log(`Attempted to open WebSocket, but existing socket is ${this.getState()}`);
return false;
} else {
logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${protocols}`);
const socketProtocols = [ ...protocols, ...this.consumer.subprotocols || [] ];
logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${socketProtocols}`);
if (this.webSocket) {
this.uninstallEventHandlers();
}
this.webSocket = new adapters.WebSocket(this.consumer.url, protocols);
this.webSocket = new adapters.WebSocket(this.consumer.url, socketProtocols);
this.installEventHandlers();
this.monitor.start();
return true;
Expand Down Expand Up @@ -456,6 +457,7 @@ class Consumer {
this._url = url;
this.subscriptions = new Subscriptions(this);
this.connection = new Connection(this);
this.subprotocols = [];
}
get url() {
return createWebSocketURL(this._url);
Expand All @@ -476,6 +478,9 @@ class Consumer {
return this.connection.open();
}
}
addSubProtocol(subprotocol) {
this.subprotocols = [ ...this.subprotocols, subprotocol ];
}
}

function createWebSocketURL(url) {
Expand Down
9 changes: 7 additions & 2 deletions actioncable/app/assets/javascripts/actioncable.js
Expand Up @@ -151,11 +151,12 @@
logger.log(`Attempted to open WebSocket, but existing socket is ${this.getState()}`);
return false;
} else {
logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${protocols}`);
const socketProtocols = [ ...protocols, ...this.consumer.subprotocols || [] ];
logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${socketProtocols}`);
if (this.webSocket) {
this.uninstallEventHandlers();
}
this.webSocket = new adapters.WebSocket(this.consumer.url, protocols);
this.webSocket = new adapters.WebSocket(this.consumer.url, socketProtocols);
this.installEventHandlers();
this.monitor.start();
return true;
Expand Down Expand Up @@ -443,6 +444,7 @@
this._url = url;
this.subscriptions = new Subscriptions(this);
this.connection = new Connection(this);
this.subprotocols = [];
}
get url() {
return createWebSocketURL(this._url);
Expand All @@ -463,6 +465,9 @@
return this.connection.open();
}
}
addSubProtocol(subprotocol) {
this.subprotocols = [ ...this.subprotocols, subprotocol ];
}
}
function createWebSocketURL(url) {
if (typeof url === "function") {
Expand Down
5 changes: 3 additions & 2 deletions actioncable/app/javascript/action_cable/connection.js
Expand Up @@ -33,9 +33,10 @@ class Connection {
logger.log(`Attempted to open WebSocket, but existing socket is ${this.getState()}`)
return false
} else {
logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${protocols}`)
const socketProtocols = [...protocols, ...this.consumer.subprotocols || []]
logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${socketProtocols}`)
if (this.webSocket) { this.uninstallEventHandlers() }
this.webSocket = new adapters.WebSocket(this.consumer.url, protocols)
this.webSocket = new adapters.WebSocket(this.consumer.url, socketProtocols)
this.installEventHandlers()
this.monitor.start()
return true
Expand Down
5 changes: 5 additions & 0 deletions actioncable/app/javascript/action_cable/consumer.js
Expand Up @@ -32,6 +32,7 @@ export default class Consumer {
this._url = url
this.subscriptions = new Subscriptions(this)
this.connection = new Connection(this)
this.subprotocols = []
}

get url() {
Expand All @@ -55,6 +56,10 @@ export default class Consumer {
return this.connection.open()
}
}

addSubProtocol(subprotocol) {
this.subprotocols = [...this.subprotocols, subprotocol]
}
}

export function createWebSocketURL(url) {
Expand Down
Expand Up @@ -20,6 +20,8 @@ export default function(name, options, callback) {
const connection = consumer.connection
const monitor = connection.monitor

if ("subprotocols" in options) consumer.addSubProtocol(options.subprotocols)

server.on("connection", function() {
const clients = server.clients()
assert.equal(clients.length, 1)
Expand Down
8 changes: 8 additions & 0 deletions actioncable/test/javascript/src/unit/consumer_test.js
Expand Up @@ -16,4 +16,12 @@ module("ActionCable.Consumer", () => {
client.addEventListener("close", done)
consumer.disconnect()
})

consumerTest("#addSubProtocol", {subprotocols: "some subprotocol"}, ({consumer, server, assert, done}) => {
server.on("connection", () => {
assert.equal(consumer.subprotocols.length, 1)
assert.equal(consumer.subprotocols[0], "some subprotocol")
done()
})
})
})

0 comments on commit 847cc9f

Please sign in to comment.