首页

java websocket实例

java

2020-8-19

websocket实例

/*
 * WebSocket.java
 * Copyright 2014 Patrick Meade.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.pmeade.websocket.net;

import com.pmeade.websocket.io.WebSocketServerInputStream;
import com.pmeade.websocket.io.WebSocketServerOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.channels.SocketChannel;

/**
 * WebSocket decorates Socket to provide the server side of a Socket that
 * speaks the WebSocket (RFC 6455) protocol.
 * @author veloxi
 */
public class WebSocket extends Socket {
    /**
     * Construct a WebSocket. WebSocket decorates a Socket for WebSocket
     * (RFC 6455) behavior.
     * @param s Socket to be decorated with WebSocket behavior.
     */
    public WebSocket(final Socket s) {
        this.socket = s;
    }

    @Override
    public final void connect(final SocketAddress endpoint) throws IOException {
        socket.connect(endpoint);
    }

    @Override
    public final void connect(final SocketAddress endpoint, final int timeout)
            throws IOException {
        socket.connect(endpoint, timeout);
    }

    @Override
    public final void bind(final SocketAddress bindpoint) throws IOException {
        socket.bind(bindpoint);
    }

    @Override
    public final InetAddress getInetAddress() {
        return socket.getInetAddress();
    }

    @Override
    public final InetAddress getLocalAddress() {
        return socket.getLocalAddress();
    }

    @Override
    public final int getPort() {
        return socket.getPort();
    }

    @Override
    public final int getLocalPort() {
        return socket.getLocalPort();
    }

    @Override
    public final SocketAddress getRemoteSocketAddress() {
        return socket.getRemoteSocketAddress();
    }

    @Override
    public final SocketAddress getLocalSocketAddress() {
        return socket.getLocalSocketAddress();
    }

    @Override
    public final SocketChannel getChannel() {
        throw new UnsupportedOperationException();
    }

    @Override
    public final WebSocketServerInputStream getInputStream()
            throws IOException {
        if (wssos == null) {
            this.getOutputStream();
        }
        if (wssis == null) {
            wssis = new WebSocketServerInputStream(
                        socket.getInputStream(), wssos);
        }
        return wssis;
    }

    @Override
    public final WebSocketServerOutputStream getOutputStream()
            throws IOException {
        if (wssos == null) {
            wssos = new WebSocketServerOutputStream(socket.getOutputStream());
        }
        return wssos;
    }

    @Override
    public final void setTcpNoDelay(final boolean on) throws SocketException {
        socket.setTcpNoDelay(on);
    }

    @Override
    public final boolean getTcpNoDelay() throws SocketException {
        return socket.getTcpNoDelay();
    }

    @Override
    public final void setSoLinger(final boolean on, final int linger)
            throws SocketException {
        socket.setSoLinger(on, linger);
    }

    @Override
    public final int getSoLinger() throws SocketException {
        return socket.getSoLinger();
    }

    @Override
    public final void sendUrgentData(final int data)
            throws IOException {
        socket.sendUrgentData(data);
    }

    @Override
    public final void setOOBInline(final boolean on)
            throws SocketException {
        socket.setOOBInline(on);
    }

    @Override
    public final boolean getOOBInline() throws SocketException {
        return socket.getOOBInline();
    }

    @Override
    public final synchronized void setSoTimeout(final int timeout)
            throws SocketException {
        socket.setSoTimeout(timeout);
    }

    @Override
    public final synchronized int getSoTimeout() throws SocketException {
        return socket.getSoTimeout();
    }

    @Override
    public final synchronized void setSendBufferSize(final int size)
            throws SocketException {
        socket.setSendBufferSize(size);
    }

    @Override
    public final synchronized int getSendBufferSize() throws SocketException {
        return socket.getSendBufferSize();
    }

    @Override
    public final synchronized void setReceiveBufferSize(final int size)
            throws SocketException {
        socket.setReceiveBufferSize(size);
    }

    @Override
    public final synchronized int getReceiveBufferSize()
            throws SocketException {
        return socket.getReceiveBufferSize();
    }

    @Override
    public final void setKeepAlive(final boolean on) throws SocketException {
        socket.setKeepAlive(on);
    }

    @Override
    public final boolean getKeepAlive() throws SocketException {
        return socket.getKeepAlive();
    }

    @Override
    public final void setTrafficClass(final int tc) throws SocketException {
        socket.setTrafficClass(tc);
    }

    @Override
    public final int getTrafficClass() throws SocketException {
        return socket.getTrafficClass();
    }

    @Override
    public final void setReuseAddress(final boolean on) throws SocketException {
        socket.setReuseAddress(on);
    }

    @Override
    public final boolean getReuseAddress() throws SocketException {
        return socket.getReuseAddress();
    }

    @Override
    public final synchronized void close() throws IOException {
        socket.close();
    }

    @Override
    public final void shutdownInput() throws IOException {
        socket.shutdownInput();
    }

    @Override
    public final void shutdownOutput() throws IOException {
        socket.shutdownOutput();
    }

    @Override
    public final String toString() {
        return socket.toString();
    }

    @Override
    public final boolean isConnected() {
        return socket.isConnected();
    }

    @Override
    public final boolean isBound() {
        return socket.isBound();
    }

    @Override
    public final boolean isClosed() {
        return socket.isClosed();
    }

    @Override
    public final boolean isInputShutdown() {
        return socket.isInputShutdown();
    }

    @Override
    public final boolean isOutputShutdown() {
        return socket.isOutputShutdown();
    }

    @Override
    public final void setPerformancePreferences(
            final int connectionTime,
            final int latency,
            final int bandwidth) {
        socket.setPerformancePreferences(connectionTime, latency, bandwidth);
    }

    /**
     * Socket to be decorated for WebSocket behavior.
     */
    private final Socket socket;

    /**
     * WebSocketServerInputStream that decorates the InputStream for
     * this Socket. Created on the first call to <code>getInputStream()</code>.
     */
    private WebSocketServerInputStream wssis = null;

    /**
     * WebSocketServerOutputStream that decorates the OutputStream for
     * this Socket. Created on the first call to <code>getInputStream()</code>
     * or <code>getOutputStream()</code>.
     */
    private WebSocketServerOutputStream wssos = null;
}
资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
websocket-master/.gitignore , 197
websocket-master/LICENSE , 34520
websocket-master/README.md , 5043
websocket-master/doc/rfc2616.txt , 422279
websocket-master/doc/rfc6455.txt , 162067
websocket-master/doc/rfc7230.txt , 205947
websocket-master/doc/rfc7231.txt , 235053
websocket-master/doc/rfc7232.txt , 56696
websocket-master/doc/rfc7233.txt , 46933
websocket-master/doc/rfc7234.txt , 90647
websocket-master/doc/rfc7235.txt , 38142
websocket-master/pom.xml , 4664
websocket-master/src/main/java/com/pmeade/websocket/example/EchoServer.java , 2961
websocket-master/src/main/java/com/pmeade/websocket/http/HttpRequest.java , 6991
websocket-master/src/main/java/com/pmeade/websocket/http/package-info.java , 833
websocket-master/src/main/java/com/pmeade/websocket/io/LineInputStream.java , 6219
websocket-master/src/main/java/com/pmeade/websocket/io/WebSocketServerInputStream.java , 21093
websocket-master/src/main/java/com/pmeade/websocket/io/WebSocketServerOutputStream.java , 12851
websocket-master/src/main/java/com/pmeade/websocket/io/package-info.java , 842
websocket-master/src/main/java/com/pmeade/websocket/net/WebSocket.java , 7788
websocket-master/src/main/java/com/pmeade/websocket/net/WebSocketServerSocket.java , 4227
websocket-master/src/main/java/com/pmeade/websocket/net/package-info.java , 842
websocket-master/src/test/java/com/pmeade/websocket/http/HttpRequestTest.java , 11945
websocket-master/src/test/java/com/pmeade/websocket/io/LineInputStreamTest.java , 6964
websocket-master/src/test/java/com/pmeade/websocket/io/WebSocketServerInputStreamTest.java , 57140
websocket-master/src/test/java/com/pmeade/websocket/io/WebSocketServerOutputStreamTest.java , 13132
websocket-master/src/test/java/com/pmeade/websocket/net/WebSocketServerSocketTest.java , 6249
websocket-master/src/test/java/com/pmeade/websocket/net/WebSocketTest.java , 15350
websocket-master/start-echo-server , 960
没有账号? 忘记密码?

社交账号快速登录