File tree Expand file tree Collapse file tree 11 files changed +356
-0
lines changed
src/main/java/com/hmkcode
websocket-glassfish-server Expand file tree Collapse file tree 11 files changed +356
-0
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <classpath >
3+ <classpathentry kind =" src" path =" src/main/java" including =" **/*.java" />
4+ <classpathentry kind =" src" path =" src/main/resources" excluding =" **/*.java" />
5+ <classpathentry kind =" output" path =" target/classes" />
6+ <classpathentry kind =" var" path =" M2_REPO/javax/activation/activation/1.1/activation-1.1.jar" />
7+ <classpathentry kind =" var" path =" M2_REPO/javax/websocket/javax.websocket-api/1.0-rc5/javax.websocket-api-1.0-rc5.jar" sourcepath =" M2_REPO/javax/websocket/javax.websocket-api/1.0-rc5/javax.websocket-api-1.0-rc5-sources.jar" />
8+ <classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER" />
9+ <classpathentry kind =" var" path =" M2_REPO/javax/javaee-api/7.0-b82/javaee-api-7.0-b82.jar" />
10+ <classpathentry kind =" var" path =" M2_REPO/com/sun/mail/javax.mail/1.5.0-b01/javax.mail-1.5.0-b01.jar" />
11+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/tyrus/tyrus-client/1.0-rc3/tyrus-client-1.0-rc3.jar" />
12+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/tyrus/tyrus-core/1.0-rc3/tyrus-core-1.0-rc3.jar" />
13+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/tyrus/tyrus-spi/1.0-rc3/tyrus-spi-1.0-rc3.jar" />
14+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/tyrus/tyrus-websocket-core/1.0-rc3/tyrus-websocket-core-1.0-rc3.jar" />
15+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/tyrus/tyrus-container-grizzly/1.0-rc3/tyrus-container-grizzly-1.0-rc3.jar" />
16+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/grizzly/grizzly-framework/2.3/grizzly-framework-2.3.jar" />
17+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/grizzly/grizzly-http-server/2.3/grizzly-http-server-2.3.jar" />
18+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/grizzly/grizzly-http/2.3/grizzly-http-2.3.jar" />
19+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/grizzly/grizzly-rcm/2.3/grizzly-rcm-2.3.jar" />
20+ <classpathentry kind =" var" path =" M2_REPO/org/glassfish/tyrus/tyrus-server/1.0-rc3/tyrus-server-1.0-rc3.jar" />
21+ </classpath >
Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+
4+
5+ <groupId >com.hmkcode</groupId >
6+ <artifactId >websocket-app-client</artifactId >
7+ <version >1.0-SNAPSHOT</version >
8+ <packaging >jar</packaging >
9+ <modelVersion >4.0.0</modelVersion >
10+ <name >websocket-app-client</name >
11+
12+
13+ <repositories >
14+ <repository >
15+ <id >java.net-promoted</id >
16+ <url >https://maven.java.net/content/groups/promoted/</url >
17+ </repository >
18+ </repositories >
19+
20+ <dependencies >
21+ <dependency >
22+ <groupId >javax</groupId >
23+ <artifactId >javaee-api</artifactId >
24+ <version >7.0-b82</version >
25+ </dependency >
26+ <dependency >
27+ <groupId >org.glassfish.tyrus</groupId >
28+ <artifactId >tyrus-client</artifactId >
29+ <version >1.0-rc3</version >
30+ </dependency >
31+ <dependency >
32+ <groupId >org.glassfish.tyrus</groupId >
33+ <artifactId >tyrus-container-grizzly</artifactId >
34+ <version >1.0-rc3</version >
35+ </dependency >
36+ </dependencies >
37+
38+ <build >
39+ <plugins >
40+ <plugin >
41+ <groupId >org.apache.maven.plugins</groupId >
42+ <artifactId >maven-compiler-plugin</artifactId >
43+ <version >2.3.2</version >
44+ <configuration >
45+ <source >1.7</source >
46+ <target >1.7</target >
47+ </configuration >
48+ </plugin >
49+ <plugin >
50+ <groupId >org.codehaus.mojo</groupId >
51+ <artifactId >exec-maven-plugin</artifactId >
52+ <executions >
53+ <execution >
54+ <id >my-execution</id >
55+ <phase >compile</phase >
56+ <goals >
57+ <goal >java</goal >
58+ </goals >
59+ </execution >
60+ </executions >
61+ <configuration >
62+ <mainClass >com.hmkcode.App</mainClass >
63+ </configuration >
64+ </plugin >
65+ </plugins >
66+ </build >
67+ </project >
Original file line number Diff line number Diff line change 1+
2+ package com .hmkcode ;
3+
4+ import java .io .BufferedReader ;
5+ import java .io .IOException ;
6+ import java .io .InputStreamReader ;
7+ import java .net .URI ;
8+ import javax .websocket .ContainerProvider ;
9+ import javax .websocket .DeploymentException ;
10+ import javax .websocket .Session ;
11+ import javax .websocket .WebSocketContainer ;
12+
13+ public class App {
14+
15+ public Session session ;
16+
17+ protected void start ()
18+ {
19+
20+ WebSocketContainer container = ContainerProvider .getWebSocketContainer ();
21+
22+ String uri = "ws://localhost:8080/websocket-glassfish-server/websocket/desktop-client" ;
23+ System .out .println ("Connecting to " + uri );
24+ try {
25+ session = container .connectToServer (MyClient .class , URI .create (uri ));
26+ } catch (DeploymentException e ) {
27+ e .printStackTrace ();
28+ } catch (IOException e ) {
29+ e .printStackTrace ();
30+ }
31+
32+ }
33+ public static void main (String args []){
34+ App client = new App ();
35+ client .start ();
36+
37+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
38+ String input = "" ;
39+ try {
40+ do {
41+ input = br .readLine ();
42+ if (!input .equals ("exit" ))
43+ client .session .getBasicRemote ().sendText (input );
44+
45+ }while (!input .equals ("exit" ));
46+
47+ } catch (IOException e ) {
48+ // TODO Auto-generated catch block
49+ e .printStackTrace ();
50+ }
51+ }
52+ }
Original file line number Diff line number Diff line change 1+ package com .hmkcode ;
2+
3+ import java .io .IOException ;
4+ import javax .websocket .ClientEndpoint ;
5+ import javax .websocket .OnError ;
6+ import javax .websocket .OnMessage ;
7+ import javax .websocket .OnOpen ;
8+ import javax .websocket .Session ;
9+
10+ @ ClientEndpoint
11+ public class MyClient {
12+ @ OnOpen
13+ public void onOpen (Session session ) {
14+ System .out .println ("Connected to endpoint: " + session .getBasicRemote ());
15+ try {
16+ session .getBasicRemote ().sendText ("Hello" );
17+ } catch (IOException ex ) {
18+ }
19+ }
20+
21+ @ OnMessage
22+ public void onMessage (String message ) {
23+ System .out .println (message );
24+ }
25+
26+ @ OnError
27+ public void onError (Throwable t ) {
28+ t .printStackTrace ();
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <classpath >
3+ <classpathentry kind =" src" path =" src/main/java" including =" **/*.java" />
4+ <classpathentry kind =" src" path =" src/main/resources" excluding =" **/*.java" />
5+ <classpathentry kind =" output" path =" target/classes" />
6+ <classpathentry kind =" var" path =" M2_REPO/javax/activation/activation/1.1/activation-1.1.jar" />
7+ <classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER" />
8+ <classpathentry kind =" var" path =" M2_REPO/javax/javaee-api/7.0-b82/javaee-api-7.0-b82.jar" />
9+ <classpathentry kind =" var" path =" M2_REPO/com/sun/mail/javax.mail/1.5.0-b01/javax.mail-1.5.0-b01.jar" />
10+ </classpath >
Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+
4+
5+ <groupId >com.hmkcode</groupId >
6+ <artifactId >websocket-glassfish-server</artifactId >
7+ <version >1.0-SNAPSHOT</version >
8+ <packaging >war</packaging >
9+ <modelVersion >4.0.0</modelVersion >
10+ <name >websocket-glassfish-server</name >
11+
12+ <repositories >
13+ <repository >
14+ <id >java.net-promoted</id >
15+ <url >https://maven.java.net/content/groups/promoted/</url >
16+ </repository >
17+ </repositories >
18+
19+ <dependencies >
20+ <dependency >
21+ <groupId >javax</groupId >
22+ <artifactId >javaee-api</artifactId >
23+ <version >7.0-b82</version >
24+ <scope >provided</scope >
25+ </dependency >
26+
27+ </dependencies >
28+
29+ <build >
30+ <plugins >
31+ <plugin >
32+ <groupId >org.apache.maven.plugins</groupId >
33+ <artifactId >maven-compiler-plugin</artifactId >
34+ <version >2.3.2</version >
35+ <configuration >
36+ <source >1.7</source >
37+ <target >1.7</target >
38+ </configuration >
39+ </plugin >
40+
41+ </plugins >
42+ </build >
43+ </project >
Original file line number Diff line number Diff line change 1+ package com .hmkcode ;
2+
3+ import java .io .IOException ;
4+ import java .util .LinkedList ;
5+ import javax .websocket .EncodeException ;
6+ import javax .websocket .OnClose ;
7+ import javax .websocket .OnMessage ;
8+ import javax .websocket .OnOpen ;
9+ import javax .websocket .Session ;
10+ import javax .websocket .server .PathParam ;
11+ import javax .websocket .server .ServerEndpoint ;
12+
13+ @ ServerEndpoint (value ="/websocket/{client-id}" )
14+ public class MyServerEndpoint {
15+
16+ private static final LinkedList <Session > clients = new LinkedList <Session >();
17+
18+ @ OnOpen
19+ public void onOpen (Session session ) {
20+ clients .add (session );
21+ }
22+ @ OnMessage
23+ public void onMessage (String message ,@ PathParam ("client-id" ) String clientId ) {
24+ for (Session client : clients ) {
25+ try {
26+ client .getBasicRemote ().sendObject (clientId +": " +message );
27+
28+ } catch (IOException e ) {
29+ e .printStackTrace ();
30+ } catch (EncodeException e ) {
31+ e .printStackTrace ();
32+ }
33+ }
34+ }
35+ @ OnClose
36+ public void onClose (Session peer ) {
37+ clients .remove (peer );
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <web-app xmlns =" http://java.sun.com/xml/ns/javaee"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://java.sun.com/xml/ns/javaee
5+ http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
6+ version =" 3.0" >
7+
8+ <display-name >websocket-glassfish-server</display-name >
9+ <welcome-file-list >
10+ <welcome-file >index.htm</welcome-file >
11+ </welcome-file-list >
12+
13+
14+ </web-app >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2+ < html xmlns ="http://www.w3.org/1999/xhtml ">
3+
4+ < head >
5+ < meta http-equiv ="Content-Type " content ="text/html; charset=utf-8 " />
6+ < title > Java API for WebSocket (JSR-356)</ title >
7+ < link href ="bootstrap/css/bootstrap.min.css " rel ="stylesheet " type ="text/css " />
8+ < script src ="http://code.jquery.com/jquery-1.9.1.js "> </ script >
9+ < script src ="websocket.js " type ="text/javascript "> </ script >
10+ </ head >
11+ < body >
12+ < div class ="container ">
13+ < h1 > Java API for WebSocket (JSR-356)</ h1 >
14+ < div >
15+ < span id ="status " class ="label label-important "> Not Connected</ span >
16+ </ div >
17+ < br />
18+
19+ < label style ="display:inline-block "> Message: </ label > < input type ="text " id ="message " />
20+ < button id ="send " class ="btn btn-primary " onclick ="sendMessage() "> Send</ button >
21+
22+ < table id ="received_messages " class ="table table-striped ">
23+ < tr >
24+ < th > #</ th >
25+ < th > Sender</ th >
26+ < th > Message</ th >
27+ </ tr >
28+ </ table >
29+ </ div >
30+ </ body >
31+ </ html >
You can’t perform that action at this time.
0 commit comments