To learn and understand WebSocket operation in Vertx I have taken the reference of the blog
The codebase can be accessed
The starting point of this app is index.html which makes websocket request with the help of Vertx-Eventbus. To use Vertx Eventbus we have used here a js for sending the request through Eventbus.
The request is being sent with the help of a js function sendData which is a part of realtime-auctions.js and which is invoked while clicking the Button with the value bid.
In realtime-auctions.js there is a function registerHandlerForUpdateCurrentPriceAndFeed() where we have created a EventBus and registered a Handler to receive events.
When this function is triggered during onload of index.html, the handler for the path "/eventbus/*" (as described in SockJSVerticle.groovy) is invoked because of:
Now we can say that the Event Bus Handler is ready to send and receive messages.
So on clicking the bid button from the index.html data 'price': '0007' is being sent to the Handler as described above and the BridgeEventType.SEND event is published and from there we are again publishing data to the eventbus and that is received by the handler registered during html page onload and which makes the necessary changes within the page.
Similarly we have also registered another Handler:
So once the client side handlers are being registered, then those are capable of receiving messages as published from the server end, provided the eventbus name which is here 'auction' needs to be mentioned correctly. So, If we need to send and receive data over a different eventbus Say, for example 'auction2' then we need to register handler at for this eventbus both at the client and server end separately to send and receive data.
Similarly, while sending request data also, Eventbus with the same name (i.e. aution) is being registered at the sever end end with the help of BridgeOptions as used in SockJSHandler which then is used to intercept the request data.
Blog
.The codebase can be accessed
HERE
The starting point of this app is index.html which makes websocket request with the help of Vertx-Eventbus. To use Vertx Eventbus we have used here a js for sending the request through Eventbus.
The request is being sent with the help of a js function sendData which is a part of realtime-auctions.js and which is invoked while clicking the Button with the value bid.
In realtime-auctions.js there is a function registerHandlerForUpdateCurrentPriceAndFeed() where we have created a EventBus and registered a Handler to receive events.
When this function is triggered during onload of index.html, the handler for the path "/eventbus/*" (as described in SockJSVerticle.groovy) is invoked because of:
router.route("/eventbus/*").handler(eventBusHandler())
and the BridgeEventType.SOCKET_CREATED event is published. Now we can say that the Event Bus Handler is ready to send and receive messages.
So on clicking the bid button from the index.html data 'price': '0007' is being sent to the Handler as described above and the BridgeEventType.SEND event is published and from there we are again publishing data to the eventbus and that is received by the handler registered during html page onload and which makes the necessary changes within the page.
Similarly we have also registered another Handler:
router.route("/api/test").handler(new EventBusHandler().&generateSockData)
So, when we access the URL http://host:port/context/api/test it independently publish data to the event bus and that too being received by the handler at the html page in the same way as described above.So once the client side handlers are being registered, then those are capable of receiving messages as published from the server end, provided the eventbus name which is here 'auction' needs to be mentioned correctly. So, If we need to send and receive data over a different eventbus Say, for example 'auction2' then we need to register handler at for this eventbus both at the client and server end separately to send and receive data.
Similarly, while sending request data also, Eventbus with the same name (i.e. aution) is being registered at the sever end end with the help of BridgeOptions as used in SockJSHandler which then is used to intercept the request data.
No comments:
Post a Comment