Added optional compression

This commit is contained in:
hydrabolt
2015-10-26 20:18:09 +00:00
parent c89633b72f
commit 5bb0ce141f
3 changed files with 39 additions and 8 deletions

View File

@@ -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));

View File

@@ -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));

View File

@@ -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 });