简体   繁体   中英

Multiple messages on a Grizzly Websocket Connection

We are using Websockets from the Grizzly project and had expected that the implementation would allow multiple incoming messages over a connection to be processed at the same time. It appears that this is not the case or there is a configuration step that we have missed. To validate this I have created a modified echo test that delays in the onMessage after echoing the text. When a client sends multiple messages over the same connection the server always blocks until onMessage completes before processing a subsequent message. Is this the expected functionality?

The simplified server code is as follows:

package com.grorange.samples.echo;
import java.util.concurrent.atomic.AtomicBoolean;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;
import org.glassfish.grizzly.websockets.DataFrame;
import org.glassfish.grizzly.websockets.WebSocket;
import org.glassfish.grizzly.websockets.WebSocketAddOn;
import org.glassfish.grizzly.websockets.WebSocketApplication;
import org.glassfish.grizzly.websockets.WebSocketEngine;

public class Echo extends WebSocketApplication {
    private final AtomicBoolean inMessage = new AtomicBoolean(false);

    @Override
    public void onClose(WebSocket socket, DataFrame frame) {
        super.onClose(socket, frame);
        System.out.println("Disconnected!");
    }

    @Override
    public void onConnect(WebSocket socket) {
        System.out.println("Connected!");
    }

    @Override
    public void onMessage(WebSocket socket, String text) {
        System.out.println("Server: " + text);
        socket.send(text);
        if (this.inMessage.compareAndSet(false, true)) {
            try {
                Thread.sleep(10000);
            } catch (Exception e) {}

            this.inMessage.set(false);
        }
    }

    @Override
    public void onMessage(WebSocket socket, byte[] bytes) {
        socket.send(bytes);
        if (this.inMessage.compareAndSet(false, true)) {
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (Exception e) {}

            this.inMessage.set(false);
        }

    }

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.createSimpleServer("http://0.0.0.0", 8083);
        WebSocketAddOn addOn = new WebSocketAddOn();
        addOn.setTimeoutInSeconds(60);
        for (NetworkListener listener : server.getListeners()) {
            listener.registerAddOn(addOn);
        }

        WebSocketEngine.getEngine().register("", "/Echo", new Echo());
        server.start();
        Thread.sleep(Long.MAX_VALUE);
    }
}

The simplified client code is:

Yes, it's expected. The way to go is to pass message processing, inside onMessage, to a different thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM