Add Atmosphere sample application
Add Atmosphere example application based on http://github.com/Atmosphere/atmosphere-samples/tree/master/samples/chat Closes gh-2341
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user