[CONF] Apache Wicket Wicket Native WebSockets

2013-03-19 Thread confluence







Wicket Native WebSockets
Page edited by Hendy Irawan


 Changes (2)
 




...
 For Jetty 7.5+ this is  
filter-classorg.apache.wicket.protocol.http.Jetty7WebSocketFilter/filter-class filter-classorg.apache.wicket.protocol.ws.jetty7.Jetty7WebSocketFilter/filter-class 
 For Tomcat 7.0.27+: 
filter-classorg.apache.wicket.protocol.http.Tomcat7WebSocketFilter/filter-class filter-classorg.apache.wicket.protocol.ws.tomcat7.Tomcat7WebSocketFilter/filter-class 
 h5. WebSocketBehavior 
...


Full Content



What ?
Why ?
How it works ?
How to use it ?


Maven dependency
Custom WicketFilter
WebSocketBehavior
Client side APIs


Testing
Differences with Wicket-Atmosphere module.
FAQ

Request and session scoped beans do not work.



Wicket Native WebSockets


What ?

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. 
All messages sent through the web socket connection are not intercepted by WicketFilter. This is how the web containers currently work.
When a client is connected it is being registered in a global (application scoped) registry using as a key the application, the client session and the id of the page that registered it. Later when the server needs to push a message it uses this registry to filter which clients need to receive the message.
When a message is received from the client Wicket uses the associated page id for the web connection and loads the Wicket Page as Ajax requests do, then it broadcasts a IWebSocketMessage (Wicket 1.5 Event system) to all its components so they can react on it.
The server-side can push plain text and binary messages to the client - pure web socket messages, but can also add components for re-render, prepend/append _javascript_ as you do with AjaxRequestTarget.

How to use it ?

Maven dependency

Add dependency to either org.apache.wicket:wicket-native-websocket-jetty or org.apache.wicket:wicket-native-websocket-tomcat.

Custom WicketFilter 

Setup the custom WicketFilter implementation for the chosen web container in your web.xml.

For Jetty 7.5+ this is 
  filter-classorg.apache.wicket.protocol.ws.jetty7.Jetty7WebSocketFilter/filter-class

For Tomcat 7.0.27+:
  filter-classorg.apache.wicket.protocol.ws.tomcat7.Tomcat7WebSocketFilter/filter-class

WebSocketBehavior

org.apache.wicket.protocol.ws.api.WebSocketBehavior is much like Ajax behaviors that you may know from earlier versions of Wicket.
Add WebSocketBehavior to the page that will use web socket communication:

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 APIs

By 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 = 

[CONF] Apache Wicket Wicket Native WebSockets

2013-03-19 Thread confluence







Wicket Native WebSockets
Page edited by Hendy Irawan


 Changes (3)
 




...
h5. Maven dependency  
Add dependency to either org.apache.wicket:wicket-native-websocket-jetty9, org.apache.wicket:wicket-native-websocket-jetty, or org.apache.wicket:wicket-native-websocket-tomcat. 
 h5. Custom WicketFilter  
...
Setup the custom WicketFilter implementation for the chosen web container in your web.xml.  
For Jetty 7.5+ 9.0+ this is 
  filter-classorg.apache.wicket.protocol.ws.jetty9.Jetty9WebSocketFilter/filter-class  For Jetty 7.5+ - 8.x this is  
  filter-classorg.apache.wicket.protocol.ws.jetty7.Jetty7WebSocketFilter/filter-class  
...


Full Content



What ?
Why ?
How it works ?
How to use it ?


Maven dependency
Custom WicketFilter
WebSocketBehavior
Client side APIs


Testing
Differences with Wicket-Atmosphere module.
FAQ

Request and session scoped beans do not work.



Wicket Native WebSockets


What ?

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. 
All messages sent through the web socket connection are not intercepted by WicketFilter. This is how the web containers currently work.
When a client is connected it is being registered in a global (application scoped) registry using as a key the application, the client session and the id of the page that registered it. Later when the server needs to push a message it uses this registry to filter which clients need to receive the message.
When a message is received from the client Wicket uses the associated page id for the web connection and loads the Wicket Page as Ajax requests do, then it broadcasts a IWebSocketMessage (Wicket 1.5 Event system) to all its components so they can react on it.
The server-side can push plain text and binary messages to the client - pure web socket messages, but can also add components for re-render, prepend/append _javascript_ as you do with AjaxRequestTarget.

How to use it ?

Maven dependency

Add dependency to either org.apache.wicket:wicket-native-websocket-jetty9, org.apache.wicket:wicket-native-websocket-jetty, or org.apache.wicket:wicket-native-websocket-tomcat.

Custom WicketFilter 

Setup the custom WicketFilter implementation for the chosen web container in your web.xml.

For Jetty 9.0+ this is 
  filter-classorg.apache.wicket.protocol.ws.jetty9.Jetty9WebSocketFilter/filter-class

For Jetty 7.5+ - 8.x this is 
  filter-classorg.apache.wicket.protocol.ws.jetty7.Jetty7WebSocketFilter/filter-class

For Tomcat 7.0.27+:
  filter-classorg.apache.wicket.protocol.ws.tomcat7.Tomcat7WebSocketFilter/filter-class

WebSocketBehavior

org.apache.wicket.protocol.ws.api.WebSocketBehavior is much like Ajax behaviors that you may know from earlier versions of Wicket.
Add WebSocketBehavior to the page that will use web socket communication:

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 APIs

By adding a