🚀 Launch 1.0
This commit is contained in:
@@ -29,14 +29,14 @@ class SolarNetworkConnect : JavaPlugin() {
|
|||||||
private var messages: Map<String, String> = mapOf()
|
private var messages: Map<String, String> = mapOf()
|
||||||
|
|
||||||
private fun handleWebSocketPacket(packet: WebSocketPacket) {
|
private fun handleWebSocketPacket(packet: WebSocketPacket) {
|
||||||
// logger.info("Received WebSocket packet: type=${packet.type}")
|
logger.info("Received WebSocket packet: type=${packet.type}")
|
||||||
if (packet.type.startsWith("messages") && packet.data != null) {
|
if (packet.type.startsWith("messages") && packet.data != null) {
|
||||||
try {
|
try {
|
||||||
when (packet.type) {
|
when (packet.type) {
|
||||||
"messages.new" -> {
|
"messages.new" -> {
|
||||||
val message = SnChatMessage.fromJson(packet.data)
|
val message = SnChatMessage.fromJson(packet.data)
|
||||||
// Ignore automated accounts
|
// Ignore automated accounts
|
||||||
if (message.sender.account.automatedId.isNullOrBlank()) return;
|
if (message.sender.account.automatedId.isNullOrBlank().not()) return;
|
||||||
// Only some rooms got synced
|
// Only some rooms got synced
|
||||||
if (syncChatRooms.isEmpty() || syncChatRooms.contains(message.chatRoomId)) {
|
if (syncChatRooms.isEmpty() || syncChatRooms.contains(message.chatRoomId)) {
|
||||||
val roomName = message.chatRoom.name ?: "DM"
|
val roomName = message.chatRoom.name ?: "DM"
|
||||||
|
@@ -2,6 +2,7 @@ package dev.solsynth.snConnect.services
|
|||||||
|
|
||||||
import dev.solsynth.snConnect.models.WebSocketPacket
|
import dev.solsynth.snConnect.models.WebSocketPacket
|
||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.channelFlow
|
import kotlinx.coroutines.flow.channelFlow
|
||||||
@@ -47,9 +48,10 @@ class SnService(private val baseUrl: String, val clientId: String, val clientSec
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
fun connect() {
|
fun connect() {
|
||||||
|
logger.info("Attempting WebSocket connection to $url")
|
||||||
websocket = client.newWebSocket(request, object : WebSocketListener() {
|
websocket = client.newWebSocket(request, object : WebSocketListener() {
|
||||||
override fun onOpen(webSocket: WebSocket, response: Response) {
|
override fun onOpen(webSocket: WebSocket, response: Response) {
|
||||||
logger.info("WebSocket connection opened to $url")
|
logger.info("WebSocket connection opened successfully to $url")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onMessage(webSocket: WebSocket, text: String) {
|
override fun onMessage(webSocket: WebSocket, text: String) {
|
||||||
|
@@ -1,29 +1,23 @@
|
|||||||
package dev.solsynth.snConnect
|
package dev.solsynth.snConnect
|
||||||
|
|
||||||
import dev.solsynth.snConnect.services.SnService
|
import dev.solsynth.snConnect.services.SnService
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import kotlinx.coroutines.withTimeoutOrNull
|
import kotlinx.coroutines.withTimeoutOrNull
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import java.util.logging.Logger
|
|
||||||
|
|
||||||
class WebSocketTest {
|
suspend fun main() {
|
||||||
private val logger = Logger.getLogger(WebSocketTest::class.java.name)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testWebSocketConnection() = runBlocking {
|
|
||||||
val service = SnService(
|
val service = SnService(
|
||||||
baseUrl = "http://localhost:8080", // Replace with actual test server URL
|
baseUrl = "https://api.solian.app", // Replace with actual test server URL
|
||||||
clientId = "testClientId",
|
clientId = "goatcraft",
|
||||||
clientSecret = "testClientSecret",
|
clientSecret = "testClientSecret",
|
||||||
botApiKey = "testBotApiKey"
|
botApiKey = "02-l7ARXnEimQaBY0Dj3SQ.g4UTdShR9GiNWBDjC7qm9Xu83t0Vq8mQ2WPaTO8S-_j6EuKcWd0Kqb_hEkFlahmhfAd5lcH7j_-N8knSIXjo3X7OSFck0E_ogwluZpGSzqbYOrlBAQc9Rk1VhHVNu_W4fi9NR6NnUwpAgyTIh2RuRHk98oVa2I4Ie-NXPybb26N3i9wXv3-wXlkml0IOrhs3FRKMbcJNmKIzEYP0KQNqs3w9TAx0R2fe9DAAQ8WvPW5iPGSbyxr_fF4Qm7tQ0rQvg89x0cUGmKQHtCTeKa2WZi7UBTbw_b4SiHMqpLMxIDQBEZQGqkJ5r37_YCyCNqe5Huha86GG7bT__m6z5emeow"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
print("server started")
|
||||||
// Collect from the flow for a limited time to avoid hanging on failure
|
// Collect from the flow for a limited time to avoid hanging on failure
|
||||||
val packets = withTimeoutOrNull(10000L) { // 10 seconds timeout
|
val packets = withTimeoutOrNull(10000L) { // 10 seconds timeout
|
||||||
val collectedPackets = mutableListOf<String>()
|
val collectedPackets = mutableListOf<String>()
|
||||||
service.connectWebSocketAsFlow().collect { packet ->
|
service.connectWebSocketAsFlow().collect { packet ->
|
||||||
collectedPackets.add("Received packet: type=${packet.type}, endpoint=${packet.endpoint}")
|
collectedPackets.add("Received packet: type=${packet.type}, endpoint=${packet.endpoint}")
|
||||||
logger.info(collectedPackets.last())
|
print(collectedPackets.last())
|
||||||
// In a real test, you might check packet contents or behavior
|
// In a real test, you might check packet contents or behavior
|
||||||
}
|
}
|
||||||
collectedPackets
|
collectedPackets
|
||||||
@@ -31,7 +25,6 @@ class WebSocketTest {
|
|||||||
|
|
||||||
// For this test, we're mainly checking that the flow can be created and the setup doesn't crash
|
// For this test, we're mainly checking that the flow can be created and the setup doesn't crash
|
||||||
// In real scenarios, you'd need a WebSocket server running at the specified URL
|
// In real scenarios, you'd need a WebSocket server running at the specified URL
|
||||||
logger.info("WebSocket test finished. Packets received: ${packets?.size ?: 0}")
|
print("WebSocket test finished. Packets received: ${packets?.size ?: 0}")
|
||||||
assert(packets != null) // Flow started without immediate error
|
assert(packets != null) // Flow started without immediate error
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user