From 5bb0ce141f9007350c52c2e71dc80d11464c656c Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Mon, 26 Oct 2015 20:18:09 +0000 Subject: [PATCH] Added optional compression --- lib/Client.js | 20 +++++++++++++++++--- src/Client.js | 23 +++++++++++++++++++---- test/bot.1.js | 4 +++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/Client.js b/lib/Client.js index 5aa5fce5d..88c9502cf 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -14,6 +14,7 @@ var Invite = require("./invite.js"); var PMChannel = require("./PMChannel.js"); var ServerPermissions = require("./ServerPermissions.js"); var gameMap = require("../ref/gameMap.json"); +var zlib; //node modules var request = require("superagent"); @@ -37,7 +38,13 @@ var Client = (function () { further efforts will be made to connect. */ this.options = options; - this.options.queue = this.options.queue; + this.options.compress = options.compress; + + if (this.options.compress) { + // only require zlib if necessary + zlib = require("zlib"); + } + this.token = token; this.state = 0; this.websocket = null; @@ -1024,6 +1031,12 @@ var Client = (function () { //message this.websocket.onmessage = function (e) { + if (e.type === "Binary") { + if (!zlib) zlib = require("zlib"); + + e.data = zlib.inflateSync(e.data).toString(); + } + var dat = false, data = {}; @@ -1661,7 +1674,7 @@ var Client = (function () { //def trySendConnData Client.prototype.trySendConnData = function trySendConnData() { - + var self = this; if (this.token && !this.alreadySentData) { this.alreadySentData = true; @@ -1677,7 +1690,8 @@ var Client = (function () { "$device": "discord.js", "$referrer": "", "$referring_domain": "" - } + }, + compress: self.options.compress } }; this.websocket.send(JSON.stringify(data)); diff --git a/src/Client.js b/src/Client.js index 25581f17f..7e6df5b62 100644 --- a/src/Client.js +++ b/src/Client.js @@ -8,6 +8,7 @@ var Invite = require("./invite.js"); var PMChannel = require("./PMChannel.js"); var ServerPermissions = require("./ServerPermissions.js"); var gameMap = require("../ref/gameMap.json"); +var zlib; //node modules var request = require("superagent"); @@ -27,7 +28,13 @@ class Client { further efforts will be made to connect. */ this.options = options; - this.options.queue = this.options.queue; + this.options.compress = options.compress; + + if(this.options.compress){ + // only require zlib if necessary + zlib = require("zlib"); + } + this.token = token; this.state = 0; this.websocket = null; @@ -1108,7 +1115,14 @@ class Client { //message this.websocket.onmessage = function (e) { - + + if(e.type === "Binary"){ + if(!zlib) + zlib = require("zlib"); + + e.data = zlib.inflateSync(e.data).toString(); + } + var dat = false, data = {}; try { @@ -1628,7 +1642,7 @@ class Client { //def trySendConnData trySendConnData() { - + var self = this; if (this.token && !this.alreadySentData) { this.alreadySentData = true; @@ -1644,7 +1658,8 @@ class Client { "$device": "discord.js", "$referrer": "", "$referring_domain": "" - } + }, + compress : self.options.compress } }; this.websocket.send(JSON.stringify(data)); diff --git a/test/bot.1.js b/test/bot.1.js index 722656d26..473e53bf2 100644 --- a/test/bot.1.js +++ b/test/bot.1.js @@ -1,6 +1,8 @@ var Discord = require("../"); var Member = require("../lib/Member.js"); -var mybot = new Discord.Client(); +var mybot = new Discord.Client({ + compress : true +}); var fs = require("fs"); var request = require("request").defaults({ encoding: null });