1 package fi.iki.elonen.samples.echo;
2 
3 /*
4  * #%L
5  * NanoHttpd-Websocket
6  * %%
7  * Copyright (C) 2012 - 2015 nanohttpd
8  * %%
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice, this
13  *    list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the nanohttpd nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  * #L%
34  */
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.concurrent.CountDownLatch;
39 import java.util.concurrent.Future;
40 import java.util.concurrent.TimeUnit;
41 
42 import org.eclipse.jetty.websocket.api.Session;
43 import org.eclipse.jetty.websocket.api.StatusCode;
44 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
45 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
46 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
47 import org.eclipse.jetty.websocket.api.annotations.WebSocket;
48 
49 /**
50  * Basic Echo Client Socket
51  */
52 @WebSocket(maxTextMessageSize = 64 * 1024)
53 public class SimpleEchoSocket {
54 
55     private final List<String> receivedMessages = new ArrayList<String>();
56 
57     private final List<String> toSendMessages = new ArrayList<String>();
58 
59     private final CountDownLatch closeLatch;
60 
SimpleEchoSocket()61     public SimpleEchoSocket() {
62         this.closeLatch = new CountDownLatch(1);
63     }
64 
awaitClose(int duration, TimeUnit unit)65     public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException {
66         return this.closeLatch.await(duration, unit);
67     }
68 
getReceivedMessages()69     public List<String> getReceivedMessages() {
70         return this.receivedMessages;
71     }
72 
getToSendMessages()73     public List<String> getToSendMessages() {
74         return this.toSendMessages;
75     }
76 
77     @OnWebSocketClose
onClose(int statusCode, String reason)78     public void onClose(int statusCode, String reason) {
79         System.out.printf("Connection closed: %d - %s%n", statusCode, reason);
80         this.closeLatch.countDown();
81     }
82 
83     @OnWebSocketConnect
onConnect(Session session)84     public void onConnect(Session session) {
85         System.out.printf("Got connect: %s%n", session);
86         try {
87             Future<Void> fut;
88 
89             for (String message : this.toSendMessages) {
90                 fut = session.getRemote().sendStringByFuture(message);
91                 fut.get(5, TimeUnit.SECONDS);
92             }
93             session.close(StatusCode.NORMAL, "I'm done");
94         } catch (Throwable t) {
95             t.printStackTrace();
96         }
97     }
98 
99     @OnWebSocketMessage
onMessage(String msg)100     public void onMessage(String msg) {
101         System.out.printf("Got msg: %s%n", msg);
102         this.receivedMessages.add(msg);
103     }
104 }
105