Search in sources :

Example 6 with OnWebSocketMessage

use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.

the class BrowserSocket method onTextMessage.

@OnWebSocketMessage
public void onTextMessage(String message) {
    if (message.length() > 300) {
        int len = message.length();
        LOG.info("onTextMessage({} ... {}) size:{}", message.substring(0, 15), message.substring(len - 15, len).replaceAll("[\r\n]*", ""), len);
    } else {
        LOG.info("onTextMessage({})", message);
    }
    // Is multi-line?
    if (message.contains("\n")) {
        // echo back exactly
        writeMessage(message);
        return;
    }
    // Is resource lookup?
    if (message.charAt(0) == '@') {
        String name = message.substring(1);
        URL url = Loader.getResource(name);
        if (url == null) {
            writeMessage("Unable to find resource: " + name);
            return;
        }
        try (InputStream in = url.openStream()) {
            String data = IO.toString(in);
            writeMessage(data);
        } catch (IOException e) {
            writeMessage("Unable to read resource: " + name);
            LOG.warn("Unable to read resource: " + name, e);
        }
        return;
    }
    // Is parameterized?
    int idx = message.indexOf(':');
    if (idx > 0) {
        String key = message.substring(0, idx).toLowerCase(Locale.ENGLISH);
        String val = message.substring(idx + 1);
        switch(key) {
            case "info":
                {
                    if (StringUtil.isBlank(userAgent)) {
                        writeMessage("Client has no User-Agent");
                    } else {
                        writeMessage("Client User-Agent: " + this.userAgent);
                    }
                    if (StringUtil.isBlank(requestedExtensions)) {
                        writeMessage("Client requested no Sec-WebSocket-Extensions");
                    } else {
                        writeMessage("Client requested Sec-WebSocket-Extensions: " + this.requestedExtensions);
                        writeMessage("Negotiated Sec-WebSocket-Extensions: " + session.getUpgradeResponse().getHeader("Sec-WebSocket-Extensions"));
                    }
                    break;
                }
            case "many":
                {
                    String[] parts = val.split(",");
                    int size = Integer.parseInt(parts[0]);
                    int count = Integer.parseInt(parts[1]);
                    writeManyAsync(size, count);
                    break;
                }
            case "manythreads":
                {
                    String[] parts = val.split(",");
                    int threadCount = Integer.parseInt(parts[0]);
                    int size = Integer.parseInt(parts[1]);
                    int count = Integer.parseInt(parts[2]);
                    Thread[] threads = new Thread[threadCount];
                    // Setup threads
                    for (int n = 0; n < threadCount; n++) {
                        threads[n] = new Thread(new WriteMany(session.getRemote(), size, count), "WriteMany[" + n + "]");
                    }
                    // Execute threads
                    for (Thread thread : threads) {
                        thread.start();
                    }
                    // Drop out of this thread
                    break;
                }
            case "time":
                {
                    Calendar now = Calendar.getInstance();
                    DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL);
                    writeMessage("Server time: %s", sdf.format(now.getTime()));
                    break;
                }
            default:
                {
                    writeMessage("key[%s] val[%s]", key, val);
                }
        }
        return;
    }
    // Not parameterized, echo it back as-is
    writeMessage(message);
}
Also used : InputStream(java.io.InputStream) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) IOException(java.io.IOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) URL(java.net.URL) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Example 7 with OnWebSocketMessage

use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.

the class BigEchoSocket method onBinary.

@OnWebSocketMessage
public void onBinary(Session session, byte[] buf, int offset, int length) throws IOException {
    if (!session.isOpen()) {
        LOG.warn("Session is closed");
        return;
    }
    RemoteEndpoint remote = session.getRemote();
    remote.sendBytes(ByteBuffer.wrap(buf, offset, length), null);
    if (remote.getBatchMode() == BatchMode.ON)
        remote.flush();
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Example 8 with OnWebSocketMessage

use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.

the class EchoSocket method onBinary.

@OnWebSocketMessage
public void onBinary(byte[] buf, int offset, int len) throws IOException {
    LOG.debug("onBinary(byte[{}],{},{})", buf.length, offset, len);
    // echo the message back.
    ByteBuffer data = ByteBuffer.wrap(buf, offset, len);
    RemoteEndpoint remote = this.session.getRemote();
    remote.sendBytes(data, null);
    if (remote.getBatchMode() == BatchMode.ON)
        remote.flush();
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) ByteBuffer(java.nio.ByteBuffer) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Example 9 with OnWebSocketMessage

use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.

the class EchoSocket method onText.

@OnWebSocketMessage
public void onText(String message) throws IOException {
    LOG.debug("onText({})", message);
    // echo the message back.
    RemoteEndpoint remote = session.getRemote();
    remote.sendString(message, null);
    if (remote.getBatchMode() == BatchMode.ON)
        remote.flush();
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Example 10 with OnWebSocketMessage

use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.

the class RFCSocket method onBinary.

@OnWebSocketMessage
public void onBinary(byte[] buf, int offset, int len) throws IOException {
    LOG.debug("onBinary(byte[{}],{},{})", buf.length, offset, len);
    // echo the message back.
    ByteBuffer data = ByteBuffer.wrap(buf, offset, len);
    RemoteEndpoint remote = session.getRemote();
    remote.sendBytes(data, null);
    if (remote.getBatchMode() == BatchMode.ON)
        remote.flush();
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) ByteBuffer(java.nio.ByteBuffer) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Aggregations

OnWebSocketMessage (org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)16 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)9 ByteBuffer (java.nio.ByteBuffer)4 Gson (com.google.gson.Gson)3 JsonObject (com.google.gson.JsonObject)3 IOException (java.io.IOException)2 WebSocketMessage (org.springframework.web.reactive.socket.WebSocketMessage)2 JsonPrimitive (com.google.gson.JsonPrimitive)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 List (java.util.List)1 Document (org.dom4j.Document)1 XMPPPacketReader (org.dom4j.io.XMPPPacketReader)1 UnknownStanzaException (org.jivesoftware.openfire.multiplex.UnknownStanzaException)1