org.eclipse.jetty.websocket.WebSocketClient Java Examples

The following examples show how to use org.eclipse.jetty.websocket.WebSocketClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebSocketServerInputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleTest() throws Exception
{
  final int port = 6666;
  String connectionURI = "ws://localhost:" + port + WebSocketServerInputOperator.DEFAULT_EXTENSION;

  final String message = "hello world";

  WebSocketServerInputOperator wssio = new TestWSSIO();
  wssio.setPort(port);
  wssio.setup(null);

  WebSocketClientFactory clientFactory = new WebSocketClientFactory();
  clientFactory.start();
  WebSocketClient client = new WebSocketClient(clientFactory);

  Future<WebSocket.Connection> connectionFuture = client.open(new URI(connectionURI), new TestWebSocket());
  WebSocket.Connection connection = connectionFuture.get(5, TimeUnit.SECONDS);

  connection.sendMessage(message);

  long startTime = System.currentTimeMillis();

  while (startTime + 10000 > System.currentTimeMillis()) {
    if (TestWSSIO.messages.size() >= 1) {
      break;
    }
    Thread.sleep(100);
  }

  Assert.assertEquals("The number of messages recieved is incorrect.", 1, TestWSSIO.messages.size());
  Assert.assertEquals("Incorrect message received", message, TestWSSIO.messages.get(0));

  connection.close();
  wssio.teardown();
}
 
Example #2
Source File: WampJettyFactory.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected void connect(URI uri, long timeout, JettyConnection connection) throws Exception{	
	if(!fact.isStarted())
		fact.start();
		
	WebSocketClient ws = fact.newWebSocketClient();
	ws.setProtocol(getProtocolName());
	
	if(timeout > 0)
		ws.open(uri, connection, timeout, TimeUnit.MILLISECONDS);
	else
		ws.open(uri,connection);
}