WebSocketContainer wsContainer = ContainerProvider
.getWebSocketContainer();
tomcat.start();
Session wsSession = wsContainer.connectToServer(
TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder
.create().preferredSubprotocols(Arrays.asList("sp3"))
.build(), new URI("ws://localhost:" + getPort()
+ SubProtocolsEndpoint.PATH_BASIC));
Assert.assertTrue(wsSession.isOpen());
if (wsSession.getNegotiatedSubprotocol() != null) {
Assert.assertTrue(wsSession.getNegotiatedSubprotocol().isEmpty());
}
wsSession.close();
wsSession = wsContainer.connectToServer(
TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder
.create().preferredSubprotocols(Arrays.asList("sp2"))
.build(), new URI("ws://localhost:" + getPort()
+ SubProtocolsEndpoint.PATH_BASIC));
Assert.assertTrue(wsSession.isOpen());
Assert.assertEquals("sp2", wsSession.getNegotiatedSubprotocol());
// Client thread might move faster than server. Wait for upto 5s for the
// subProtocols to be set
int count = 0;
while (count < 50 && SubProtocolsEndpoint.subprotocols == null) {
count++;
Thread.sleep(100);
}
Assert.assertNotNull(SubProtocolsEndpoint.subprotocols);
Assert.assertArrayEquals(new String[]{"sp1","sp2"},
SubProtocolsEndpoint.subprotocols.toArray(new String[2]));
wsSession.close();
}