Wicket Native WebSocketsPage edited by Hendy IrawanChanges (2)
Full ContentWicket Native WebSocketsWhat ?Since version 6.0 Wicket provides an experimental module that provides integration with WebSocket support in the web containers like Jetty and Tomcat. Why ?Martin Grigorov looked at Typesafe's Console and the support for WebSockets in Play Framework as a whole and I decided that it will be a good addition to WicketStuff HTML5 project. Later Martijn Dashorst suggested that such projects should be added to Apache Wicket as experimental modules and when they become more mature they can be promoted as production ready, or if they are not used/liked then they can be moved to WicketStuff project. How it works ?Each of the integrations provide a custom implementation of org.apache.wicket.protocol.http.WicketFilter that first checks whether the current web request needs to upgrade its protocol from HTTP to WebSocket. This is done by checking the request headers for the special header with name "Upgrade". If the request is an upgrade then the filter registers an endpoint for the web socket connection. Later this endpoint is used to read/write messages from/to the client. How to use it ?Maven dependencyAdd dependency to either org.apache.wicket:wicket-native-websocket-jetty or org.apache.wicket:wicket-native-websocket-tomcat. Custom WicketFilterSetup the custom WicketFilter implementation for the chosen web container in your web.xml. For Jetty 7.5+ this is For Tomcat 7.0.27+: WebSocketBehaviororg.apache.wicket.protocol.ws.api.WebSocketBehavior is much like Ajax behaviors that you may know from earlier versions of Wicket. MyPage.java public class MyPage extends WebPage { public MyPage() { add(new WebSocketBehavior() { @Override protected void onMessage(WebSocketRequestHandler handler, TextMessage message) { } }); } } Use message.getText() to read the message sent by the client and use the passed handler.push(String) to push a text message to the connected client. Additionally you can use handler.add(Component...) to add Wicket components for re-render, handler#prependJavaScript(CharSequence) and handler#appendJavaScript(CharSequence) as you do with AjaxRequestTarget. See the demo application at martin-g's GitHub. It is written with Scala and uses Akka which are not available at Maven central repository so it cannot be hosted at Apache Git servers. Client side APIsBy adding a WebSocketBehavior to your component(s) Wicket will contribute wicket-websocket-jquery.js library which provides some helper functions to write your client side code. There is a default websocket connection per Wicket Page opened for you which you can use like Wicket.WebSocket.send('{msg: "my message"}'). If you need more WebSocket connections then you can do: var ws = new Wicket.WebSocket(); ws.send('message'); To close a connection: Wicket.WebSocket.close() or ws.close(). Wicket.WebSocket is a simple wrapper around the native window.WebSocket API which is used to intercept the calls and to fire special events (Wicket.Event PubSub).
If you don't need to listen for these events then you can just use the native _javascript_ API (window.WebSocket). A demo code can be seen in the Demo Application TestingThe module provides org.apache.wicket.protocol.ws.util.tester.WebSocketTester which gives you the possibility to emulate sending and receiving messages without the need to run in a real web container, as WicketTester does this for HTTP requests. Check WebSocketTesterTest for an example. Differences with Wicket-Atmosphere module.Wicket-Atmosphere provides integration with Atmosphere and let it handle the inconsistencies in WebSocket protocol support in different browsers and web containers. If either the browser or the web container do not support WebSockets then Atmosphere will downgrade (depending on the configuration) to either long-polling, streaming, server-side events, jsonp, ... to simulate the long running connection. Wicket Native WebSocket uses only WebSocket connections, so it wont work for browsers and web containers which do not support WebSockets. There are no plans to add such support in future. Use it only if you really know that you will run your application in an environment that supports WebSockets. Currently supported web containers are Jetty 7.5+ and Tomcat 7.0.27+. FAQRequest and session scoped beans do not work.The Web Socket communication is not processed by Servlet Filters and Listeners and thus the Dependency Injection libraries have no chance to export the request and session beans.
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Wicket > Wicket Native WebSockets confluence