Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/example/ServerStressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.awt.event.ActionListener;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.NotYetConnectedException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -47,6 +46,7 @@
import javax.swing.event.ChangeListener;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.exceptions.WebsocketNotConnectedException;

public class ServerStressTest extends JFrame {
private JSlider clients;
Expand Down Expand Up @@ -225,7 +225,7 @@ public void send() {
for( WebSocketClient cl : websockets ) {
try {
cl.send( payload );
} catch ( NotYetConnectedException e ) {
} catch ( WebsocketNotConnectedException e ) {
notyetconnected++;
}
}
Expand Down
37 changes: 23 additions & 14 deletions src/main/java/org/java_websocket/AbstractWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,8 @@ public void run() {
try {
connections.addAll( getConnections() );
long current = ( System.currentTimeMillis() - ( connectionLostTimeout * 1500 ) );
WebSocketImpl webSocketImpl;
for( WebSocket conn : connections ) {
if( conn instanceof WebSocketImpl ) {
webSocketImpl = ( WebSocketImpl ) conn;
if( webSocketImpl.getLastPong() < current ) {
log.trace("Closing connection due to no pong received: {}", conn);
webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE, "The connection was closed because the other endpoint did not respond with a pong in time. For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection" );
} else {
if( webSocketImpl.isOpen() ) {
webSocketImpl.sendPing();
} else {
log.trace("Trying to ping a non open connection: {}", conn);
}
}
}
executeConnectionLostDetection(conn, current);
}
} catch ( Exception e ) {
//Ignore this exception
Expand All @@ -195,6 +182,28 @@ public void run() {

}

/**
* Send a ping to the endpoint or close the connection since the other endpoint did not respond with a ping
* @param webSocket the websocket instance
* @param current the current time in milliseconds
*/
private void executeConnectionLostDetection(WebSocket webSocket, long current) {
if (!(webSocket instanceof WebSocketImpl)) {
return;
}
WebSocketImpl webSocketImpl = (WebSocketImpl) webSocket;
if( webSocketImpl.getLastPong() < current ) {
log.trace("Closing connection due to no pong received: {}", webSocketImpl);
webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE, "The connection was closed because the other endpoint did not respond with a pong in time. For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection" );
} else {
if( webSocketImpl.isOpen() ) {
webSocketImpl.sendPing();
} else {
log.trace("Trying to ping a non open connection: {}", webSocketImpl);
}
}
}

/**
* Getter to get all the currently available connections
* @return the currently available connections
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/java_websocket/AbstractWrappedByteChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,26 @@
import java.nio.channels.ByteChannel;
import java.nio.channels.SocketChannel;

/*
* @deprecated
*/
@Deprecated
public class AbstractWrappedByteChannel implements WrappedByteChannel {

private final ByteChannel channel;

/*
* @deprecated
*/
@Deprecated
public AbstractWrappedByteChannel( ByteChannel towrap ) {
this.channel = towrap;
}

/*
* @deprecated
*/
@Deprecated
public AbstractWrappedByteChannel( WrappedByteChannel towrap ) {
this.channel = towrap;
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/java_websocket/SSLSocketChannel2.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,14 @@ public SSLSocketChannel2( SocketChannel channel , SSLEngine sslEngine , Executor

private void consumeFutureUninterruptible( Future<?> f ) {
try {
boolean interrupted = false;
while ( true ) {
try {
f.get();
break;
} catch ( InterruptedException e ) {
interrupted = true;
Thread.currentThread().interrupt();
}
}
if( interrupted )
Thread.currentThread().interrupt();
} catch ( ExecutionException e ) {
throw new RuntimeException( e );
}
Expand Down
26 changes: 6 additions & 20 deletions src/main/java/org/java_websocket/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,16 @@

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.NotYetConnectedException;
import java.util.Collection;

import org.java_websocket.drafts.Draft;
import org.java_websocket.enums.Opcode;
import org.java_websocket.enums.ReadyState;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.framing.Framedata;

public interface WebSocket {

/**
* The default port of WebSockets, as defined in the spec. If the nullary
* constructor is used, DEFAULT_PORT will be the port the WebSocketServer
* is binded to. Note that ports under 1024 usually require root permissions.
*/
int DEFAULT_PORT = 80;

/**
* The default wss port of WebSockets, as defined in the spec. If the nullary
* constructor is used, DEFAULT_WSS_PORT will be the port the WebSocketServer
* is binded to. Note that ports under 1024 usually require root permissions.
*/
int DEFAULT_WSS_PORT = 443;

/**
* sends the closing handshake.
* may be send in response to an other handshake.
Expand Down Expand Up @@ -81,7 +67,7 @@ public interface WebSocket {
* Send Text data to the other end.
*
* @param text the text data to send
* @throws NotYetConnectedException websocket is not yet connected
* @throws WebsocketNotConnectedException websocket is not yet connected
*/
void send( String text );

Expand All @@ -90,7 +76,7 @@ public interface WebSocket {
*
* @param bytes the binary data to send
* @throws IllegalArgumentException the data is null
* @throws NotYetConnectedException websocket is not yet connected
* @throws WebsocketNotConnectedException websocket is not yet connected
*/
void send( ByteBuffer bytes );

Expand All @@ -99,7 +85,7 @@ public interface WebSocket {
*
* @param bytes the byte array to send
* @throws IllegalArgumentException the data is null
* @throws NotYetConnectedException websocket is not yet connected
* @throws WebsocketNotConnectedException websocket is not yet connected
*/
void send( byte[] bytes );

Expand All @@ -117,9 +103,9 @@ public interface WebSocket {

/**
* Send a ping to the other end
* @throws NotYetConnectedException websocket is not yet connected
* @throws WebsocketNotConnectedException websocket is not yet connected
*/
void sendPing() throws NotYetConnectedException;
void sendPing();

/**
* Allows to send continuous/fragmented frames conveniently. <br>
Expand Down
Loading