javax.websocket.OnMessage Java Examples

The following examples show how to use javax.websocket.OnMessage. 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: StressEndpoint.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@OnMessage
public void handleMessage(Session session, final ByteBuffer message, boolean last) throws IOException {
    if(out == null) {
        out = session.getBasicRemote().getSendStream();
    }
    byte[] data = new byte[message.remaining()];
    message.get(data);
    out.write(data);

    if(last) {
        out.close();
        out = null;
    } else {
        out.flush();
    }
}
 
Example #2
Source File: EchoAsyncAnnotation.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@OnMessage
public void echoTextMessage(Session session, String msg, boolean last) {
    if (sb == null) {
        sb = new StringBuilder();
    }
    sb.append(msg);
    if (last) {
        // Before we send the next message, have to wait for the previous
        // message to complete
        try {
            f.get();
        } catch (InterruptedException | ExecutionException e) {
            // Let the container deal with it
            throw new RuntimeException(e);
        }
        f = session.getAsyncRemote().sendText(sb.toString());
        sb = null;
    }
}
 
Example #3
Source File: TestClose.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@OnMessage
public void onMessage(Session session, String message) {
    log.info("Message received: " + message);
    events.onMessageCalled.countDown();
    awaitLatch(events.onMessageWait, "onMessageWait not triggered");

    if (events.onMessageSends) {
        try {
            int count = 0;
            // The latches above are meant to ensure the correct
            // sequence of events but in some cases, particularly with
            // APR, there is a short delay between the client closing /
            // resetting the connection and the server recognising that
            // fact. This loop tries to ensure that it lasts much longer
            // than that delay so any close / reset from the client
            // triggers an error here.
            while (count < 10) {
                count++;
                session.getBasicRemote().sendText("Test reply");
                Thread.sleep(500);
            }
        } catch (IOException | InterruptedException e) {
            // Expected to fail
        }
    }
}
 
Example #4
Source File: EchoAsyncAnnotation.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@OnMessage
public void echoTextMessage(Session session, String msg, boolean last) {
    if (sb == null) {
        sb = new StringBuilder();
    }
    sb.append(msg);
    if (last) {
        // Before we send the next message, have to wait for the previous
        // message to complete
        try {
            f.get();
        } catch (InterruptedException | ExecutionException e) {
            // Let the container deal with it
            throw new RuntimeException(e);
        }
        f = session.getAsyncRemote().sendText(sb.toString());
        sb = null;
    }
}
 
Example #5
Source File: EchoStreamAnnotation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void echoBinaryMessage(byte[] msg, Session session, boolean last)
        throws IOException {
    if (stream == null) {
        stream = session.getBasicRemote().getSendStream();
    }
    stream.write(msg);
    stream.flush();
    if (last) {
        stream.close();
        stream = null;
    }
}
 
Example #6
Source File: EchoAnnotation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void echoBinaryMessage(Session session, ByteBuffer bb,
        boolean last) {
    try {
        if (session.isOpen()) {
            session.getBasicRemote().sendBinary(bb, last);
        }
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #7
Source File: EchoAnnotation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void echoTextMessage(Session session, String msg, boolean last) {
    try {
        if (session.isOpen()) {
            session.getBasicRemote().sendText(msg, last);
        }
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #8
Source File: TesterEchoServer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage(maxMessageSize = MAX_SIZE)
public void echoTextMessage(Session session, String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #9
Source File: TestWsWebSocketContainer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@SuppressWarnings("unused")
@OnMessage
public void echoTextMessage(Session session, String msg, boolean last) {
    try {
        synchronized (monitor) {
            while (block) {
                monitor.wait();
            }
        }
    } catch (InterruptedException e) {
        // Ignore
    }
}
 
Example #10
Source File: TesterEchoServer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage(maxMessageSize = MAX_SIZE)
public void echoBinaryMessage(Session session, ByteBuffer msg) {
    try {
        session.getBasicRemote().sendBinary(msg);
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #11
Source File: TesterEchoServer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void echoTextMessage(Session session, @SuppressWarnings("unused") String msg) {
    try {
        session.getBasicRemote().getSendWriter();
        // Simulate an error
        throw new RuntimeException();
    } catch (IOException e) {
        // Should not happen
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #12
Source File: TesterEchoServer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void echoTextMessage(Session session, String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #13
Source File: TestEncodingDecoding.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public String onMessage(String message, Session session) throws IOException {
    received.add(message);
    session.getAsyncRemote().setBatchingAllowed(true);
    session.getAsyncRemote().sendText(MESSAGE_ONE);
    return MESSAGE_TWO;
}
 
Example #14
Source File: TestClassLoader.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void onMessage(String msg) {
    if (!failed && !PASS.equals(msg)) {
        failed = true;
    }
    msgCount.incrementAndGet();
}
 
Example #15
Source File: TestClassLoader.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public String onMessage(@SuppressWarnings("unused") String msg) {
    if (Thread.currentThread().getContextClassLoader() instanceof WebappClassLoaderBase) {
        return PASS;
    } else {
        return FAIL;
    }
}
 
Example #16
Source File: TesterEchoServer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage(maxMessageSize = MAX_SIZE)
public void echoBinaryMessage(Session session, ByteBuffer msg) {
    try {
        session.getBasicRemote().sendBinary(msg);
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #17
Source File: ChatAnnotation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void incoming(String message) {
    // Never trust the client
    String filteredMessage = String.format("%s: %s",
            nickname, HTMLFilter.filter(message.toString()));
    broadcast(filteredMessage);
}
 
Example #18
Source File: EchoStreamAnnotation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void echoBinaryMessage(byte[] msg, Session session, boolean last)
        throws IOException {
    if (stream == null) {
        stream = session.getBasicRemote().getSendStream();
    }
    stream.write(msg);
    stream.flush();
    if (last) {
        stream.close();
        stream = null;
    }
}
 
Example #19
Source File: TestWsWebSocketContainer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@SuppressWarnings("unused")
@OnMessage
public void echoBinaryMessage(Session session, ByteBuffer msg,
        boolean last) {
    try {
        synchronized (monitor) {
            while (block) {
                monitor.wait();
            }
        }
    } catch (InterruptedException e) {
        // Ignore
    }
}
 
Example #20
Source File: WebSocket.java    From teaching with Apache License 2.0 5 votes vote down vote up
@OnMessage
public void onMessage(String message) {
    //todo 现在有个定时任务刷,应该去掉
	log.debug("【websocket消息】收到客户端消息:"+message);
	JSONObject obj = new JSONObject();
	obj.put("cmd", "heartcheck");//业务类型
	obj.put("msgTxt", "心跳响应");//消息内容
	session.getAsyncRemote().sendText(obj.toJSONString());
}
 
Example #21
Source File: TesterFirehoseServer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void onMessage(Session session, String msg) throws IOException {

    if (started) {
        return;
    }
    synchronized (this) {
        if (started) {
            return;
        } else {
            started = true;
        }
    }

    System.out.println("Received " + msg + ", now sending data");

    session.getUserProperties().put(
            org.apache.tomcat.websocket.Constants.BLOCKING_SEND_TIMEOUT_PROPERTY,
            Long.valueOf(SEND_TIME_OUT_MILLIS));

    Basic remote = session.getBasicRemote();
    remote.setBatchingAllowed(true);

    for (int i = 0; i < MESSAGE_COUNT; i++) {
        remote.sendText(MESSAGE);
        if (i % (MESSAGE_COUNT * 0.4) == 0) {
            remote.setBatchingAllowed(false);
            remote.setBatchingAllowed(true);
        }
    }

    // Flushing should happen automatically on session close
    session.close();
}
 
Example #22
Source File: TesterWsClientAutobahn.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void echoTextMessage(Session session, String msg, boolean last) {
    try {
        if (session.isOpen()) {
            session.getBasicRemote().sendText(msg, last);
        }
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #23
Source File: TesterWsClientAutobahn.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@OnMessage
public void echoBinaryMessage(Session session, ByteBuffer bb,
        boolean last) {
    try {
        if (session.isOpen()) {
            session.getBasicRemote().sendBinary(bb, last);
        }
    } catch (IOException e) {
        try {
            session.close();
        } catch (IOException e1) {
            // Ignore
        }
    }
}
 
Example #24
Source File: StressEndpoint.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@OnMessage
public void handleMessage(Session session, final String message) throws IOException {
    if(closed != null) {
        System.out.println("closed message " + closed);
    }
    if(message.equals("close")) {
        closed = Thread.currentThread().getName();
        session.close();
        return;
    }
    MESSAGES.add(message);
}
 
Example #25
Source File: WebSocket.java    From jeecg-boot-with-activiti with MIT License 5 votes vote down vote up
@OnMessage
public void onMessage(String message) {
	//log.info("【websocket消息】收到客户端消息:"+message);
	JSONObject obj = new JSONObject();
	obj.put("cmd", "heartcheck");//业务类型
	obj.put("msgTxt", "心跳响应");//消息内容
	session.getAsyncRemote().sendText(obj.toJSONString());
}
 
Example #26
Source File: ErrorEndpoint.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@OnMessage
public void handleMessage(final String message) throws IOException {
    QUEUE.add(message);
    if(message.equals("app-error")) {
        throw new RuntimeException("an error");
    } else if(message.equals("io-error")) {
        throw new IOException();
    }
}
 
Example #27
Source File: AutobahnAnnotatedEndpoint.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@OnMessage
public void handleMessage(final String message, Session session, boolean last) throws IOException {
    if (writer == null) {
        writer = session.getBasicRemote().getSendWriter();
    }
    writer.write(message);
    if (last) {
        writer.close();
        writer = null;
    }
}
 
Example #28
Source File: AutobahnAnnotatedEndpoint.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@OnMessage
public void handleMessage(final byte[] message, Session session, boolean last) throws IOException {
    if (stream == null) {
        stream = session.getBasicRemote().getSendStream();
    }
    stream.write(message);
    stream.flush();
    if (last) {
        stream.close();
        stream = null;
    }
}
 
Example #29
Source File: DisconnectServerEndpoint.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@OnMessage
public String text(String message, Session session) throws IOException {
    if(message.equals("close")) {
        session.close();
        return null;
    }
    return "ECHO-" + message;
}
 
Example #30
Source File: WebSocket.java    From jeecg-cloud with Apache License 2.0 5 votes vote down vote up
@OnMessage
public void onMessage(String message) {
    //todo 现在有个定时任务刷,应该去掉
	log.debug("【websocket消息】收到客户端消息:"+message);
	JSONObject obj = new JSONObject();
	obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_CHECK);//业务类型
	obj.put(WebsocketConst.MSG_TXT, "心跳响应");//消息内容
	session.getAsyncRemote().sendText(obj.toJSONString());
}