Skip to content
Browse files

Update to the new client-side API (Wicket.WebSocket)

Improve the messages pushed by the server.
Fix some warnings in Start.scala
  • Loading branch information...
1 parent 50e5cc7 commit 51e5a922ff9e35497e9ffe04949c3e7d2e832d91 @martin-g committed Jun 20, 2012
View
20 src/main/resources/org/apache/wicket/websocket/jetty/example/client.js
@@ -17,19 +17,16 @@
jQuery(function($) {
- var ws = null;
-
Wicket.Event.subscribe("/websocket/open", function(jqEvent) {
$('#connexion').hide();
$('#sentMessages').show();
});
- Wicket.Event.subscribe("/websocket/message", function(jqEvent, data) {
- $('#messages').prepend('<span>' + data + '</span><br/>');
+ Wicket.Event.subscribe("/websocket/message", function(jqEvent, message) {
+ $('#messages').prepend('<span>' + message + '</span><br/>');
});
var close = function(jqEvent) {
- ws = null;
$('#sentMessages').hide();
$('#connexion').show();
$('#messages').empty();
@@ -39,21 +36,16 @@ jQuery(function($) {
Wicket.Event.subscribe("/websocket/error", close);
$('#connect').click(function() {
-
- ws = new Wicket.WebSocket();
+ Wicket.WebSocket.createDefaultConnection();
});
$('#send').click(function() {
- if (ws) {
- ws.send($('#message').val());
- $('#message').val('');
- }
+ Wicket.WebSocket.send($('#message').val());
+ $('#message').val('');
});
$('#disconnect').click(function() {
- if (ws) {
- ws.close();
- }
+ Wicket.WebSocket.close();
});
});
View
2 src/main/scala/org/apache/wicket/websocket/jetty/example/EventSystem.scala
@@ -138,7 +138,7 @@ class EventSystem(val application: Application)
val application = Application.get(c.applicationName)
val connectionRegistry = new SimpleWebSocketConnectionRegistry();
val webSocketConnection = connectionRegistry.getConnection(application, c.sessionId, c.pageId)
- webSocketConnection.sendMessage("Pushed asynchronous message from Akka!")
+ webSocketConnection.sendMessage("A message pushed asynchronously by Akka directly to the plain WebSocketConnection!")
})
}
}
View
28 src/main/scala/org/apache/wicket/websocket/jetty/example/WebSocketDemo.scala
@@ -1,20 +1,21 @@
package org.apache.wicket.websocket.jetty.example
-import org.apache.wicket.markup.head.{IHeaderResponse, JavaScriptHeaderItem}
+import org.apache.wicket.markup.head.{HeaderItem, IHeaderResponse, JavaScriptHeaderItem}
import org.apache.wicket.markup.html.WebPage
import org.apache.wicket.request.resource.PackageResourceReference
import org.apache.wicket.ajax.markup.html.AjaxLink
import org.apache.wicket.ajax.{AjaxRequestTarget, WebSocketRequestHandler}
import org.apache.wicket.markup.html.panel.FeedbackPanel
-import org.apache.wicket.protocol.ws.api.{SimpleWebSocketConnectionRegistry, WebSocketBehavior}
+import org.apache.wicket.protocol.ws.api.{WicketWebSocketJQueryResourceReference, SimpleWebSocketConnectionRegistry, WebSocketBehavior}
import org.apache.wicket.protocol.ws.api.message.{ClosedMessage, ConnectedMessage, TextMessage}
+import java.util
/**
- * A (ugly) demo page for WebSocketBehavior
+ * A demo page for native WebSocket support
*/
class WebSocketDemo extends WebPage {
- val feedback = new FeedbackPanel("feedback");
+ val feedback = new FeedbackPanel("feedback")
feedback.setOutputMarkupId(true)
add(feedback)
@@ -51,7 +52,7 @@ class WebSocketDemo extends WebPage {
{
getSession.info("You typed: " + data.getText)
handler.add(feedback)
- handler.push("A message pushed by the server via WebSocketRequestHandler!")
+ handler.push("A message pushed by the server by using the WebSocketRequestHandler that is available in WebSocketBehavior#onMessage!")
}
})
@@ -66,7 +67,7 @@ class WebSocketDemo extends WebPage {
if (connection != null)
{
val webSocketHandler = new WebSocketRequestHandler(this, connection)
- webSocketHandler.push("A message pushed by clicking a normal AjaxLink")
+ webSocketHandler.push("A message pushed by creating WebSocketRequestHandler manually in an Ajax request")
}
getSession.info("AjaxLink clicked")
target.add(feedback)
@@ -77,7 +78,18 @@ class WebSocketDemo extends WebPage {
{
super.renderHead(response)
- response.render(JavaScriptHeaderItem.forReference(
- new PackageResourceReference(classOf[WebSocketDemo], "client.js")))
+ response.render(JavaScriptHeaderItem.forReference(new ClientResourceReference))
+ }
+
+ /**
+ * A custom resource reference that depends on WicketWebSocketJQueryResourceReference
+ */
+ private class ClientResourceReference extends PackageResourceReference(classOf[WebSocketDemo], "client.js")
+ {
+ override def getDependencies: util.ArrayList[HeaderItem] = {
+ val list = new util.ArrayList[HeaderItem]()
+ list.add(JavaScriptHeaderItem.forReference(WicketWebSocketJQueryResourceReference.get()))
+ list
+ }
}
}
View
38 src/test/scala/org/apache/wicket/Start.scala
@@ -6,42 +6,42 @@ import org.eclipse.jetty.webapp.WebAppContext
object Start extends App {
- val server = new Server();
- val connector = new SelectChannelConnector();
+ val server = new Server()
+ val connector = new SelectChannelConnector()
// Set some timeout options to make debugging easier.
- connector.setMaxIdleTime(1000 * 60 * 60);
- connector.setSoLingerTime(-1);
- connector.setPort(8080);
- server.setConnectors(Array(connector));
+ connector.setMaxIdleTime(1000 * 60 * 60)
+ connector.setSoLingerTime(-1)
+ connector.setPort(8080)
+ server.setConnectors(Array(connector))
- val bb = new WebAppContext();
- bb.setServer(server);
- bb.setContextPath("/");
- bb.setWar("src/main/webapp");
+ val bb = new WebAppContext()
+ bb.setServer(server)
+ bb.setContextPath("/")
+ bb.setWar("src/main/webapp")
// START JMX SERVER
// MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
// MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
// server.getContainer().addEventListener(mBeanContainer);
// mBeanContainer.start();
- server.setHandler(bb);
+ server.setHandler(bb)
try {
- System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
- server.start();
- System.in.read();
- System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
+ System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP")
+ server.start()
+ System.in.read()
+ System.out.println(">>> STOPPING EMBEDDED JETTY SERVER")
// while (System.in.available() == 0) {
// Thread.sleep(5000);
// }
- server.stop();
- server.join();
+ server.stop()
+ server.join()
} catch {
case e: Exception => {
- e.printStackTrace();
- System.exit(100);
+ e.printStackTrace()
+ System.exit(100)
}
}
}

0 comments on commit 51e5a92

Please sign in to comment.
Something went wrong with that request. Please try again.