ypc 发表于 2017-3-30 15:28:17

tcp协议

package websocket.echo;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import javax.servlet.ServletException;
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 EchoMessage extends WebSocketServlet
{
private static final long serialVersionUID = 1L;
private volatile int byteBufSize;
private volatile int charBufSize;

public void init()
    throws ServletException
{
    super.init();
    this.byteBufSize = getInitParameterIntValue("byteBufferMaxSize", 2097152);
    this.charBufSize = getInitParameterIntValue("charBufferMaxSize", 2097152);
}

public int getInitParameterIntValue(String name, int defaultValue) {
    String val = getInitParameter(name);
    int result;
    if (null != val)
      try {
      result = Integer.parseInt(val);
      } catch (Exception x) {
      int result = defaultValue;
      }
    else {
      result = defaultValue;
    }

    return result;
}

protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request)
{
    return new EchoMessageInbound(this.byteBufSize, this.charBufSize);
}

private static final class EchoMessageInbound extends MessageInbound
{
    public EchoMessageInbound(int byteBufferMaxSize, int charBufferMaxSize)
    {
      setByteBufferMaxSize(byteBufferMaxSize);
      setCharBufferMaxSize(charBufferMaxSize);
    }

    protected void onBinaryMessage(ByteBuffer message) throws IOException
    {
      getWsOutbound().writeBinaryMessage(message);
    }

    protected void onTextMessage(CharBuffer message) throws IOException
    {
      getWsOutbound().writeTextMessage(message);
    }
}
}

页: [1]
查看完整版本: tcp协议