| 
									
										
										
										
											2015-01-14 07:06:16 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-23 15:47:12 +08:00
										 |  |  | package sample.atmosphere;
 | 
					
						
							| 
									
										
										
										
											2015-01-14 07:06:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import java.io.IOException;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 03:23:14 +08:00
										 |  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
					
						
							| 
									
										
										
										
											2015-01-14 07:06:16 +08:00
										 |  |  | 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;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @ManagedService(path = "/chat")
 | 
					
						
							|  |  |  | public class ChatService {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	private final Logger logger = LoggerFactory.getLogger(ChatService.class);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	@Ready
 | 
					
						
							| 
									
										
										
										
											2017-12-08 21:29:03 +08:00
										 |  |  | 	public void onReady(AtmosphereResource resource) {
 | 
					
						
							| 
									
										
										
										
											2015-01-14 07:06:16 +08:00
										 |  |  | 		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;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 14:32:31 +08:00
										 |  |  | 	public static class JacksonEncoderDecoder
 | 
					
						
							|  |  |  | 			implements Encoder<Message, String>, Decoder<String, Message> {
 | 
					
						
							| 
									
										
										
										
											2015-01-14 07:06:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		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);
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }
 |