java websocket下载_java websocket实例

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 .

*/

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 getInputStream().

*/

private WebSocketServerInputStream wssis = null;

/**

* WebSocketServerOutputStream that decorates the OutputStream for

* this Socket. Created on the first call to getInputStream()

* or getOutputStream().

*/

private WebSocketServerOutputStream wssos = null;

}

需2积分下载

去下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用Java-WebSocket库来编写多个WebSocket实例。在使用Java-WebSocket库时,需要创建多个WebSocketServer实例,并为每个实例指定不同的端口号。每个WebSocketServer实例都可以处理多个WebSocket连接。在处理WebSocket连接时,可以使用WebSocket类的onOpen、onClose、onMessage和onError方法来处理WebSocket连接的打开、关闭、消息和错误事件。此外,还可以使用WebSocket类的send方法向客户端发送消息。下面是一个简单的Java-WebSocket多个WebSocket实例的示例代码: ``` import java.net.InetSocketAddress; import java.util.concurrent.CopyOnWriteArrayList; import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; public class MultiWebSocketServer { private CopyOnWriteArrayList<WebSocket> webSockets = new CopyOnWriteArrayList<>(); public MultiWebSocketServer(int... ports) { for (int port : ports) { WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) { @Override public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) { webSockets.add(webSocket); System.out.println("WebSocket opened: " + webSocket.getRemoteSocketAddress()); } @Override public void onClose(WebSocket webSocket, int i, String s, boolean b) { webSockets.remove(webSocket); System.out.println("WebSocket closed: " + webSocket.getRemoteSocketAddress()); } @Override public void onMessage(WebSocket webSocket, String s) { System.out.println("WebSocket message received: " + s); } @Override public void onError(WebSocket webSocket, Exception e) { System.out.println("WebSocket error: " + e.getMessage()); } }; server.start(); System.out.println("WebSocket server started on port " + port); } } public void broadcast(String message) { for (WebSocket webSocket : webSockets) { webSocket.send(message); } } public static void main(String[] args) { MultiWebSocketServer server = new MultiWebSocketServer(8080, 8081); server.broadcast("Hello, world!"); } } ``` 此代码创建了两个WebSocketServer实例,分别监听8080和8081端口。在每个WebSocketServer实例的onOpen、onClose、onMessage和onError方法中,分别处理WebSocket连接的打开、关闭、消息和错误事件。在broadcast方法中,使用WebSocket类的send方法向所有客户端发送消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值