/
SimpleJakartaWebSocket.java
74 lines (64 loc) · 2.14 KB
/
SimpleJakartaWebSocket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.osgi.test;
import java.util.concurrent.CountDownLatch;
import jakarta.websocket.ClientEndpoint;
import jakarta.websocket.CloseReason;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnError;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import static org.junit.Assert.fail;
@ClientEndpoint(
subprotocols = {"chat"})
public class SimpleJakartaWebSocket
{
private Session session;
public CountDownLatch messageLatch = new CountDownLatch(1);
public CountDownLatch closeLatch = new CountDownLatch(1);
@OnError
public void onError(Throwable t)
{
//t.printStackTrace();
fail(t.getMessage());
}
@OnClose
public void onClose(CloseReason close)
{
//System.out.printf("Closed: %d, \"%s\"%n",close.getCloseCode().getCode(),close.getReasonPhrase());
closeLatch.countDown();
}
@OnMessage
public void onMessage(String message)
{
//System.out.printf("Received: \"%s\"%n",message);
messageLatch.countDown();
}
@OnOpen
public void onOpen(Session session)
{
//System.out.printf("Opened%n");
this.session = session;
}
public void writeMessage(String message) throws Exception
{
//System.out.printf("Writing: \"%s\"%n",message);
session.getBasicRemote().sendText(message);
}
}