Skip to content

Commit

Permalink
Add Atmosphere sample application
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Jan 13, 2015
1 parent 735b627 commit 43d577a
Show file tree
Hide file tree
Showing 10 changed files with 10,210 additions and 0 deletions.
1 change: 1 addition & 0 deletions spring-boot-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<module>spring-boot-sample-actuator-ui</module>
<module>spring-boot-sample-amqp</module>
<module>spring-boot-sample-aop</module>
<module>spring-boot-sample-atmosphere</module>
<module>spring-boot-sample-batch</module>
<module>spring-boot-sample-data-elasticsearch</module>
<module>spring-boot-sample-data-gemfire</module>
Expand Down
61 changes: 61 additions & 0 deletions spring-boot-samples/spring-boot-sample-atmosphere/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>1.2.2.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-atmosphere</artifactId>
<name>Spring Boot Atmosphere Sample</name>
<description>Spring Boot Atmosphere Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>atmosphere-javascript</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* 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.
*/

package sample;

import java.io.IOException;

import org.atmosphere.config.managed.Decoder;
import org.atmosphere.config.managed.Encoder;
import org.atmosphere.config.service.Disconnect;
import org.atmosphere.config.service.ManagedService;
import org.atmosphere.config.service.Ready;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

@ManagedService(path = "/chat")
public class ChatService {

private final Logger logger = LoggerFactory.getLogger(ChatService.class);

@Ready
public void onReady(final AtmosphereResource resource) {
this.logger.info("Connected", resource.uuid());
}

@Disconnect
public void onDisconnect(AtmosphereResourceEvent event) {
this.logger.info("Client {} disconnected [{}]", event.getResource().uuid(),
(event.isCancelled() ? "cancelled" : "closed"));
}

@org.atmosphere.config.service.Message(encoders = JacksonEncoderDecoder.class, decoders = JacksonEncoderDecoder.class)
public Message onMessage(Message message) throws IOException {
this.logger.info("Author {} sent message {}", message.getAuthor(),
message.getMessage());
return message;
}

public static class JacksonEncoderDecoder implements Encoder<Message, String>,
Decoder<String, Message> {

private final ObjectMapper mapper = new ObjectMapper();

@Override
public String encode(Message m) {
try {
return this.mapper.writeValueAsString(m);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}

@Override
public Message decode(String s) {
try {
return this.mapper.readValue(s, Message.class);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* 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.
*/

package sample;

import java.util.Date;

public class Message {

private String message;

private String author;

private long time = new Date().getTime();

public String getMessage() {
return this.message;
}

public void setMessage(String message) {
this.message = message;
}

public String getAuthor() {
return this.author;
}

public void setAuthor(String author) {
this.author = author;
}

public long getTime() {
return this.time;
}

public void setTime(long time) {
this.time = time;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* 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.
*/

package sample;

import java.util.Collections;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.atmosphere.cpr.AtmosphereInitializer;
import org.atmosphere.cpr.AtmosphereServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableAutoConfiguration
public class SampleAtmosphereApplication {

@Bean
public EmbeddedAtmosphereInitializer atmosphereInitializer() {
return new EmbeddedAtmosphereInitializer();
}

@Bean
public ServletRegistrationBean atmosphereServlet() {
// Dispatcher servlet is mapped to '/home' to allow the AtmosphereServlet
// to be mapped to '/chat'
ServletRegistrationBean registration = new ServletRegistrationBean(
new AtmosphereServlet(), "/chat/*");
registration.addInitParameter("org.atmosphere.cpr.packages", "sample");
registration.addInitParameter("org.atmosphere.interceptor.HeartbeatInterceptor"
+ ".clientHeartbeatFrequencyInSeconds", "10");
registration.setLoadOnStartup(0);
// Need to occur before the EmbeddedAtmosphereInitializer
registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registration;
}

@Configuration
static class MvcConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/home/home.html");
}

}

private static class EmbeddedAtmosphereInitializer extends AtmosphereInitializer
implements ServletContextInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
onStartup(Collections.<Class<?>> emptySet(), servletContext);
}

}

public static void main(String[] args) throws Exception {
SpringApplication.run(SampleAtmosphereApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server.servlet-path=/home/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Atmosphere Chat</title>
<!-- Atmosphere -->
<script type="text/javascript" src="webjars/atmosphere-javascript/2.2.3/atmosphere.js"></script>
<!-- Application -->
<script type="text/javascript" src="javascript/jquery-1.9.0.js"></script>
<script type="text/javascript" src="javascript/application.js"></script>
<style>
* {
font-family: tahoma;
font-size: 12px;
padding: 0px;
margin: 0px;
}

p {
line-height: 18px;
}

div {
width: 500px;
margin-left: auto;
margin-right: auto;
}

#content {
padding: 5px;
background: #ddd;
border-radius: 5px;
border: 1px solid #CCC;
margin-top: 10px;
}

#header {
padding: 5px;
background: #f5deb3;
border-radius: 5px;
border: 1px solid #CCC;
margin-top: 10px;
}

#input {
border-radius: 2px;
border: 1px solid #ccc;
margin-top: 10px;
padding: 5px;
width: 400px;
}

#status {
width: 88px;
display: block;
float: left;
margin-top: 15px;
}
</style>
</head>
<body>
<div id="header">
<h3>Atmosphere Chat. Default transport is WebSocket, fallback is
long-polling</h3>
</div>
<div id="content"></div>
<div>
<span id="status">Connecting...</span> <input type="text" id="input"
disabled="disabled" />
</div>
</body>
</html>
Loading

0 comments on commit 43d577a

Please sign in to comment.