实例介绍
【实例截图】
【核心代码】
/*
* 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;
}
标签: websocket
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论