[WIP] Added customizable gateway payload compression and added guild subscriptions option to identify

[ci skip]
This commit is contained in:
Szymon Uglis 2020-06-04 14:04:40 +02:00
parent 70c3d585d5
commit 6a400b5f07
3 changed files with 25 additions and 12 deletions

View file

@ -33,6 +33,12 @@ class ClientOptions {
/// If you do not specify a certain intent, you will not receive any of the gateway events that are batched into that group.
GatewayIntents? gatewayIntents;
/// Allows to receive compressed payloads from gateway
bool compressedGatewayPayloads;
/// Enables dispatching of guild subscription events (presence and typing events)
bool guildSubscriptions;
/// Makes a new `ClientOptions` object.
ClientOptions(
{this.allowedMentions,
@ -42,7 +48,9 @@ class ClientOptions {
this.cacheMembers = true,
this.largeThreshold = 50,
this.ignoredEvents = const [],
this.gatewayIntents});
this.gatewayIntents,
this.compressedGatewayPayloads = true,
this.guildSubscriptions = true });
}
/// When identifying to the gateway, you can specify an intents parameter which

View file

@ -227,7 +227,8 @@ class Shard implements Disposable {
"\$device": "nyxx",
},
"large_threshold": manager._ws._client._options.largeThreshold,
"compress": false
"compress": manager._ws._client._options.compressedGatewayPayloads,
"guild_subscriptions" : manager._ws._client._options.guildSubscriptions
};
if (manager._ws._client._options.gatewayIntents != null) {
@ -463,14 +464,15 @@ Future<void> _shardHandler(SendPort shardPort) async {
// Attempts to connect to ws
Future<void> _connect([bool resume = false]) async {
await _socket?.close();
_socket = null;
await transport.WebSocket.connect(gatewayUri).then((ws) {
_socket = ws;
_socket!.listen((data) {
shardPort.send({ "cmd" : "DATA", "jsonData" : _decodeBytes(data), "resume" : resume});
}, onDone: () async {
if(_socket!.closeCode == 1010) {
return;
}
shardPort.send({ "cmd" : "DISCONNECTED", "errorCode" : _socket!.closeCode, "errorReason" : _socket!.closeReason });
}, onError: (err) => shardPort.send({ "cmd" : "ERROR", "error": err.toString(), "errorCode" : _socket!.closeCode, "errorReason" : _socket!.closeReason }));
}, onError: (err, __) => shardPort.send({ "cmd" : "ERROR", "error": err.toString(), "errorCode" : _socket!.closeCode, "errorReason" : _socket!.closeReason }));
@ -487,17 +489,17 @@ Future<void> _shardHandler(SendPort shardPort) async {
}
if(cmd == "RECONNECT") {
await _socket?.close(1000);
await _socket?.close(1010);
await _connect(true);
}
if(cmd == "CONNECT") {
await _socket?.close(1000);
await _socket?.close(1010);
await _connect();
}
if(cmd == "TERMINATE") {
await _socket?.close(1000);
await _socket?.close(1010);
shardPort.send({ "cmd" : "TERMINATE_OK" });
}
}

View file

@ -124,6 +124,9 @@ class Guild extends SnowflakeEntity implements Disposable {
return 8 * megabyte;
}
/// Returns this guilds shard
Shard get shard => client.shardManager.shards.firstWhere((_shard) => _shard.guilds.contains(this.id));
Guild._new(this.client, Map<String, dynamic> raw, [this.available = true, bool guildCreate = false])
: super(Snowflake(raw["id"] as String)) {
if (!this.available) return;
@ -696,9 +699,9 @@ class Guild extends SnowflakeEntity implements Disposable {
Stream<IMember> searchMembersGateway(String query, {int limit = 0}) async* {
final nonce = "$query${id.toString()}";
this.client.shard.requestMembers(this.id, query: query, limit: limit, nonce: nonce);
this.shard.requestMembers(this.id, query: query, limit: limit, nonce: nonce);
final first = (await this.client.shard.onMemberChunk.take(1).toList()).first;
final first = (await this.shard.onMemberChunk.take(1).toList()).first;
for (final member in first.members) {
yield member;
@ -711,8 +714,8 @@ class Guild extends SnowflakeEntity implements Disposable {
}
}
}
}*/
}
*/
/// Gets all of the webhooks for this channel.
Stream<Webhook> getWebhooks() async* {
final response = await client._http._execute(BasicRequest._new("/channels/$id/webhooks"));