Permalink
Browse files

add new jboss-websocket support and remove embedded jetty

  • Loading branch information...
1 parent 1871195 commit edf48c3743460836dd627ba540880e9132d36de6 @wesleyhales committed May 11, 2012
View
125 pom.xml
@@ -19,7 +19,7 @@
<!-- Timestamp format for the maven.build.timestamp property -->
<!-- You can reference property in pom.xml or filtered resources (must
enable third-party plugin if using Maven < 2.1) -->
- <javaee6.spec.version>3.0.0.Beta1</javaee6.spec.version>
+ <javaee6.spec.version>3.0.0.Final</javaee6.spec.version>
<!-- Alternatively, comment out the above line, and un-comment the line below to
use version 3.0.0.Beta1-redhat-1 which is a release certified
to work with JBoss EAP 6. It requires you have access to the JBoss EAP 6 maven repository. -->
@@ -30,7 +30,19 @@
<jettyVersion>8.1.0.RC2</jettyVersion>
</properties>
-
+ <distributionManagement>
+ <repository>
+ <id>jboss-releases-repository</id>
+ <name>JBoss Releases Repository</name>
+ <url>https://repository.jboss.org/nexus/service/local/staging/deploy/maven2/</url>
+ </repository>
+
+ <snapshotRepository>
+ <id>jboss-snapshots-repository</id>
+ <name>JBoss Snapshots Repository</name>
+ <url>https://repository.jboss.org/nexus/content/repositories/snapshots/</url>
+ </snapshotRepository>
+ </distributionManagement>
<dependencyManagement>
<dependencies>
<!-- JBoss distributes a complete set of Java EE 6 APIs including
@@ -61,6 +73,8 @@
<scope>provided</scope>
</dependency>
+
+
<!-- Import the Common Annotations API (JSR-250), we use provided scope
as the API is included in JBoss -->
<dependency>
@@ -145,43 +159,92 @@
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-continuation</artifactId>
- <version>${jettyVersion}</version>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-http</artifactId>
- <version>${jettyVersion}</version>
- </dependency>
+ <dependency>
+ <groupId>org.jboss.as</groupId>
+ <artifactId>jboss-as-websockets</artifactId>
+ <version>0.2.Alpha1</version>
+ </dependency>
<dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-io</artifactId>
- <version>${jettyVersion}</version>
- </dependency>
+ <groupId>org.jboss.as</groupId>
+ <artifactId>jboss-as-server</artifactId>
+
+ <!-- Use the locally compiled snapshot for now
+ TODO: don't hard code this.-->
+ <version>7.1.2.Final-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-server</artifactId>
- <version>${jettyVersion}</version>
- </dependency>
<dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-util</artifactId>
- <version>${jettyVersion}</version>
- </dependency>
+ <groupId>org.jboss.web</groupId>
+ <artifactId>jbossweb</artifactId>
+ <scope>provided</scope>
+ <!-- TODO: don't hard code this -->
+ <version>7.0.16.Final</version>
+ </dependency>
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-websocket</artifactId>
- <version>${jettyVersion}</version>
- </dependency>
+ <dependency>
+ <groupId>org.jboss.spec.javax.servlet</groupId>
+ <artifactId>jboss-servlet-api_3.0_spec</artifactId>
+ <version>1.0.0.Final</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <!--<dependency>-->
+ <!--<groupId>org.eclipse.jetty</groupId>-->
+ <!--<artifactId>jetty-continuation</artifactId>-->
+ <!--<version>${jettyVersion}</version>-->
+ <!--</dependency>-->
+
+ <!--<dependency>-->
+ <!--<groupId>org.eclipse.jetty</groupId>-->
+ <!--<artifactId>jetty-http</artifactId>-->
+ <!--<version>${jettyVersion}</version>-->
+ <!--</dependency>-->
+
+ <!--<dependency>-->
+ <!--<groupId>org.eclipse.jetty</groupId>-->
+ <!--<artifactId>jetty-io</artifactId>-->
+ <!--<version>${jettyVersion}</version>-->
+ <!--</dependency>-->
+
+ <!--<dependency>-->
+ <!--<groupId>org.eclipse.jetty</groupId>-->
+ <!--<artifactId>jetty-server</artifactId>-->
+ <!--<version>${jettyVersion}</version>-->
+ <!--</dependency>-->
+
+ <!--<dependency>-->
+ <!--<groupId>org.eclipse.jetty</groupId>-->
+ <!--<artifactId>jetty-util</artifactId>-->
+ <!--<version>${jettyVersion}</version>-->
+ <!--</dependency>-->
+
+ <!--<dependency>-->
+ <!--<groupId>org.eclipse.jetty</groupId>-->
+ <!--<artifactId>jetty-websocket</artifactId>-->
+ <!--<version>${jettyVersion}</version>-->
+ <!--</dependency>-->
</dependencies>
+ <repositories>
+ <repository>
+ <id>jboss-public-repository-group</id>
+ <name>JBoss Public Repository Group</name>
+ <url>http://repository.jboss.org/nexus/content/groups/public/</url>
+ <layout>default</layout>
+ <releases>
+ <enabled>true</enabled>
+ <updatePolicy>never</updatePolicy>
+ </releases>
+ <snapshots>
+ <enabled>true</enabled>
+ <updatePolicy>never</updatePolicy>
+ </snapshots>
+ </repository>
+ </repositories>
+
<build>
<!-- Maven will append the version to the finalName (which is the name
given to the generated war, and hence the context root) -->
@@ -5,8 +5,6 @@
import java.util.logging.Logger;
import javax.ejb.Stateful;
-import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -18,10 +16,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import org.eclipse.jetty.util.ConcurrentHashSet;
import org.jboss.as.quickstarts.html5_mobile.model.Member;
-import org.jboss.as.quickstarts.html5_mobile.websockets.ChatWebSocketHandler;
-import org.jboss.as.quickstarts.html5_mobile.websockets.Notify;
/**
* JAX-RS Example
@@ -1,84 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2012, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.as.quickstarts.html5_mobile.websockets;
-
-
-import org.eclipse.jetty.server.Connector;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.handler.DefaultHandler;
-import org.eclipse.jetty.server.nio.SelectChannelConnector;
-import org.eclipse.jetty.util.ConcurrentHashSet;
-
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
-import java.net.InetAddress;
-import java.util.Set;
-
-/**
- * @author <a href="mailto:whales@redhat.com">Wesley Hales</a>
- */
-public class ChatServerServletContextListener implements ServletContextListener {
-
- private Server server = null;
- /**
- * Start Embedding Jetty server when WEB Application is started.
- *
- */
- public void contextInitialized(ServletContextEvent event) {
- try {
- // 1) Create a Jetty server with the 8081 port.
- InetAddress addr = InetAddress.getLocalHost();
- this.server = new Server();
- Connector connector = new SelectChannelConnector();
- connector.setPort(8081);
- connector.setHost(addr.getHostAddress());
-
- server.addConnector(connector);
-
- // 2) Register ChatWebSocketHandler in the Jetty server instance.
- ChatWebSocketHandler chatWebSocketHandler = new ChatWebSocketHandler();
- chatWebSocketHandler.setHandler(new DefaultHandler());
-
- server.setHandler(chatWebSocketHandler);
-
- // 2) Start the Jetty server.
- server.start();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Stop Embedding Jetty server when WEB Application is stopped.
- */
- public void contextDestroyed(ServletContextEvent event) {
- if (server != null) {
- try {
- // stop the Jetty server.
- server.stop();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
-}
@@ -21,18 +21,15 @@
*/
package org.jboss.as.quickstarts.html5_mobile.websockets;
-import org.eclipse.jetty.util.ConcurrentHashSet;
-import org.eclipse.jetty.websocket.WebSocket;
-import org.eclipse.jetty.websocket.WebSocketHandler;
-import org.eclipse.jetty.websocket.WebSocketServlet;
+
import org.jboss.as.quickstarts.html5_mobile.model.Member;
-import org.jboss.as.quickstarts.html5_mobile.rest.MemberService;
+import org.jboss.as.websockets.WebSocket;
+import org.jboss.as.websockets.servlet.WebSocketServlet;
+import org.jboss.websockets.Frame;
+import org.jboss.websockets.frame.TextFrame;
-import javax.annotation.PostConstruct;
-import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
-import javax.inject.Inject;
-import javax.inject.Singleton;
+import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
@@ -44,69 +41,60 @@
/**
* @author <a href="mailto:whales@redhat.com">Wesley Hales</a>
*/
-public class ChatWebSocketHandler extends WebSocketHandler {
-
- private static Set<ChatWebSocket> websockets = new ConcurrentHashSet<ChatWebSocket>();
- public WebSocket doWebSocketConnect(HttpServletRequest request,
- String protocol) {
- return new ChatWebSocket();
- }
+@WebServlet("/websocket/")
+public class ChatWebSocketHandler extends WebSocketServlet {
+ private static Set<WebSocket> websockets = new HashSet<WebSocket>();
public void observeItemEvent(@Observes Member member) {
try {
- for (ChatWebSocket webSocket : getWebsockets()) {
- webSocket.connection.sendMessage("{\"cdievent\":{\"fire\":function(){" +
- "eventObj.initEvent(\'memberEvent\', true, true);" +
- "eventObj.name = '" + member.getName() + "';\n" +
- "document.dispatchEvent(eventObj);" +
- "}}}");
+ for (WebSocket socket : getWebsockets()) {
+ socket.writeFrame(TextFrame.from("{\"cdievent\":{\"fire\":function(){" +
+ "eventObj.initEvent(\'memberEvent\', true, true);" +
+ "eventObj.name = '" + member.getName() + "';\n" +
+ "document.dispatchEvent(eventObj);" +
+ "}}}"));
}
} catch (IOException x) {
//todo - do something
}
}
+ @Override
+ protected void onSocketOpened(WebSocket socket) throws IOException {
+ System.out.println("Websocket opened :)");
+ websockets.add(socket);
+ }
- public class ChatWebSocket implements WebSocket.OnTextMessage {
-
- private Connection connection;
-
- public void onOpen(Connection connection) {
- // Client (Browser) WebSockets has opened a connection.
- // 1) Store the opened connection
- this.connection = connection;
- // 2) Add ChatWebSocket in the global list of ChatWebSocket
- // instances
- // instance.
- getWebsockets().add(this);
- }
+ @Override
+ protected void onSocketClosed(WebSocket socket) throws IOException {
+ System.out.println("Websocket closed :(");
+ websockets.remove(socket);
+ }
- public void onMessage(String data) {
- // Loop for each instance of ChatWebSocket to send message server to
- // each client WebSockets.
+ @Override
+ protected void onReceivedFrame(WebSocket socket) throws IOException {
+ final Frame frame = socket.readFrame();
+ if (frame instanceof TextFrame) {
+ final String text = ((TextFrame) frame).getText();
+ if ("Hello".equals(text)) {
+ socket.writeFrame(TextFrame.from("Hey, there!"));
+ }
try {
- for (ChatWebSocket webSocket : getWebsockets()) {
+ for (WebSocket asocket : getWebsockets()) {
+ System.out.println("!!!!!!!!!!!!!!!!!!" + asocket.getSocketID());
// send a message to the current client WebSocket.
- webSocket.connection.sendMessage(data);
+ asocket.writeFrame(TextFrame.from(text));
}
} catch (IOException x) {
// Error was detected, close the ChatWebSocket client side
- this.connection.disconnect();
+ System.out.println("!!!!!!!!!!!!!!!!!!huge problem");
}
-
- }
-
- public void onClose(int closeCode, String message) {
- // Remove ChatWebSocket in the global list of ChatWebSocket
- // instance.
- getWebsockets().remove(this);
}
}
- public static synchronized Set<ChatWebSocket> getWebsockets() {
+ public static synchronized Set<WebSocket> getWebsockets() {
return websockets;
}
-
}
Oops, something went wrong.

0 comments on commit edf48c3

Please sign in to comment.