Search in sources :

Example 1 with OnMessage

use of javax.websocket.OnMessage in project jetty.project by eclipse.

the class ConfiguredEchoSocket method echoBinary.

@OnMessage(maxMessageSize = 333444)
public ByteBuffer echoBinary(ByteBuffer buf) {
    // this one isn't that important, just here to satisfy the @OnMessage
    // settings that we actually test for via "binary-max" TEXT message
    ByteBuffer ret = buf.slice();
    ret.flip();
    return ret;
}
Also used : ByteBuffer(java.nio.ByteBuffer) OnMessage(javax.websocket.OnMessage)

Example 2 with OnMessage

use of javax.websocket.OnMessage in project jetty.project by eclipse.

the class SessionInfoSocket method onMessage.

@OnMessage
public String onMessage(Session session, String message) {
    if ("pathParams".equalsIgnoreCase(message)) {
        StringBuilder ret = new StringBuilder();
        ret.append("pathParams");
        Map<String, String> pathParams = session.getPathParameters();
        if (pathParams == null) {
            ret.append("=<null>");
        } else {
            ret.append('[').append(pathParams.size()).append(']');
            List<String> keys = new ArrayList<>();
            for (String key : pathParams.keySet()) {
                keys.add(key);
            }
            Collections.sort(keys);
            for (String key : keys) {
                String value = pathParams.get(key);
                ret.append(": '").append(key).append("'=").append(value);
            }
        }
        return ret.toString();
    }
    if ("requestUri".equalsIgnoreCase(message)) {
        StringBuilder ret = new StringBuilder();
        ret.append("requestUri=");
        URI uri = session.getRequestURI();
        if (uri == null) {
            ret.append("=<null>");
        } else {
            ret.append(uri.toASCIIString());
        }
        return ret.toString();
    }
    // simple echo
    return "echo:'" + message + "'";
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) OnMessage(javax.websocket.OnMessage)

Example 3 with OnMessage

use of javax.websocket.OnMessage in project jetty.project by eclipse.

the class AnnotatedEndpointScanner method onMethodAnnotation.

@Override
public void onMethodAnnotation(AnnotatedEndpointMetadata<T, C> metadata, Class<?> pojo, Method method, Annotation annotation) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("onMethodAnnotation({}, {}, {}, {})", metadata, pojo, method, annotation);
    }
    if (isAnnotation(annotation, OnOpen.class)) {
        assertIsPublicNonStatic(method);
        assertIsReturn(method, Void.TYPE);
        assertNotDuplicate(metadata.onOpen, OnOpen.class, pojo, method);
        OnOpenCallable onopen = new OnOpenCallable(pojo, method);
        visitMethod(onopen, pojo, method, paramsOnOpen, OnOpen.class);
        metadata.onOpen = onopen;
        return;
    }
    if (isAnnotation(annotation, OnClose.class)) {
        assertIsPublicNonStatic(method);
        assertIsReturn(method, Void.TYPE);
        assertNotDuplicate(metadata.onClose, OnClose.class, pojo, method);
        OnCloseCallable onclose = new OnCloseCallable(pojo, method);
        visitMethod(onclose, pojo, method, paramsOnClose, OnClose.class);
        metadata.onClose = onclose;
        return;
    }
    if (isAnnotation(annotation, OnError.class)) {
        assertIsPublicNonStatic(method);
        assertIsReturn(method, Void.TYPE);
        assertNotDuplicate(metadata.onError, OnError.class, pojo, method);
        OnErrorCallable onerror = new OnErrorCallable(pojo, method);
        visitMethod(onerror, pojo, method, paramsOnError, OnError.class);
        metadata.onError = onerror;
        return;
    }
    if (isAnnotation(annotation, OnMessage.class)) {
        assertIsPublicNonStatic(method);
        // assertIsReturn(method,Void.TYPE); // no validation, it can be any return type
        OnMessageCallable onmessage = new OnMessageCallable(pojo, method);
        visitMethod(onmessage, pojo, method, paramsOnMessage, OnMessage.class);
        OnMessage messageAnno = (OnMessage) annotation;
        Param param = onmessage.getMessageObjectParam();
        switch(param.role) {
            case MESSAGE_BINARY:
                metadata.onBinary = new OnMessageBinaryCallable(onmessage);
                metadata.setMaxBinaryMessageSize(messageAnno.maxMessageSize());
                break;
            case MESSAGE_BINARY_STREAM:
                metadata.onBinaryStream = new OnMessageBinaryStreamCallable(onmessage);
                metadata.setMaxBinaryMessageSize(messageAnno.maxMessageSize());
                break;
            case MESSAGE_TEXT:
                metadata.onText = new OnMessageTextCallable(onmessage);
                metadata.setMaxTextMessageSize(messageAnno.maxMessageSize());
                break;
            case MESSAGE_TEXT_STREAM:
                metadata.onTextStream = new OnMessageTextStreamCallable(onmessage);
                metadata.setMaxTextMessageSize(messageAnno.maxMessageSize());
                break;
            case MESSAGE_PONG:
                metadata.onPong = new OnMessagePongCallable(onmessage);
                break;
            default:
                StringBuilder err = new StringBuilder();
                err.append("An unrecognized message type <");
                err.append(param.type);
                err.append(">: does not meet specified type categories of [TEXT, BINARY, DECODER, or PONG]");
                throw new InvalidSignatureException(err.toString());
        }
    }
}
Also used : InvalidSignatureException(org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException) OnMessage(javax.websocket.OnMessage)

Example 4 with OnMessage

use of javax.websocket.OnMessage in project tomcat by apache.

the class EchoAsyncAnnotation method echoBinaryMessage.

@OnMessage
public void echoBinaryMessage(byte[] msg, Session session, boolean last) throws IOException {
    if (bytes == null) {
        bytes = new ByteArrayOutputStream();
    }
    bytes.write(msg);
    if (last) {
        // message to complete
        try {
            f.get();
        } catch (InterruptedException | ExecutionException e) {
            // Let the container deal with it
            throw new RuntimeException(e);
        }
        f = session.getAsyncRemote().sendBinary(ByteBuffer.wrap(bytes.toByteArray()));
        bytes = null;
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ExecutionException(java.util.concurrent.ExecutionException) OnMessage(javax.websocket.OnMessage)

Example 5 with OnMessage

use of javax.websocket.OnMessage in project jetty.project by eclipse.

the class JsrBrowserSocket method onMessage.

@OnMessage
public void onMessage(String message) {
    LOG.info("onTextMessage({})", message);
    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":
                {
                    writeMessage("Using javax.websocket");
                    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 Sec-WebSocket-Extensions: " + this.requestedExtensions);
                    }
                    Set<Session> openSessions = session.getOpenSessions();
                    writeMessage("OpenSessions.size() = " + openSessions.size());
                    int i = 0;
                    for (Session open : openSessions) {
                        writeMessage("  OpenSession[%d] = %s", i++, open);
                    }
                    break;
                }
            case "many":
                {
                    String[] parts = StringUtil.csvSplit(val);
                    int size = Integer.parseInt(parts[0]);
                    int count = Integer.parseInt(parts[1]);
                    writeManyAsync(size, count);
                    break;
                }
            case "manythreads":
                {
                    String[] parts = StringUtil.csvSplit(val);
                    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(remote, 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);
                }
        }
    } else {
        // Not parameterized, echo it back
        writeMessage(message);
    }
}
Also used : Set(java.util.Set) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServerEndpoint(javax.websocket.server.ServerEndpoint) Session(javax.websocket.Session) OnMessage(javax.websocket.OnMessage)

Aggregations

OnMessage (javax.websocket.OnMessage)8 ServerEndpoint (javax.websocket.server.ServerEndpoint)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1 Session (javax.websocket.Session)1 Device (org.ecabrerar.examples.websockethome.model.Device)1