Search code examples
htmlwebsocketjetty-8

is it possible to send a data when a websocket connection is opened


I am implementing a Jetty Websocket servlet. When the server receives a new connection, I want to send a message that will be read by websocket's onopen function. I want this message to be sent only during the open and not using the regular connection.SendMessage() function. Is it possible to do that? and how?


Solution

  • There is no support for this in the protocol but you could fudge something yourself.

    • When your server completes a handshake, store the initial message you want to deliver to a client.
    • In your client's onopen function, send a "read initial message" request.
    • In your server, check that this client hasn't read its initial message; respond with the message; set a flag saying that the initial message has been sent.
    • Your client and server are both now free to send other messages.