Skip to content

Commit 7fcad3f

Browse files
committed
Merge pull request #111 from jfarcand/master
Add Atmosphere Sample
2 parents c75f92c + 1b184a7 commit 7fcad3f

9 files changed

Lines changed: 9993 additions & 0 deletions

File tree

websocket/atmosphere-chat/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
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+
<modelVersion>4.0.0</modelVersion>
11+
<groupId>org.javaee7.websocket</groupId>
12+
<artifactId>atmosphere-chat</artifactId>
13+
<packaging>war</packaging>
14+
<version>1.0-SNAPSHOT</version>
15+
<name>atmosphere-chat</name>
16+
<url>http://maven.apache.org</url>
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.atmosphere.client</groupId>
20+
<artifactId>javascript</artifactId>
21+
<version>2.0.7</version>
22+
<type>war</type>
23+
</dependency>
24+
<dependency>
25+
<groupId>ch.qos.logback</groupId>
26+
<artifactId>logback-classic</artifactId>
27+
<version>1.0.13</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>ch.qos.logback</groupId>
32+
<artifactId>logback-core</artifactId>
33+
<version>1.0.13</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.atmosphere</groupId>
37+
<artifactId>atmosphere-runtime</artifactId>
38+
<version>2.0.3</version>
39+
</dependency>
40+
<!-- Uncomment to use Redis as a backend for ClouPush
41+
<dependency>
42+
<groupId>org.atmosphere</groupId>
43+
<artifactId>atmosphere-redis</artifactId>
44+
<version>${project.version}</version>
45+
</dependency>
46+
-->
47+
<dependency>
48+
<groupId>org.codehaus.jackson</groupId>
49+
<artifactId>jackson-mapper-asl</artifactId>
50+
<version>1.9.3</version>
51+
</dependency>
52+
</dependencies>
53+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2013 Jeanfrancois Arcand
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javaee7.websocket.atmosphere;
17+
18+
import org.atmosphere.config.service.Disconnect;
19+
import org.atmosphere.config.service.ManagedService;
20+
import org.atmosphere.config.service.Ready;
21+
import org.atmosphere.cpr.AtmosphereResource;
22+
import org.atmosphere.cpr.AtmosphereResourceEvent;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.io.IOException;
27+
28+
/**
29+
* Simple annotated class that demonstrate the power of Atmosphere. This class supports all transports, support
30+
* message length guarantee, heart beat, message cache thanks to the @ManagedAService.
31+
*
32+
* The client will first try with WebSocket and then fallback using the client's preference.
33+
*/
34+
@ManagedService(path = "/chat")
35+
public class ChatEndpoint {
36+
private final Logger logger = LoggerFactory.getLogger(ChatEndpoint.class);
37+
38+
/**
39+
* Invoked when the connection as been fully established and suspended, e.g ready for receiving messages.
40+
*
41+
* @param r
42+
*/
43+
@Ready
44+
public void onReady(final AtmosphereResource r) {
45+
logger.info("Browser {} connected.", r.uuid());
46+
}
47+
48+
/**
49+
* Invoked when the client disconnect or when an unexpected closing of the underlying connection happens.
50+
*
51+
* @param event
52+
*/
53+
@Disconnect
54+
public void onDisconnect(AtmosphereResourceEvent event) {
55+
if (event.isCancelled()) {
56+
logger.info("Browser {} unexpectedly disconnected", event.getResource().uuid());
57+
} else if (event.isClosedByClient()) {
58+
logger.info("Browser {} closed the connection", event.getResource().uuid());
59+
}
60+
}
61+
62+
/**
63+
* Simple annotated class that demonstrate how {@link org.atmosphere.config.managed.Encoder} and {@link org.atmosphere.config.managed.Decoder
64+
* can be used.
65+
*
66+
* @param message an instance of {@link Message}
67+
* @return
68+
* @throws IOException
69+
*/
70+
@org.atmosphere.config.service.Message(encoders = {JacksonEncoder.class}, decoders = {JacksonDecoder.class})
71+
public Message onMessage(Message message) throws IOException {
72+
logger.info("{} just send {}", message.getAuthor(), message.getMessage());
73+
return message;
74+
}
75+
76+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2013 Jeanfrancois Arcand
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javaee7.websocket.atmosphere;
17+
18+
import org.atmosphere.config.managed.Decoder;
19+
import org.codehaus.jackson.map.ObjectMapper;
20+
21+
import java.io.IOException;
22+
23+
/**
24+
* Decode a String into a {@link Message}.
25+
*/
26+
public class JacksonDecoder implements Decoder<String, Message> {
27+
28+
private final ObjectMapper mapper = new ObjectMapper();
29+
30+
@Override
31+
public Message decode(String s) {
32+
try {
33+
return mapper.readValue(s, Message.class);
34+
} catch (IOException e) {
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2013 Jeanfrancois Arcand
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javaee7.websocket.atmosphere;
17+
18+
import org.atmosphere.config.managed.Encoder;
19+
import org.codehaus.jackson.map.ObjectMapper;
20+
21+
import java.io.IOException;
22+
23+
/**
24+
* Encode a {@link Message} into a String
25+
*/
26+
public class JacksonEncoder implements Encoder<Message, String> {
27+
28+
private final ObjectMapper mapper = new ObjectMapper();
29+
30+
@Override
31+
public String encode(Message m) {
32+
try {
33+
return mapper.writeValueAsString(m);
34+
} catch (IOException e) {
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2013 Jeanfrancois Arcand
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javaee7.websocket.atmosphere;
17+
18+
import java.util.Date;
19+
20+
public class Message {
21+
22+
private String message;
23+
private String author;
24+
private long time;
25+
26+
public Message() {
27+
this("", "");
28+
}
29+
30+
public Message(String author, String message) {
31+
this.author = author;
32+
this.message = message;
33+
this.time = new Date().getTime();
34+
}
35+
36+
public String getMessage() {
37+
return message;
38+
}
39+
40+
public String getAuthor() {
41+
return author;
42+
}
43+
44+
public void setAuthor(String author) {
45+
this.author = author;
46+
}
47+
48+
public void setMessage(String message) {
49+
this.message = message;
50+
}
51+
52+
public long getTime() {
53+
return time;
54+
}
55+
56+
public void setTime(long time) {
57+
this.time = time;
58+
}
59+
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5+
version="3.1">
6+
7+
<description>Atmosphere Chat</description>
8+
<display-name>Atmosphere Chat</display-name>
9+
<servlet>
10+
<description>AtmosphereServlet</description>
11+
<servlet-name>AtmosphereServlet</servlet-name>
12+
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
13+
<init-param>
14+
<param-name>org.atmosphere.cpr.packages</param-name>
15+
<param-value>org.javaee7.websocket.atmosphere</param-value>
16+
</init-param>
17+
<async-supported>true</async-supported>
18+
<load-on-startup>0</load-on-startup>
19+
</servlet>
20+
<servlet-mapping>
21+
<servlet-name>AtmosphereServlet</servlet-name>
22+
<url-pattern>/chat/*</url-pattern>
23+
</servlet-mapping>
24+
</web-app>
25+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Atmosphere Chat</title>
6+
<!-- Atmosphere -->
7+
<script type="text/javascript" src="javascript/atmosphere.js"></script>
8+
<!-- Application -->
9+
<script type="text/javascript" src="javascript/jquery-1.9.0.js"></script>
10+
<script type="text/javascript" src="javascript/application.js"></script>
11+
<style>
12+
* {font-family: tahoma; font-size: 12px; padding: 0px; margin: 0px;}
13+
p {line-height: 18px;}
14+
div {width: 500px; margin-left: auto; margin-right: auto;}
15+
#content {padding: 5px; background: #ddd; border-radius: 5px; border: 1px solid #CCC; margin-top: 10px;}
16+
#header {padding: 5px; background: #f5deb3; border-radius: 5px; border: 1px solid #CCC; margin-top: 10px;}
17+
#input {border-radius: 2px; border: 1px solid #ccc; margin-top: 10px; padding: 5px; width: 400px;}
18+
#status {width: 88px; display: block; float: left; margin-top: 15px;}
19+
</style>
20+
</head>
21+
<body>
22+
<div id="header"><h3>Atmosphere Chat. Default transport is WebSocket, fallback is long-polling</h3></div>
23+
<div id="content"></div>
24+
<div>
25+
<span id="status">Connecting...</span>
26+
<input type="text" id="input" disabled="disabled"/>
27+
</div>
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)