| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package examples; |
| 15 |
|
| 16 |
import java.util.concurrent.CountDownLatch; |
| 17 |
import java.util.concurrent.TimeUnit; |
| 18 |
|
| 19 |
import org.eclipse.jetty.ee9.websocket.api.Session; |
| 20 |
import org.eclipse.jetty.ee9.websocket.api.StatusCode; |
| 21 |
import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketClose; |
| 22 |
import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketConnect; |
| 23 |
import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketMessage; |
| 24 |
import org.eclipse.jetty.ee9.websocket.api.annotations.WebSocket; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
@WebSocket(maxTextMessageSize = 64 * 1024) |
| 30 |
public class SimpleEchoSocket |
| 31 |
{ |
| 32 |
private final CountDownLatch closeLatch; |
| 33 |
@SuppressWarnings("unused") |
| 34 |
private Session session; |
| 35 |
|
| 36 |
public SimpleEchoSocket() |
| 37 |
{ |
| 38 |
this.closeLatch = new CountDownLatch(1); |
| 39 |
} |
| 40 |
|
| 41 |
public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException |
| 42 |
{ |
| 43 |
return this.closeLatch.await(duration, unit); |
| 44 |
} |
| 45 |
|
| 46 |
@OnWebSocketClose |
| 47 |
public void onClose(int statusCode, String reason) |
| 48 |
{ |
| 49 |
System.out.printf("Connection closed: %d - %s%n", statusCode, reason); |
| 50 |
this.session = null; |
| 51 |
this.closeLatch.countDown(); |
| 52 |
} |
| 53 |
|
| 54 |
@OnWebSocketConnect |
| 55 |
public void onConnect(Session session) |
| 56 |
{ |
| 57 |
System.out.printf("Got connect: %s%n", session); |
| 58 |
this.session = session; |
| 59 |
try |
| 60 |
{ |
| 61 |
session.getRemote().sendString("Hello"); |
| 62 |
session.getRemote().sendString("Thanks for the conversation."); |
| 63 |
session.close(StatusCode.NORMAL, "I'm done"); |
| 64 |
} |
| 65 |
catch (Throwable t) |
| 66 |
{ |
| 67 |
t.printStackTrace(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@OnWebSocketMessage |
| 72 |
public void onMessage(String msg) |
| 73 |
{ |
| 74 |
System.out.printf("Got msg: %s%n", msg); |
| 75 |
} |
| 76 |
} |