~emersion/gamja

d9344916741f36b8e2b33a8d27985aacfbe842dc — Simon Ser 2 years ago 76f097e ircv3-websocket
Add support for IRCv3 WebSocket subprotocol

References: https://github.com/ircv3/ircv3-specifications/pull/342
1 files changed, 12 insertions(+), 1 deletions(-)

M lib/client.js
M lib/client.js => lib/client.js +12 -1
@@ 27,6 27,7 @@ const RECONNECT_DELAY_SEC = 10;
const NORMAL_CLOSURE = 1000;
const GOING_AWAY = 1001;
const UNSUPPORTED_DATA = 1003;
const ABNORMAL_CLOSURE = 1006;

let lastLabel = 0;



@@ 46,6 47,7 @@ export default class Client extends EventTarget {
	isupport = new Map();

	ws = null;
	requestSubprotocol = true;
	params = {
		url: null,
		username: null,


@@ 80,8 82,10 @@ export default class Client extends EventTarget {
		console.log("Connecting to " + this.params.url);
		this.setStatus(Client.Status.CONNECTING);

		let subproto = this.requestSubprotocol ? "text.ircv3.net" : undefined;

		try {
			this.ws = new WebSocket(this.params.url);
			this.ws = new WebSocket(this.params.url, subproto);
		} catch (err) {
			console.error("Failed to create connection:", err);
			setTimeout(() => {


@@ 96,6 100,13 @@ export default class Client extends EventTarget {
		this.ws.addEventListener("close", (event) => {
			console.log("Connection closed (code: " + event.code + ")");

			if (this.status === Client.Status.CONNECTING && this.requestSubprotocol && !this.ws.protocol && event.code === ABNORMAL_CLOSURE) {
				console.log("Connection with a sub-protocol failed, retrying without any subprotocol");
				this.requestSubprotocol = false;
				this.reconnect();
				return;
			}

			if (event.code !== NORMAL_CLOSURE && event.code !== GOING_AWAY) {
				this.dispatchEvent(new CustomEvent("error", { detail: "Connection error" }));
			}