我正在尝试使用带有基于注释的客户端端点的tyrus独立客户端(tyrus-standalone-client-1.9)访问websocket服务器端点。我主要关注this example。
也就是说,我的客户端端点目前看起来像
@ClientEndpoint
public class MyClientEndpoint {
private static CountDownLatch latch;
private Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session session) throws Exception {
session.getBasicRemote().sendText("initialRequest")
}
@OnMessage
public void onMessage(String message, Session session) throws Exception {
// do something
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info(String.format("Session %s close because of %s", session.getId(), closeReason));
latch.countDown();
}
public static void main(String[] args) {
latch = new CountDownLatch(1);
ClientManager client = ClientManager.createClient();
try {
URI serverEndpointUri = new URI("ws://localhost/websockets/server/endpoint");
client.connectToServer(MyClientEndpoint.class, serverEndpointUri);
latch.await();
} catch (DeploymentException | URISyntaxException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
但是我需要传递一些会话ID以及请求,我需要修改请求的origin头以使服务器端点接受我的连接。
在程序化客户端端点中,我可以执行类似
的操作final Builder configBuilder = ClientEndpointConfig.Builder.create();
configBuilder.configurator(new Configurator() {
@Override
public void beforeRequest(final Map<String, List<String>> headers) {
headers.put("Cookie", Arrays.asList("X-Session=0f822c8c-bf63-4ae7-9d2f-af263f86baad"));
headers.put("Origin", Arrays.asList("http://localhost"));
}
});
ClientEndpointConfig clientConfig = configBuilder.build();
ClientManager client = ClientManager.createClient();
URI serverEndpointUri = new URI("ws://localhost/websockets/server/endpoint");
client.connectToServer(new MyClientEndpoint(), clientConfig, serverEndpointUri);
但似乎没有任何选项可以将配置传递给基于注释的客户端。
是否有其他方法可以添加/修改我目前缺少的请求标头?我真的很想继续使用基于注释的方法,因为它对我来说似乎更清晰......
答案 0 :(得分:7)
请参阅ModifyRequestResponseHeadersTest.java:183
@ClientEndpoint(configurator = MyClientConfigurator.class)
public static class MyClientEndpoint {
public static final CountDownLatch messageLatch = new CountDownLatch(1);
public static volatile String receivedMessage;
@OnOpen
public void onOpen(Session session) throws IOException {
session.getBasicRemote().sendText(SENT_MESSAGE);
}
@OnMessage
public void onMessage(String message) {
receivedMessage = message;
messageLatch.countDown();
}
}
和MyClientConfigurator:
public static class MyClientConfigurator extends ClientEndpointConfig.Configurator {
static volatile boolean called = false;
@Override
public void beforeRequest(Map<String, List<String>> headers) {
called = true;
headers.put(HEADER_NAME, Arrays.asList(HEADER_VALUE));
headers.put("Origin", Arrays.asList("myOrigin"));
}
@Override
public void afterResponse(HandshakeResponse handshakeResponse) {
final Map<String, List<String>> headers = handshakeResponse.getHeaders();
assertEquals(HEADER_VALUE[0], headers.get(HEADER_NAME).get(0));
assertEquals(HEADER_VALUE[1], headers.get(HEADER_NAME).get(1));
assertEquals(HEADER_VALUE[2], headers.get(HEADER_NAME).get(2));
assertEquals("myOrigin", headers.get("origin").get(0));
}
}