question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I connect to spring boot websocket?

See original GitHub issue

springboot config

    override fun registerStompEndpoints(registry: StompEndpointRegistry) {
        super.registerStompEndpoints(registry)
        registry.addEndpoint("/idea-chat")
            .setAllowedOrigins("*")
            .setHandshakeHandler(principalHandshakeHandle)
            .addInterceptors(jwtVaildInterceptor)
    }

    override fun configureMessageBroker(registry: MessageBrokerRegistry) {
        registry.enableSimpleBroker("/queue", "/topic")
        registry.setUserDestinationPrefix("/user")
        super.configureMessageBroker(registry)
    }

krossbow


    private  var tokenField = JTextField()

    @OptIn(DelicateCoroutinesApi::class)
    private fun socketConnect() {
        GlobalScope.launch {
            val stompClient = StompClient(KtorWebSocketClient())
            try{
                val session = stompClient.connect("ws://192.168.199.86/idea-chat?token=${tokenField.text}")
                val subscribeText = session.subscribeText("/room/闲聊吹水")
                launch {
                    subscribeText.collect {msg ->
                        println("msg=:$msg")
                    }
                }
            }catch (e: StompConnectionException) {
                println(":$e  ")
            }
        }
    }

error log

Exception in thread "DefaultDispatcher-worker-2" org.hildan.krossbow.stomp.WebSocketConnectionException: Failed to connect at web socket level to ws://192.168.199.86/idea-chat?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Njc1MzA2OTMsInVzZXJuYW1lIjoiYWRtaW4ifQ.Tv-6SlXjB6_GG6FhyzaEMaACtnSX6qfDLWhfgsUbiog
	at org.hildan.krossbow.stomp.StompClient.webSocketConnect(StompClient.kt:86)
	at org.hildan.krossbow.stomp.StompClient.access$webSocketConnect(StompClient.kt:23)
	at org.hildan.krossbow.stomp.StompClient$webSocketConnect$1.invokeSuspend(StompClient.kt)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
	Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@53390416, Dispatchers.Default]
Caused by: org.hildan.krossbow.websocket.WebSocketConnectionException: Couldn't connect to web socket at ws://192.168.199.86/idea-chat?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Njc1MzA2OTMsInVzZXJuYW1lIjoiYWRtaW4ifQ.Tv-6SlXjB6_GG6FhyzaEMaACtnSX6qfDLWhfgsUbiog
	at org.hildan.krossbow.websocket.ktor.KtorWebSocketClient.connect(KtorWebSocketClient.kt:30)
	at org.hildan.krossbow.websocket.ktor.KtorWebSocketClient$connect$1.invokeSuspend(KtorWebSocketClient.kt)
	... 6 more
Caused by: java.lang.IllegalArgumentException: Engine doesn't support WebSocketCapability
	at io.ktor.client.engine.HttpClientEngine$DefaultImpls.checkExtensions(HttpClientEngine.kt:105)
	at io.ktor.client.engine.HttpClientEngine$DefaultImpls.access$checkExtensions(HttpClientEngine.kt:24)
	at io.ktor.client.engine.HttpClientEngine$install$1.invokeSuspend(HttpClientEngine.kt:68)
	at io.ktor.client.engine.HttpClientEngine$install$1.invoke(HttpClientEngine.kt)
	at io.ktor.client.engine.HttpClientEngine$install$1.invoke(HttpClientEngine.kt)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81)
	at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:101)
	at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
	at io.ktor.client.plugins.HttpSend$DefaultSender.execute(HttpSend.kt:138)
	at io.ktor.client.plugins.HttpRedirect$Plugin$install$1.invokeSuspend(HttpRedirect.kt:61)
	at io.ktor.client.plugins.HttpRedirect$Plugin$install$1.invoke(HttpRedirect.kt)
	at io.ktor.client.plugins.HttpRedirect$Plugin$install$1.invoke(HttpRedirect.kt)
	at io.ktor.client.plugins.HttpSend$InterceptedSender.execute(HttpSend.kt:116)
	at io.ktor.client.plugins.HttpCallValidator$Companion$install$3.invokeSuspend(HttpCallValidator.kt:147)
	at io.ktor.client.plugins.HttpCallValidator$Companion$install$3.invoke(HttpCallValidator.kt)
	at io.ktor.client.plugins.HttpCallValidator$Companion$install$3.invoke(HttpCallValidator.kt)
	at io.ktor.client.plugins.HttpSend$InterceptedSender.execute(HttpSend.kt:116)
	at io.ktor.client.plugins.HttpSend$Plugin$install$1.invokeSuspend(HttpSend.kt:104)
	at io.ktor.client.plugins.HttpSend$Plugin$install$1.invoke(HttpSend.kt)
	at io.ktor.client.plugins.HttpSend$Plugin$install$1.invoke(HttpSend.kt)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:91)
	at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invokeSuspend(WebSockets.kt:161)
	at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invoke(WebSockets.kt)
	at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invoke(WebSockets.kt)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:91)
	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:126)
	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81)
	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invokeSuspend(HttpRequestLifecycle.kt:35)
	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(HttpRequestLifecycle.kt)
	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(HttpRequestLifecycle.kt)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81)
	at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:101)
	at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
	at io.ktor.client.HttpClient.execute$ktor_client_core(HttpClient.kt:191)
	at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:108)
	at io.ktor.client.plugins.websocket.BuildersKt$webSocketSession$2.invokeSuspend(builders.kt:239)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
	... 4 more

I’m a novice. It’s boring. I can’t connect it with anything

my code repository https://github.com/mdddj/json_to_dart_pro/blob/ab5e947ea6612358ac84b2a15449f8fe4cbb1ccd/src/main/kotlin/windown/ChatWindow.kt#L49

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mdddjcommented, Nov 4, 2022

Thank you for your help. I found a solution to the problem. Deleting the @ EnableWebSocketSecurity annotation in Springboot3.0 will solve the problem

https://docs.spring.io/spring-security/reference/6.0/servlet/integrations/websocket.html

image

0reactions
joffrey-bioncommented, Nov 4, 2022

Glad that you found the problem!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using WebSocket to build an interactive web application
This guide walks you through the process of creating a “Hello, world” application that sends messages back and forth between a browser and...
Read more >
Spring Boot + WebSocket Simple Example - JavaInUse
Start the application- Go to http://localhost:8080 Click on start new chat it opens the WebSocket connection. Type text in the textbox and click...
Read more >
Intro to WebSockets with Spring - Baeldung
WebSockets is a bidirectional, full-duplex, persistent connection between a web browser and a server. Once a WebSocket connection is ...
Read more >
Using Spring Boot for WebSocket Implementation with STOMP
Implementing a client is an even simpler task. Step 1. Autowire Spring STOMP client. @Autowired private WebSocketStompClient stompClient;. Step 2. Open a ...
Read more >
how to capture connection event in my webSocket server with ...
Spring WebSocket publishes events when messages are received from the client, if you are using STOMP, these are the events published:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found