mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +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",
|
||||
"dependencies": {
|
||||
"pako": "^1.0.3",
|
||||
"superagent": "^3.0.0",
|
||||
"tweetnacl": "^0.14.3",
|
||||
"ws": "^1.1.1"
|
||||
"uws": "^0.11.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"node-opus": "^0.2.0",
|
||||
@@ -46,14 +47,13 @@
|
||||
"parallel-webpack": "^1.5.0",
|
||||
"uglify-js": "github:mishoo/UglifyJS2#harmony",
|
||||
"utf-8-validate": "^1.2.1",
|
||||
"webpack": "2.1.0-beta.27",
|
||||
"zlibjs": "^0.2.0"
|
||||
"webpack": "2.1.0-beta.27"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
},
|
||||
"browser": {
|
||||
"ws": false,
|
||||
"uws": false,
|
||||
"opusscript": false,
|
||||
"node-opus": false,
|
||||
"tweet-nacl": false,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const WebSocket = require('ws');
|
||||
const WebSocket = require('uws');
|
||||
const Constants = require('../../util/Constants');
|
||||
const SecretKey = require('./util/SecretKey');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
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 Constants = require('../../util/Constants');
|
||||
const inflate = browser ? require('zlibjs').inflateSync : require('zlib').inflateSync;
|
||||
const pako = require('pako');
|
||||
const PacketManager = require('./packets/WebSocketPacketManager');
|
||||
const convertArrayBuffer = require('../../util/ConvertArrayBuffer');
|
||||
|
||||
/**
|
||||
* The WebSocket Manager of the Client
|
||||
@@ -80,11 +79,16 @@ class WebSocketManager extends EventEmitter {
|
||||
this.normalReady = false;
|
||||
if (this.status !== Constants.Status.RECONNECTING) this.status = Constants.Status.CONNECTING;
|
||||
this.ws = new WebSocket(gateway);
|
||||
if (browser) this.ws.binaryType = 'arraybuffer';
|
||||
this.ws.onopen = () => this.eventOpen();
|
||||
if (browser) {
|
||||
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.onmessage = (e) => this.eventMessage(e);
|
||||
this.ws.onerror = (e) => this.eventError(e);
|
||||
this._queue = [];
|
||||
this._remaining = 3;
|
||||
}
|
||||
@@ -230,10 +234,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* @returns {Object}
|
||||
*/
|
||||
parseEventData(data) {
|
||||
if (typeof data !== 'string') {
|
||||
if (data instanceof ArrayBuffer) data = convertArrayBuffer(data);
|
||||
data = inflate(data).toString();
|
||||
}
|
||||
if (data instanceof ArrayBuffer) data = pako.inflate(data, { to: 'string' });
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
@@ -261,7 +262,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* @param {Error} error The encountered error
|
||||
*/
|
||||
if (this.client.listenerCount('error') > 0) this.client.emit('error', err);
|
||||
this.ws.close();
|
||||
this.tryReconnect();
|
||||
}
|
||||
|
||||
_emitReady(normal = true) {
|
||||
@@ -305,6 +306,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* Tries to reconnect the client, changing the status to Constants.Status.RECONNECTING.
|
||||
*/
|
||||
tryReconnect() {
|
||||
if (this.status === Constants.Status.RECONNECTING) return;
|
||||
this.status = Constants.Status.RECONNECTING;
|
||||
this.ws.close();
|
||||
this.packetManager.handleQueue();
|
||||
|
||||
Reference in New Issue
Block a user