Java Websocket客户端作为Servlet(App引擎)

时间:2019-02-12 00:12:56

标签: java sockets google-app-engine websocket

我正在尝试在servlet中建立WebSocket连接。我从服务器收到一些回调消息,但很少。我调用Thread.sleep(30000),因此servlet应该等待30秒以接收新消息,但似乎由于某种原因而不是等待。

这是我的Servlet中的代码:

try {
    // open websocket
    final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI("wss://www.bitmex.com/realtime"));

    // add listener
    clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() {
        public void handleMessage(String message) {


            System.out.println(message);

            logger.info("WEBSOCKET MSG: "+message);

        }
    });

    // send message to websocket
    clientEndPoint.sendMessage("{\"op\": \"subscribe\", \"args\": \"trade\"}");

    // wait 30 seconds for messages from websocket
    Thread.sleep(30000);

} catch (InterruptedException ex) {
    System.err.println("InterruptedException exception: " + ex.getMessage());
} catch (URISyntaxException ex) {
    System.err.println("URISyntaxException exception: " + ex.getMessage());
}

这是我的WebsocketClientEndpoint代码:

import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

@ClientEndpoint
public class WebsocketClientEndpoint {

    Session userSession = null;
    private MessageHandler messageHandler;

    public WebsocketClientEndpoint(URI endpointURI) {
        try {
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            container.connectToServer(this, endpointURI);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Callback hook for Connection open events.
     *
     * @param userSession the userSession which is opened.
     */
    @OnOpen
    public void onOpen(Session userSession) {
        System.out.println("opening websocket");
        this.userSession = userSession;
    }

    /**
     * Callback hook for Connection close events.
     *
     * @param userSession the userSession which is getting closed.
     * @param reason the reason for connection close
     */
    @OnClose
    public void onClose(Session userSession, CloseReason reason) {
        System.out.println("closing websocket");
        this.userSession = null;
    }

    /**
     * Callback hook for Message Events. This method will be invoked when a client send a message.
     *
     * @param message The text message
     */
    @OnMessage
    public void onMessage(String message) {
        if (this.messageHandler != null) {
            this.messageHandler.handleMessage(message);
        }
    }

    public void addMessageHandler(MessageHandler msgHandler) {
        this.messageHandler = msgHandler;
    }

    public void sendMessage(String message) {
        this.userSession.getAsyncRemote().sendText(message);
    }

    public static interface MessageHandler {

        public void handleMessage(String message);
    }
}

什么是正确的方法?还是这可能是App Engine(标准环境)出现的问题?

0 个答案:

没有答案