Could anybody help that what is wrong with this co...
# http4k
m
Could anybody help that what is wrong with this code?
Copy code
val app = { request: Request -> Response(OK).body("Hello, ${request.query("name")}!") }

val jettyServer = app.asServer(Jetty(9006)).start()
 val client = WebsocketClient.blocking(Uri.of("<wss://echo.websocket.org>"), headers = listOf(Pair("a header", "value")))

        
        client.send(WsMessage("hello"))

        // read all of the messages from the socket until it is closed (by the server).
        // we expect to get one message back before the stream is closed.
        client.received().toList().forEach(::println)
I am able to connect but I am not receiving any message. I want to develop a websocket client always send and receive messages to/from the server
d
The problem here is that you are calling toList() on the received sequence (which reifies it into a list), and the server isn't being closed by the server so the sequence never completes. if you remove toList() then you get the echo
m
Thanks @dave Yes, you are right, The second question is how could I keep listen to the server and process the incoming message one by one. I can not see any sample code for that. I can see a queue. I want to have a listener on the queue.
I guess that for this purpose I need to use
nonBlocking
one. Sorry to ask this questions I am quite new to http4k😊
d
Np. helping is what we're here for! 🙃
m
Copy code
WsMessage(body=[2,"6331bde8ece14de7ab7da70a86ac4130","My server response",{"key1":"value1","key2":null}])
onMessage I am receiving this. I have no idea what is first two elements as my server is sending
My server response
so the question is How can I pass it to lense to disrealise? I saw this sample https://www.http4k.org/blog/typesafe_websockets/ for moneylense. Thanks @dave
d
that's very strange - I've got no idea where that array is coming from in your WsMessage - could you post a snippet showing your server code? as you can see from your original example, a WsMessage("hello") will serialise and deserialise correctly. To use a lens with a WsMessage, here is an example:
Copy code
data class Message(val s: String)

val lens = WsMessage.auto<Message>().toLens()

val serialised: WsMessage = lens(Message("foo"))
val deserialised: Message = lens(serialised)
m
@dave I am receiving array of string like this so the question is how can I use lens for de serialising that
Copy code
[2,"6331bde8ece14de7ab7da70a86ac4130","My server response",{"key1":"value1","key2":null}]
d
Just use List<String> in the lens definition should work.
m
@dave How can I get the exception when my socket connection is not successful using WebsocketClient.nonBlocking? as I am providing any invalid url and I am not getting any exception
Another question can I use resilience4j for the websocket client reconnection purpose also?
d
@Mehdi it does look like when the connection is made in a non-blocking way there is a race condition there between adding an error handler. I've added an optional errorHandler parameter to the constructor call, which will go out in the next release.
m
Ok thanks, how about retry connection feature? How we I can reconnect?
d
I'm afraid that it's not currently supported - see: https://github.com/http4k/http4k/issues/154
m
Yes, Indeed. I also requested that😊, what about something like this?
Copy code
un tryToReconect(client:WebSocketClient){
        while (!client.connection.isOpen)
        {
            Timer("SettingUp", false).schedule(60000) {
                client.reconnect()
            }
        }
    }