Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 223
DPD Avatar asked Aug 30 '12 07:08

DPD


People also ask

How do you send data over WebSockets?

To send a message through the WebSocket connection you call the send() method on your WebSocket instance; passing in the data you want to transfer. socket. send(data); You can send both text and binary data through a WebSocket.

Can we send file through WebSocket?

First, APIs are needed to load the file from the file system, then the file is transformed into a suitable payload to be sent over web sockets, and finally, server-side code is required to receive the file.

Can a WebSocket server send message to client?

The Message event takes place usually when the server sends some data. Messages sent by the server to the client can include plain text messages, binary data, or images. Whenever data is sent, the onmessage function is fired.

Can we send JSON data in a WebSocket connection?

To create a WebSocket and open the connection to FME Server use getWebSocketConnection method. The result is an open WebSocket you can use in your application to communicate with FME Server. This example sends a basic JSON object as a string to the server, and displays the response from the server.


2 Answers

Don't forget the query string. It's valid in WebSocket url.

new Websocket('ws://yoursite.com/path?a=1&b=2&c=3') 

Then you can easily parse this url on server side to retrieve the data.

like image 194
Lewis Avatar answered Sep 28 '22 01:09

Lewis


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.
like image 44
simonc Avatar answered Sep 28 '22 02:09

simonc