Allows adding more protocols to the WebSocket sub protocols

This commit is contained in:
Guillaume Hain 2021-02-11 22:39:35 +01:00
parent cf2be23577
commit 6edb465bcf
No known key found for this signature in database
GPG Key ID: D2B2A8B99474CF36
7 changed files with 39 additions and 8 deletions

View File

@ -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;
@ -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);
@ -463,6 +465,9 @@
return this.connection.open();
}
}
addSubProtocol(subprotocol) {
this.subprotocols = [ ...this.subprotocols, subprotocol ];
}
}
function createWebSocketURL(url) {
if (typeof url === "function") {

View File

@ -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;
@ -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);
@ -476,6 +478,9 @@ class Consumer {
return this.connection.open();
}
}
addSubProtocol(subprotocol) {
this.subprotocols = [ ...this.subprotocols, subprotocol ];
}
}
function createWebSocketURL(url) {

View File

@ -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;
@ -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);
@ -463,6 +465,9 @@
return this.connection.open();
}
}
addSubProtocol(subprotocol) {
this.subprotocols = [ ...this.subprotocols, subprotocol ];
}
}
function createWebSocketURL(url) {
if (typeof url === "function") {

View File

@ -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

View File

@ -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() {
@ -55,6 +56,10 @@ export default class Consumer {
return this.connection.open()
}
}
addSubProtocol(subprotocol) {
this.subprotocols = [...this.subprotocols, subprotocol]
}
}
export function createWebSocketURL(url) {

View File

@ -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)

View File

@ -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()
})
})
})