-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make use of Ktor websocket extensions and serialization
Add Compression.
- Loading branch information
1 parent
2a9be0f
commit 7a0adff
Showing
6 changed files
with
273 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package dev.kord.gateway | ||
|
||
import dev.kord.common.annotation.KordUnsafe | ||
import io.ktor.util.* | ||
import io.ktor.websocket.* | ||
import java.io.ByteArrayOutputStream | ||
import java.util.zip.Inflater | ||
import java.util.zip.InflaterOutputStream | ||
|
||
/** | ||
* [WebSocketExtension] inflating incoming websocket requests using `zlib`. | ||
* | ||
* *Note:** Normally you don't need this and this is configured by Kord automatically, however, if you want to use | ||
* a custom HTTP client, you might need to add this, don't use it if you don't use what you're doing | ||
*/ | ||
@KordUnsafe | ||
public class WebSocketCompression : WebSocketExtension<Unit> { | ||
/** | ||
* https://discord.com/developers/docs/topics/gateway#transport-compression | ||
* | ||
* > Every connection to the gateway should use its own unique zlib context. | ||
* | ||
* https://api.ktor.io/ktor-shared/ktor-websockets/io.ktor.websocket/-web-socket-extension/index.html | ||
* > A WebSocket extension instance. This instance is created for each WebSocket request, | ||
* for every installed extension by WebSocketExtensionFactory. | ||
*/ | ||
private val inflater = Inflater() | ||
|
||
override val factory: WebSocketExtensionFactory<Unit, out WebSocketExtension<Unit>> | ||
get() = Companion | ||
override val protocols: List<WebSocketExtensionHeader> | ||
get() = emptyList() | ||
|
||
override fun clientNegotiation(negotiatedProtocols: List<WebSocketExtensionHeader>): Boolean = true | ||
|
||
override fun processIncomingFrame(frame: Frame): Frame { | ||
return if (frame is Frame.Binary) { | ||
frame.deflateData() | ||
} else { | ||
frame | ||
} | ||
} | ||
|
||
// Discord doesn't support deflating of gateway commands | ||
override fun processOutgoingFrame(frame: Frame): Frame = frame | ||
|
||
override fun serverNegotiation(requestedProtocols: List<WebSocketExtensionHeader>): List<WebSocketExtensionHeader> = | ||
requestedProtocols | ||
|
||
private fun Frame.deflateData(): Frame { | ||
val outputStream = ByteArrayOutputStream() | ||
InflaterOutputStream(outputStream, inflater).use { | ||
it.write(data) | ||
} | ||
|
||
return outputStream.use { | ||
val raw = String(outputStream.toByteArray(), 0, outputStream.size(), Charsets.UTF_8) | ||
Frame.Text(raw) | ||
} | ||
} | ||
|
||
public companion object : WebSocketExtensionFactory<Unit, WebSocketCompression> { | ||
override val key: AttributeKey<WebSocketCompression> = AttributeKey("WebSocketCompression") | ||
override val rsv1: Boolean = false | ||
override val rsv2: Boolean = false | ||
override val rsv3: Boolean = false | ||
|
||
override fun install(config: Unit.() -> Unit): WebSocketCompression = WebSocketCompression() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.