1
我已成功爲tomcat websockets實現了一個簡單的echo服務器示例。現在我需要從我的服務器向客戶端發送連續的消息。我如何實現這一目標?Apache Tomcat websockets服務器端消息
目前服務器端代碼
package org.playjava.websocket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;
public class SimpleEchoServlet extends WebSocketServlet {
@Override
protected StreamInbound createWebSocketInbound(String string, HttpServletRequest hsr) {
return new SimpleEchoInbound();
}
private static final class SimpleEchoInbound extends MessageInbound {
public SimpleEchoInbound() {
super();
}
@Override
protected void onBinaryMessage(ByteBuffer message) throws IOException {
getWsOutbound().writeBinaryMessage(message);
}
@Override
protected void onTextMessage(CharBuffer message) throws IOException {
getWsOutbound().writeTextMessage(message);
getWsOutbound().writeTextMessage(message);
getWsOutbound().writeTextMessage(message);
}
}
}