Skip to content

Commit 728d3df

Browse files
committed
Google Docs on desktop using WebSocket, need further debugging
1 parent 35db103 commit 728d3df

File tree

8 files changed

+267
-0
lines changed

8 files changed

+267
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.websocket.google-docs</groupId>
6+
<artifactId>google-docs-sample</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.websocket.google-docs</groupId>
12+
<artifactId>client</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>jar</packaging>
15+
16+
<properties>
17+
<javafx.version>1.8-ea-b123</javafx.version>
18+
<javafx.runtime.lib.jar>${java.home}/lib/jfxrt.jar</javafx.runtime.lib.jar>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>io.undertow</groupId>
24+
<artifactId>undertow-core</artifactId>
25+
<version>1.0.16.Final</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>io.undertow</groupId>
29+
<artifactId>undertow-websockets-jsr</artifactId>
30+
<version>1.0.16.Final</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>javax</groupId>
34+
<artifactId>javaee-api</artifactId>
35+
<version>7.0</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.oracle</groupId>
39+
<artifactId>javafx</artifactId>
40+
<version>${javafx.version}</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<!-- Build an executable JAR -->
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-jar-plugin</artifactId>
50+
<version>2.5</version>
51+
<configuration>
52+
<archive>
53+
<manifest>
54+
<addClasspath>true</addClasspath>
55+
<mainClass>org.javaee7.websocket.googledocs.client.GoogleDocClient</mainClass>
56+
</manifest>
57+
</archive>
58+
</configuration>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.codehaus.mojo</groupId>
62+
<artifactId>exec-maven-plugin</artifactId>
63+
<version>1.2.1</version>
64+
<executions>
65+
<execution>
66+
<phase>test</phase>
67+
<goals>
68+
<goal>java</goal>
69+
</goals>
70+
<configuration>
71+
<mainClass>org.javaee7.websocket.googledocs.client.GoogleDocClient</mainClass>
72+
</configuration>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.javaee7.websocket.googledocs.client;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
import javafx.application.Application;
9+
import javafx.beans.value.ChangeListener;
10+
import javafx.beans.value.ObservableValue;
11+
import javafx.scene.Scene;
12+
import javafx.scene.control.TextArea;
13+
import javafx.stage.Stage;
14+
import javax.websocket.ContainerProvider;
15+
import javax.websocket.DeploymentException;
16+
import javax.websocket.Session;
17+
import javax.websocket.WebSocketContainer;
18+
19+
/**
20+
* @author Arun Gupta
21+
*/
22+
public class GoogleDocClient extends Application {
23+
24+
public static void main(String[] args) {
25+
launch(args);
26+
}
27+
28+
@Override
29+
public void start(Stage stage) throws Exception {
30+
final Session session = connectToServer();
31+
System.out.println("Connected to server: " + session.getId());
32+
stage.setTitle("Google Docs Emulator using WebSocket");
33+
TextArea textarea = new TextArea();
34+
textarea.textProperty().addListener(
35+
new ChangeListener<String>() {
36+
37+
@Override
38+
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
39+
System.out.println("New value: " + newValue);
40+
for (Session peer : session.getOpenSessions()) {
41+
System.out.println("Trying to send data...");
42+
if (!peer.equals(session)) {
43+
try {
44+
peer.getBasicRemote().sendText(newValue);
45+
} catch (IOException ex) {
46+
Logger.getLogger(GoogleDocClient.class.getName()).log(Level.SEVERE, null, ex);
47+
}
48+
}
49+
}
50+
}
51+
52+
}
53+
);
54+
55+
textarea.setPrefSize(500, 300);
56+
textarea.setWrapText(true);
57+
Scene scene = new Scene(textarea);
58+
stage.setScene(scene);
59+
stage.show();
60+
}
61+
62+
private Session connectToServer() throws URISyntaxException, DeploymentException, IOException {
63+
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
64+
return container.connectToServer(MyClient.class, new URI("ws://localhost:8080/server/websocket"));
65+
}
66+
67+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.javaee7.websocket.googledocs.client;
2+
3+
import java.io.IOException;
4+
import javax.websocket.ClientEndpoint;
5+
import javax.websocket.OnMessage;
6+
import javax.websocket.OnOpen;
7+
import javax.websocket.Session;
8+
9+
/**
10+
* @author Arun Gupta
11+
*/
12+
@ClientEndpoint
13+
public class MyClient {
14+
15+
@OnOpen
16+
public void onOpen(Session session) {
17+
System.out.println("Connected to endpoint: " + session.getBasicRemote());
18+
}
19+
20+
@OnMessage
21+
public void onMessage(String message, Session session) throws IOException {
22+
System.out.println("Received message in client: " + message);
23+
for (Session peer : session.getOpenSessions()) {
24+
if (!peer.equals(session)) {
25+
peer.getBasicRemote().sendText(message);
26+
}
27+
}
28+
}
29+
}

websocket/google-docs/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.websocket</groupId>
6+
<artifactId>websocket-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.websocket.google-docs</groupId>
12+
<artifactId>google-docs-sample</artifactId>
13+
<packaging>pom</packaging>
14+
<name>Java EE 7 WebSocket Google Docs</name>
15+
16+
<modules>
17+
<module>server</module>
18+
<module>client</module>
19+
</modules>
20+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Install JavaFX library locally:
2+
3+
[source,text]
4+
----
5+
mvn install:install-file -Dfile=$JAVA_HOME/jre/lib/ext/jfxrt.jar -DgroupId=com.oracle -DartifactId=javafx -Dversion=1.8-ea-b123 -Dpackaging=jar
6+
----
7+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.websocket.google-docs</groupId>
6+
<artifactId>google-docs-sample</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.websocket.google-docs</groupId>
12+
<artifactId>server</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.javaee7.websocket.googledocs.server;
2+
3+
import java.io.IOException;
4+
import java.util.logging.Level;
5+
import java.util.logging.Logger;
6+
7+
import javax.websocket.EncodeException;
8+
import javax.websocket.OnMessage;
9+
import javax.websocket.OnOpen;
10+
import javax.websocket.Session;
11+
import javax.websocket.server.ServerEndpoint;
12+
13+
/**
14+
* @author Arun Gupta
15+
*/
16+
@ServerEndpoint(value = "/websocket")
17+
public class GoogleDocServer {
18+
19+
private static final Logger LOGGER = Logger.getLogger(GoogleDocServer.class.getName());
20+
21+
@OnOpen
22+
public void onOpen(Session client) {
23+
LOGGER.log(Level.INFO, "connected");
24+
}
25+
26+
@OnMessage
27+
public void broadcastText(String text, Session session) throws IOException, EncodeException {
28+
LOGGER.log(Level.INFO, "broadcastText: {0}", text);
29+
for (Session peer : session.getOpenSessions()) {
30+
if (!peer.equals(session)) {
31+
peer.getBasicRemote().sendText(text);
32+
}
33+
}
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<!--
3+
To change this license header, choose License Headers in Project Properties.
4+
To change this template file, choose Tools | Templates
5+
and open the template in the editor.
6+
-->
7+
<html>
8+
<head>
9+
<title>Google Docs Server</title>
10+
<meta charset="UTF-8">
11+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
12+
</head>
13+
<body>
14+
<div>Google Docs Server</div>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)