Skip to content

Commit

Permalink
Add ability to resume voice connection
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Nov 17, 2024
1 parent ed3760e commit 0f5e875
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
13 changes: 7 additions & 6 deletions core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ public interface LavaKord : CoroutineScope, EventSource<Event> {
*
* @param serverUri the uri to connect to
* @param password the lavalink node password
* @param name a optional name for the node
* @param name an optional name for the node
*/
public fun addNode(serverUri: String, password: String, name: String? = null): Unit =
addNode(Url(serverUri), password, name)
public fun addNode(serverUri: String, password: String, name: String? = null, session: String? = null): Unit =
addNode(Url(serverUri), password, name, session)

/**
* Adds a new node to this cluster.
*
* @param serverUri the uri to connect to
* @param password the lavalink node password
* @param name a optional name for the node
* @param name an optional name for the node
* @param session an optional session to resume
*/
public fun addNode(serverUri: Url, password: String, name: String? = null)
public fun addNode(serverUri: Url, password: String, name: String? = null, session: String? = null)

/**
* Creates and returns a new [rest-only node][RestNode].
Expand All @@ -62,7 +63,7 @@ public interface LavaKord : CoroutineScope, EventSource<Event> {
public fun createRestNode(serverUri: Url, password: String, name: String? = null): RestNode

/**
* Removes a node from the cluster by it's [name].
* Removes a node from the cluster by its [name].
*/
public fun removeNode(name: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public abstract class AbstractLavakord internal constructor(
}
}

override fun addNode(serverUri: Url, password: String, name: String?) {
override fun addNode(serverUri: Url, password: String, name: String?, session: String?) {
if (name != null) {
check(!nodesMap.containsKey(name)) { "Name is already in use" }
}
Expand All @@ -160,7 +160,7 @@ public abstract class AbstractLavakord internal constructor(
nodesMap[finalName] = node
launch {
node.check()
node.connect()
node.connect(initialSessionId = session)
}
}

Expand All @@ -178,7 +178,7 @@ public abstract class AbstractLavakord internal constructor(
}

/**
* Forwards an voice server update event to Lavalink.
* Forwards a voice server update event to Lavalink.
*/
protected suspend fun forwardVoiceEvent(
link: Link,
Expand All @@ -203,7 +203,7 @@ public abstract class AbstractLavakord internal constructor(
}

/** Called on websocket connect without resuming */
internal suspend fun onNewSession(node: Node) {
internal fun onNewSession(node: Node) {
if (!options.link.autoReconnect) return
linksMap.values.filter { it.node == node }.forEach {
launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal class NodeImpl(
override val host: Url,
override val name: String,
override val authenticationHeader: String,
override val lavakord: AbstractLavakord,
override val lavakord: AbstractLavakord
) : Node {

private val resumeTimeout = lavakord.options.link.resumeTimeout
Expand Down Expand Up @@ -101,9 +101,11 @@ internal class NodeImpl(
}

@OptIn(DelicateCoroutinesApi::class)
internal suspend fun connect(resume: Boolean = false) {
internal suspend fun connect(doResume: Boolean = false, initialSessionId: String? = null) {
val resume = doResume || initialSessionId != null
sessionId = initialSessionId ?: sessionId
session = try {
connect(resume) {
connect(resume || initialSessionId != null) {
addUrl()
timeout {
requestTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS
Expand Down

0 comments on commit 0f5e875

Please sign in to comment.