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