org.eclipse.jetty.websocket.api.StatusCode Java Examples

The following examples show how to use org.eclipse.jetty.websocket.api.StatusCode. 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: NotebookServerTest.java    From submarine with Apache License 2.0 7 votes vote down vote up
@Test
public void testWebsocketConnection() throws Exception{
  URI uri = URI.create(
      AbstractSubmarineServerTest.getWebsocketApiUrlToTest());
  WebSocketClient client = new WebSocketClient();
  try {
    client.start();
    // The socket that receives events
    EventSocket socket = new EventSocket();
    // Attempt Connect
    Future<Session> fut = client.connect(socket, uri);
    // Wait for Connect
    Session session = fut.get();
    // Send a message
    session.getRemote().sendString("Hello");
    // Close session
    //session.close();
    session.close(StatusCode.NORMAL, "I'm done");
  } finally {
    client.stop();
  }
}
 
Example #2
Source File: ServiceSocket.java    From JMeter-WebSocketSampler with Apache License 2.0 6 votes vote down vote up
@OnWebSocketMessage
public void onMessage(String msg) {
    synchronized (parent) {
        log.debug("Received message: " + msg);
        String length = " (" + msg.length() + " bytes)";
        logMessage.append(" - Received message #").append(messageCounter).append(length);
        addResponseMessage("[Message " + (messageCounter++) + "]\n" + msg + "\n\n");

        if (responseExpression == null || responseExpression.matcher(msg).find()) {
            logMessage.append("; matched response pattern").append("\n");
            closeLatch.countDown();
        } else if (!disconnectPattern.isEmpty() && disconnectExpression.matcher(msg).find()) {
            logMessage.append("; matched connection close pattern").append("\n");
            closeLatch.countDown();
            close(StatusCode.NORMAL, "JMeter closed session.");
        } else {
            logMessage.append("; didn't match any pattern").append("\n");
        }
    }
}
 
Example #3
Source File: ProxyWebSocketAdapter.java    From knox with Apache License 2.0 6 votes vote down vote up
/**
 * Cleanup sessions
 */
private void cleanupOnError(final Throwable t) {

  LOG.onError(t.toString());
  if (t.toString().contains("exceeds maximum size")) {
    if(frontendSession != null && !frontendSession.isOpen()) {
      frontendSession.close(StatusCode.MESSAGE_TOO_LARGE, t.getMessage());
    }
  }

  else {
    if(frontendSession != null && !frontendSession.isOpen()) {
      frontendSession.close(StatusCode.SERVER_ERROR, t.getMessage());
    }
    cleanup();
  }
}
 
Example #4
Source File: WebSocketMetricAdapter.java    From incubator-nemo with Apache License 2.0 5 votes vote down vote up
@Override
public final void onWebSocketClose(final int statusCode, final String reason) {
  if (statusCode != StatusCode.NORMAL) {
    LOG.warn("WebSocket session closed abnormally: {} - {}.", statusCode, reason);
  }
  MetricBroadcaster.getInstance().removeSession(session);
}
 
Example #5
Source File: ConsoleLogSocket.java    From gocd with Apache License 2.0 5 votes vote down vote up
@OnWebSocketError
public void onError(Throwable error) {
    LOGGER.error("{} closing session because an error was thrown", sessionName(), error);
    try {
        close(StatusCode.SERVER_ERROR, error.getMessage());
    } finally {
        socketHealthService.deregister(this);
    }
}
 
Example #6
Source File: BoseSoundTouchHandler.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void onWebSocketError(Throwable e) {
    logger.debug("{}: Error during websocket communication: {}", getDeviceName(), e.getMessage(), e);
    updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
    if (commandExecutor != null) {
        commandExecutor.postOperationMode(OperationModeType.OFFLINE);
        commandExecutor = null;
    }
    if (session != null) {
        session.close(StatusCode.SERVER_ERROR, getDeviceName() + ": Failure: " + e.getMessage());
        session = null;
    }
}
 
Example #7
Source File: ServiceSocket.java    From JMeter-WebSocketSampler with Apache License 2.0 5 votes vote down vote up
public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException {
    logMessage.append(" - Waiting for messages for ").append(duration).append(" ").append(unit.toString()).append("\n");
    boolean res = this.closeLatch.await(duration, unit);

    if (!parent.isStreamingConnection()) {
        close(StatusCode.NORMAL, "JMeter closed session.");
    } else {
        logMessage.append(" - Leaving streaming connection open").append("\n");
    }

    return res;
}
 
Example #8
Source File: ServiceSocket.java    From JMeter-WebSocketSampler with Apache License 2.0 5 votes vote down vote up
@OnWebSocketFrame
public void onFrame(Frame frame) {
	synchronized (parent) {
		log.debug("Received frame: " + frame.getPayload() + " "
				+ frame.getType().name());
		String length = " (" + frame.getPayloadLength() + " bytes)";
		logMessage.append(" - Received frame #").append(messageCounter)
				.append(length);
		String frameTxt = new String(frame.getPayload().array());
		addResponseMessage("[Frame " + (messageCounter++) + "]\n"
				+ frameTxt + "\n\n");

		if (responseExpression == null
				|| responseExpression.matcher(frameTxt).find()) {
			logMessage.append("; matched response pattern").append("\n");
			closeLatch.countDown();
		} else if (!disconnectPattern.isEmpty()
				&& disconnectExpression.matcher(frameTxt).find()) {
			logMessage.append("; matched connection close pattern").append(
					"\n");
			closeLatch.countDown();
			close(StatusCode.NORMAL, "JMeter closed session.");
		} else {
			logMessage.append("; didn't match any pattern").append("\n");
		}
	}
}
 
Example #9
Source File: SimpleEchoSocket.java    From java_rosbridge with GNU Lesser General Public License v3.0 5 votes vote down vote up
@OnWebSocketConnect
public void onConnect(Session session) {
	System.out.printf("Got connect: %s%n", session);
	this.session = session;
	try {
		Future<Void> fut;
		fut = session.getRemote().sendStringByFuture("Hello");
		fut.get(2, TimeUnit.SECONDS);
		fut = session.getRemote().sendStringByFuture("Thanks for the conversation.");
		fut.get(2, TimeUnit.SECONDS);
		session.close(StatusCode.NORMAL, "I'm done");
	} catch (Throwable t) {
		t.printStackTrace();
	}
}
 
Example #10
Source File: SimpleEchoSocket.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@OnWebSocketConnect
public void onConnect(Session session) {
  System.out.printf("Got connect: %s%n",session);
  try {
    Future<Void> fut = session.getRemote().sendStringByFuture("{\"field1\" : \"value\"}");
    fut.get(2, TimeUnit.SECONDS); // wait for send to complete.

    session.close(StatusCode.NORMAL,"I'm done");
  } catch (Throwable t) {
    t.printStackTrace();
  }
}
 
Example #11
Source File: WebSocketTargetSocket.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
  if (statusCode != StatusCode.NORMAL) {
    LOG.error(Utils.format("WebSocket Connection closed: {} - {}", statusCode, reason));
  }
  this.closeLatch.countDown();
}
 
Example #12
Source File: WebSocketClientSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
  if (statusCode != StatusCode.NORMAL) {
    LOG.error(Utils.format("WebSocket Connection closed: {} - {}", statusCode, reason));
    try {
      connectToWebSocket();
    } catch (Exception e) {
      LOG.error(Errors.WEB_SOCKET_01.getMessage(), e.toString(), e);
    }
  }
}
 
Example #13
Source File: DashBoardServer.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
public void stopServer() {
    try {
        if (server.isStarted()) {
            HarCompareHandler.closeAll(StatusCode.SHUTDOWN, "EXIT");
            server.stop();
            server.join();
        }
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, ex.getMessage(), ex);
    }
}
 
Example #14
Source File: WebSocketConnection.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
	if (statusCode != StatusCode.NORMAL)
		log.error("Disconnect " + statusCode + ": " + reason);
	else
		log.debug("Disconnect " + statusCode + ": " + reason);
    openLatch.countDown();
    closeLatch.countDown();
    connected = false;
}
 
Example #15
Source File: WebsocketAppenderTest.java    From karaf-decanter with Apache License 2.0 4 votes vote down vote up
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
    session.close(StatusCode.NORMAL, "I'm done");
    this.session = null;
    this.closeLatch.countDown();
}
 
Example #16
Source File: WebSocketConnection.java    From jmeter-bzm-plugins with Apache License 2.0 4 votes vote down vote up
public void close() {
    close(StatusCode.NORMAL, "JMeter closed session.");
}
 
Example #17
Source File: InteractionSocket.java    From aliyun-cupid-sdk with Apache License 2.0 4 votes vote down vote up
private boolean confirmClose(int statusCode, String reason) {
    return statusCode == StatusCode.NORMAL
            || SDKConstants.WEBSOCKET_STATUS_MESSAGE_SHUTDOWN.equalsIgnoreCase(reason);
}
 
Example #18
Source File: ServiceSocket.java    From JMeter-WebSocketSampler with Apache License 2.0 4 votes vote down vote up
public void close() {
    close(StatusCode.NORMAL, "JMeter closed session.");
}
 
Example #19
Source File: SimpleWebSocket.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
public void close() {
    if (session != null) {
        this.session.close(StatusCode.NORMAL, "Closing");
    }
}
 
Example #20
Source File: JettyWebSocketSession.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void close(final String reason) throws IOException {
    session.close(StatusCode.NORMAL, reason);
}
 
Example #21
Source File: JettyWebSocketSession.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void close(final String reason) throws IOException {
    session.close(StatusCode.NORMAL, reason);
}
 
Example #22
Source File: ConsoleLogSocket.java    From gocd with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    close(StatusCode.NORMAL, null);
}