Skip to content

Commit 9804749

Browse files
Add support for websocket protocol with fallback (#303)
1 parent 3d1b9ad commit 9804749

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ module.exports = class IrcClient extends EventEmitter {
5454
ping_interval: 30,
5555
ping_timeout: 120,
5656
message_max_length: 350,
57-
transport: default_transport
57+
transport: default_transport,
58+
websocket_protocol: 'text.ircv3.net'
5859
};
5960

6061
const props = Object.keys(defaults);

src/transports/websocket.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ module.exports = class Connection extends EventEmitter {
1818

1919
this.encoding = 'utf8';
2020
this.incoming_buffer = '';
21+
22+
this.protocol_fallback = false;
23+
this.protocol = options.websocket_protocol;
2124
}
2225

2326
isConnected() {
@@ -55,13 +58,13 @@ module.exports = class Connection extends EventEmitter {
5558
ws_addr += options.port ? ':' + options.port : '';
5659
ws_addr += options.path ? options.path : '';
5760

58-
socket = this.socket = new WebSocket(ws_addr);
61+
socket = this.socket = new WebSocket(ws_addr, this.protocol);
5962

6063
socket.onopen = function() {
6164
that.onSocketFullyConnected();
6265
};
63-
socket.onclose = function() {
64-
that.onSocketClose();
66+
socket.onclose = function(event) {
67+
that.onSocketClose(event);
6568
};
6669
socket.onmessage = function(event) {
6770
that.onSocketMessage(event.data);
@@ -80,13 +83,33 @@ module.exports = class Connection extends EventEmitter {
8083
this.emit('open');
8184
}
8285

83-
onSocketClose() {
86+
onSocketClose(event) {
87+
const possible_protocol_error = !this.connected && event.code === 1006;
88+
if (possible_protocol_error && !this.protocol_fallback && this.protocol !== undefined) {
89+
// First connection attempt failed possibly due to mismatched protocol,
90+
// retry the connection with undefined protocol
91+
// After this attempt, normal reconnect functions apply which will
92+
// reconstruct this websocket, resetting these variables
93+
this.debugOut('socketClose() possible protocol error, retrying with no protocol');
94+
this.protocol_fallback = true;
95+
this.protocol = undefined;
96+
this.connect();
97+
return;
98+
}
99+
84100
this.debugOut('socketClose()');
85101
this.connected = false;
86102
this.emit('close', this.last_socket_error ? this.last_socket_error : false);
87103
}
88104

89105
onSocketMessage(data) {
106+
if (typeof data !== 'string') {
107+
this.last_socket_error = new Error('Websocket received unexpected binary data, closing the connection');
108+
this.debugOut('socketData() ' + this.last_socket_error.message);
109+
this.close();
110+
return;
111+
}
112+
90113
this.debugOut('socketData() ' + JSON.stringify(data));
91114

92115
const that = this;

0 commit comments

Comments
 (0)