Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 3255e27230952d31a0090bcae5681b600e06cf56 @matzew committed Dec 4, 2010
12 README
@@ -0,0 +1,12 @@
+See WebSocket with Jetty 8.
+
+Simply run 'mvn' (tested with Maven3) and wait for Jetty to launch on localhost:9090
+
+Now point your *modern* browser (e.g. Chromium) to http://localhost:9090/talkToServer.html
+
+That's it...
+
+There is only one Java class. Internally it simply dispatches the incoming message back to the sending
+client.
+
+Have fun!
196 pom.xml
@@ -0,0 +1,196 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (C) 2010 Matthias Weßendorf.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>net.wessendorf.enterprise</groupId>
+ <artifactId>jetty-websocket</artifactId>
+ <packaging>war</packaging>
+ <version>1.0</version>
+ <name>jetty-websocket</name>
+ <description>Very simple Jetty/WebSocket demo</description>
+ <url>http://matthiaswessendorf.wordpress.com</url>
+ <build>
+ <defaultGoal>jetty:run</defaultGoal>
+ <!--
+export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000"
+-->
+ <plugins>
+ <!-- assembly -->
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <configuration>
+ <descriptorRefs>
+ <descriptorRef>src</descriptorRef>
+ </descriptorRefs>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty-maven-plugin</artifactId>
+ <version>8.0.0.M2</version>
+ <configuration>
+ <scanIntervalSeconds>10</scanIntervalSeconds>
+ <webAppConfig>
+ <contextPath>/</contextPath>
+ </webAppConfig>
+ <connectors>
+ <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
+ <port>9090</port>
+ <maxIdleTime>60000</maxIdleTime>
+ </connector>
+ </connectors>
+ </configuration>
+ </plugin>
+ </plugins>
+
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ <encoding>${project.build.sourceEncoding}</encoding>
+ <showWarnings>true</showWarnings>
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-websocket</artifactId>
+ <version>8.0.0.M2</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- Apache OpenWebBeans -->
+ <dependency>
+ <groupId>org.apache.openwebbeans</groupId>
+ <artifactId>openwebbeans-impl</artifactId>
+ <version>${openwebbeans.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.openwebbeans</groupId>
+ <artifactId>openwebbeans-web</artifactId>
+ <version>${openwebbeans.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.openwebbeans</groupId>
+ <artifactId>openwebbeans-resource</artifactId>
+ <version>${openwebbeans.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.openwebbeans</groupId>
+ <artifactId>openwebbeans-spi</artifactId>
+ <version>${openwebbeans.version}</version>
+ </dependency>
+
+ <!-- some JavaEE 6 artifacts -->
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-interceptor_1.1_spec</artifactId>
+ <version>${geronimo_interceptor.version}</version>
+ <!--scope>runtime</scope -->
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-jta_1.1_spec</artifactId>
+ <version>1.1.1</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-jcdi_1.0_spec</artifactId>
+ <version>${geronimo_cdi.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-atinject_1.0_spec</artifactId>
+ <version>${geronimo_atinject.version}</version>
+ </dependency>
+
+ <!-- This is a lame dependency, required by the JSR 299 specification.
+ Not the fault of Apache OWB, but we have to have this here in order to be
+ able to use Apache OWB outside of the typical EE realm. (Yes here in Jetty). -->
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-validation_1.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>runtime</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.annotation</groupId>
+ <artifactId>jsr250-api</artifactId>
+ <version>1.0</version>
+ </dependency>
+
+ <!-- Apache Derby Database -->
+ <dependency>
+ <groupId>org.apache.derby</groupId>
+ <artifactId>derby</artifactId>
+ <version>10.6.2.1</version>
+ </dependency>
+
+ </dependencies>
+
+ <repositories>
+ <!-- I do not understand why there are two repositories for glassfish stuff -->
+ <repository>
+ <id>glassfish-repository.dev.java.net</id>
+ <name>Java.net Repository for Maven</name>
+ <url>http://download.java.net/maven/glassfish/</url>
+ </repository>
+ <repository>
+ <id>javaNet-repository.dev.java.net</id>
+ <name>Java.net Repository for Maven</name>
+ <url>http://download.java.net/maven/2/</url>
+ </repository>
+
+ <!-- JBoss stuff... -->
+ <repository>
+ <id>jboss</id>
+ <name>jboss nexus</name>
+ <url>http://repository.jboss.org/nexus/content/groups/public-jboss/</url>
+ </repository>
+
+ </repositories>
+
+ <properties>
+ <myfaces.version>2.0.2</myfaces.version>
+ <myfaces.codi.version>0.9.0</myfaces.codi.version>
+ <openwebbeans.version>1.0.0</openwebbeans.version>
+ <geronimo_interceptor.version>1.0</geronimo_interceptor.version>
+ <geronimo_cdi.version>1.0</geronimo_cdi.version>
+ <geronimo_jpa.version>1.1.1</geronimo_jpa.version>
+ <geronimo_atinject.version>1.0</geronimo_atinject.version>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+</project>
@@ -0,0 +1,84 @@
+package net.wessendorf.jetty.websocket;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.eclipse.jetty.websocket.WebSocket;
+import org.eclipse.jetty.websocket.WebSocketServlet;
+
+/**
+ * Simple 'ping' servlet...
+ *
+ * @author matzew
+ */
+public final class EntryServlet extends WebSocketServlet
+{
+ private static final long serialVersionUID = 1L;
+ private final Set<PingWebSocket> connectedClients = new CopyOnWriteArraySet<PingWebSocket>();
+
+ /**
+ * Doing the upgrade of the http request
+ */
+ @Override
+ protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
+ {
+ return new PingWebSocket();
+ }
+
+ /**
+ * Here happens the _real_ communication, outside of vanilla HTTP...
+ */
+ private class PingWebSocket implements WebSocket
+ {
+ Outbound outbound;
+ public void onConnect(Outbound outbound)
+ {
+ // register me, after successful 'upgrade'
+ this.outbound = outbound;
+ connectedClients.add(this);
+ }
+
+ public void onDisconnect()
+ {
+ connectedClients.remove(this);
+ }
+
+ public void onMessage(byte frame, String data)
+ {
+ //ping it back...
+ try
+ {
+ this.outbound.sendMessage("You said: " + data);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+
+ // talk to ALL registered clients (windows)
+// for (PingWebSocket ws : connectedClients)
+// {
+// try
+// {
+// ws.outbound.sendMessage(frame, "You said: " + data);
+// }
+// catch (IOException e)
+// {
+// e.printStackTrace();
+// }
+// }
+ }
+
+ public void onFragment(boolean arg0, byte arg1, byte[] arg2, int arg3,
+ int arg4)
+ {
+ }
+
+ public void onMessage(byte arg0, byte[] arg1, int arg2, int arg3)
+ {
+ }
+ }
+}
@@ -0,0 +1,33 @@
+<?xml version="1.0"?>
+<!--
+ Copyright (C) 2010 Matthias Weßendorf
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
+ version="3.0">
+
+
+ <servlet>
+ <servlet-name>WS</servlet-name>
+ <servlet-class>net.wessendorf.jetty.websocket.EntryServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>WS</servlet-name>
+ <url-pattern>/talk/*</url-pattern>
+ </servlet-mapping>
+
+</web-app>
@@ -0,0 +1,38 @@
+<html>
+ <head>
+ <title>Talk to me...</title>
+
+ <script>
+ var globalWebSocket = null;
+ globalWebSocket = new WebSocket("ws://localhost:9090/talk/tome");
+ globalWebSocket.onmessage = function(evt)
+ {
+ alert("Server told us: => " + evt.data);
+ };
+ globalWebSocket.onclose = function(evt)
+ {
+ alert("Channel has been closed: " + new Date());
+ };
+ function sendText(element)
+ {
+ var txt = element.value;
+ element.value = "";
+ globalWebSocket.send(txt);
+ }
+ function disconnect()
+ {
+ globalWebSocket.close();
+ }
+ </script>
+ </head>
+
+ <body>
+
+ <p>Type in a few words...</p>
+
+ <input onchange="sendText(this);" id="simpleField" />
+
+ <p>... and tab out...</p>
+
+ </body>
+</html>

0 comments on commit 3255e27

Please sign in to comment.