mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 21:13:30 +01:00
Replace ws with uws (#918)
* change to uws (waiting for the next release tho) * clean up, fix reconnections (maybe) * change voice to use uws * so messy
This commit is contained in:
committed by
Schuyler Cebulskie
parent
32879419e2
commit
c91ee7a3e7
@@ -29,9 +29,10 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/hydrabolt/discord.js#readme",
|
"homepage": "https://github.com/hydrabolt/discord.js#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"pako": "^1.0.3",
|
||||||
"superagent": "^3.0.0",
|
"superagent": "^3.0.0",
|
||||||
"tweetnacl": "^0.14.3",
|
"tweetnacl": "^0.14.3",
|
||||||
"ws": "^1.1.1"
|
"uws": "^0.11.0"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"node-opus": "^0.2.0",
|
"node-opus": "^0.2.0",
|
||||||
@@ -46,14 +47,13 @@
|
|||||||
"parallel-webpack": "^1.5.0",
|
"parallel-webpack": "^1.5.0",
|
||||||
"uglify-js": "github:mishoo/UglifyJS2#harmony",
|
"uglify-js": "github:mishoo/UglifyJS2#harmony",
|
||||||
"utf-8-validate": "^1.2.1",
|
"utf-8-validate": "^1.2.1",
|
||||||
"webpack": "2.1.0-beta.27",
|
"webpack": "2.1.0-beta.27"
|
||||||
"zlibjs": "^0.2.0"
|
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=6.0.0"
|
"node": ">=6.0.0"
|
||||||
},
|
},
|
||||||
"browser": {
|
"browser": {
|
||||||
"ws": false,
|
"uws": false,
|
||||||
"opusscript": false,
|
"opusscript": false,
|
||||||
"node-opus": false,
|
"node-opus": false,
|
||||||
"tweet-nacl": false,
|
"tweet-nacl": false,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const WebSocket = require('ws');
|
const WebSocket = require('uws');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const SecretKey = require('./util/SecretKey');
|
const SecretKey = require('./util/SecretKey');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
const browser = typeof window !== 'undefined';
|
const browser = typeof window !== 'undefined';
|
||||||
const WebSocket = browser ? window.WebSocket : require('ws'); // eslint-disable-line no-undef
|
const WebSocket = browser ? window.WebSocket : require('uws'); // eslint-disable-line no-undef
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const inflate = browser ? require('zlibjs').inflateSync : require('zlib').inflateSync;
|
const pako = require('pako');
|
||||||
const PacketManager = require('./packets/WebSocketPacketManager');
|
const PacketManager = require('./packets/WebSocketPacketManager');
|
||||||
const convertArrayBuffer = require('../../util/ConvertArrayBuffer');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The WebSocket Manager of the Client
|
* The WebSocket Manager of the Client
|
||||||
@@ -80,11 +79,16 @@ class WebSocketManager extends EventEmitter {
|
|||||||
this.normalReady = false;
|
this.normalReady = false;
|
||||||
if (this.status !== Constants.Status.RECONNECTING) this.status = Constants.Status.CONNECTING;
|
if (this.status !== Constants.Status.RECONNECTING) this.status = Constants.Status.CONNECTING;
|
||||||
this.ws = new WebSocket(gateway);
|
this.ws = new WebSocket(gateway);
|
||||||
if (browser) this.ws.binaryType = 'arraybuffer';
|
if (browser) {
|
||||||
this.ws.onopen = () => this.eventOpen();
|
this.ws.binaryType = 'arraybuffer';
|
||||||
|
this.ws.on('open', this.eventOpen.bind(this));
|
||||||
|
this.ws.on('error', this.eventError.bind(this));
|
||||||
|
} else {
|
||||||
|
this.ws.onopen = () => this.eventOpen();
|
||||||
|
this.ws.onerror = (e) => this.eventError(e);
|
||||||
|
}
|
||||||
this.ws.onclose = (d) => this.eventClose(d);
|
this.ws.onclose = (d) => this.eventClose(d);
|
||||||
this.ws.onmessage = (e) => this.eventMessage(e);
|
this.ws.onmessage = (e) => this.eventMessage(e);
|
||||||
this.ws.onerror = (e) => this.eventError(e);
|
|
||||||
this._queue = [];
|
this._queue = [];
|
||||||
this._remaining = 3;
|
this._remaining = 3;
|
||||||
}
|
}
|
||||||
@@ -230,10 +234,7 @@ class WebSocketManager extends EventEmitter {
|
|||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
parseEventData(data) {
|
parseEventData(data) {
|
||||||
if (typeof data !== 'string') {
|
if (data instanceof ArrayBuffer) data = pako.inflate(data, { to: 'string' });
|
||||||
if (data instanceof ArrayBuffer) data = convertArrayBuffer(data);
|
|
||||||
data = inflate(data).toString();
|
|
||||||
}
|
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,7 +262,7 @@ class WebSocketManager extends EventEmitter {
|
|||||||
* @param {Error} error The encountered error
|
* @param {Error} error The encountered error
|
||||||
*/
|
*/
|
||||||
if (this.client.listenerCount('error') > 0) this.client.emit('error', err);
|
if (this.client.listenerCount('error') > 0) this.client.emit('error', err);
|
||||||
this.ws.close();
|
this.tryReconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
_emitReady(normal = true) {
|
_emitReady(normal = true) {
|
||||||
@@ -305,6 +306,7 @@ class WebSocketManager extends EventEmitter {
|
|||||||
* Tries to reconnect the client, changing the status to Constants.Status.RECONNECTING.
|
* Tries to reconnect the client, changing the status to Constants.Status.RECONNECTING.
|
||||||
*/
|
*/
|
||||||
tryReconnect() {
|
tryReconnect() {
|
||||||
|
if (this.status === Constants.Status.RECONNECTING) return;
|
||||||
this.status = Constants.Status.RECONNECTING;
|
this.status = Constants.Status.RECONNECTING;
|
||||||
this.ws.close();
|
this.ws.close();
|
||||||
this.packetManager.handleQueue();
|
this.packetManager.handleQueue();
|
||||||
|
|||||||
Reference in New Issue
Block a user