Websockets has always been an interesting subject to me and I always believe there is more to explore. Every time I spend time with it accompanied with information from various other beautiful, informative blogs and posts I learn something new, and when it is complemented by Vert.x toolkit it becomes more interesting.
In my Previous Post, We discussed about implementing WebSockets with SOCKJS and used SOCKJS handler at the server side along with SOCKJS event bus at the client end.
In this post we will look forward to explore how WebSocket Handler handles requests and generates responses with the help of Web Socket protocol. Here is the GITHUB CODEBASE for reference.
We are citing an exmaple to see things in action.
Within SockJSVerticle the request handler for path /sendToSocket is the main Actor in Action here.
Within the Handler we have perfomrmed the following Tasks:
1) Creating an HttpClient and opening WebSocket Stream with the Client.
which is MUST MUST for any Handler handling WebSocket requests, as the handle() method which has a ServerWebSocket is used to receive requests as well as create and sent reponses too (Here we have used WebSocketFrame to do the same).
function. This function is being used to send the response from the request that we have received in the handle method of WebSocketFrameHandler
function, this function sends the data with the help of ServerWebSocket as the definition of the function contains:
Please share your comments and feedback.
In my Previous Post, We discussed about implementing WebSockets with SOCKJS and used SOCKJS handler at the server side along with SOCKJS event bus at the client end.
In this post we will look forward to explore how WebSocket Handler handles requests and generates responses with the help of Web Socket protocol. Here is the GITHUB CODEBASE for reference.
We are citing an exmaple to see things in action.
Within SockJSVerticle the request handler for path /sendToSocket is the main Actor in Action here.
Within the Handler we have perfomrmed the following Tasks:
1) Creating an HttpClient and opening WebSocket Stream with the Client.
HttpClient httpClient = vertx.createHttpClient(new HttpClientOptions()) httpClient.websocketStream(8084,"localhost","/sendToSocket")2) Writing the Data to be sent to with a Websocket.
handler({websocket->.......
...........
.write(Buffer.buffer("Send Data to Server...".toUpperCase().getBytes()))
......
3) Using the Websocket to receive data with the help of WebSocketFrame.
.frameHandler({webSocketFrame -> println "The Data at the Client Socket is:${webSocketFrame.textData()}"})
Now when we write data to the Websocket as described in Point 2, the control passes on to the WebSocketHandler that we have already registered with the Vert.x server in SockJSVerticle.groovyhttpServer1.websocketHandler(new WebSocketHandler());If we peek into this Handler we can see that it implements Handler
.frameHandler(new WebSocketFrameHandler({buf->serverWebSocket.writeBinaryMessage(buf)}))
Within WebSocketFrameHandler we have passed Consumer
void handle(WebSocketFrame frame) {
println "The Data at the Server Socket is:${frame.textData()}"
consumerbuffer.accept(frame.binaryData())
}
The data that is sent from handler method of the path /sendToSocket is received by WebSocketFrame at the handle() method of WebSocketFrameHandler and which is being sent again with the help of the Consumer
{buf->serverWebSocket.writeBinaryMessage(buf)}...
This is in short about WebSocket.Please share your comments and feedback.
No comments:
Post a Comment