简体   繁体   中英

How do I connect to a Spring STOMP websocket using a native Android client?

I'm trying to do a small chatroom practice project using Android Studio and Spring Boot with kotlin. My problem is that I've already tried using many different libraries to connect the app to the websocket: For example: OkHttp, Scarlet, Ktor

But so far I haven't found a solution.

For the websocket code I followed the tutorial on the spring website. Here is the code:

@Controller
class MessageController {
    
    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    fun greeting(message: String): String {
        return message
    }
}
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig : WebSocketMessageBrokerConfigurer {

    override fun configureMessageBroker(registry: MessageBrokerRegistry) {
        registry.enableSimpleBroker("/topic")
        registry.setApplicationDestinationPrefixes("/app")
    }

    override fun registerStompEndpoints(registry: StompEndpointRegistry) {
        registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS()
    }
}

So my question is: How can i connect a android app to this websocket?

STOMP is an application-level protocol which is used here on top of web sockets. Built-in clients rarely support this kind of protocol and in your case I don't believe Android has anything like it.

You most likely need a library that supports STOMP in order to do that. I'm the author of one such library called Krossbow if you're interested.

OkHttp, Scarlet, and Ktor don't support STOMP out of the box (at least not yet), but with Krossbow you can use Ktor or OkHttp as underlying web socket clients if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM