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+ }
0 commit comments