-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2112b7e
commit 9850636
Showing
8 changed files
with
348 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
264 changes: 247 additions & 17 deletions
264
src/main/kotlin/io/github/thecguy/cloudnet_rest_module/CloudNet_Rest_Module.kt
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/io/github/thecguy/cloudnet_rest_module/utli/WebsocketManager.kt
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,36 @@ | ||
package io.github.thecguy.cloudnet_rest_module.utli | ||
|
||
import io.ktor.websocket.* | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class WebsocketManager { | ||
|
||
object WebSocketManager { | ||
// Store sessions by service name | ||
private val serviceSessions: MutableMap<String, MutableSet<DefaultWebSocketSession>> = ConcurrentHashMap() | ||
|
||
// Add a WebSocket session for a specific service | ||
fun addSession(service: String, session: DefaultWebSocketSession) { | ||
serviceSessions.computeIfAbsent(service) { ConcurrentHashMap.newKeySet() }.add(session) | ||
} | ||
|
||
// Remove a WebSocket session for a specific service | ||
fun removeSession(service: String, session: DefaultWebSocketSession) { | ||
serviceSessions[service]?.remove(session) | ||
if (serviceSessions[service]?.isEmpty() == true) { | ||
serviceSessions.remove(service) // Clean up if no sessions are left | ||
} | ||
} | ||
|
||
// Send message to all open WebSocket sessions for a specific service | ||
suspend fun broadcast(service: String, message: String) { | ||
serviceSessions[service]?.forEach { session -> | ||
try { | ||
session.send(message) | ||
} catch (e: Exception) { | ||
e.printStackTrace() // Handle errors during message sending | ||
} | ||
} | ||
} | ||
} | ||
} |