Skip to content

Commit 543bd90

Browse files
authored
Merge pull request TooTallNate#455 from marci4/master
Added callback for successfull start of server Switched around call for connect/closing latches
2 parents 7e97e02 + 43c8b14 commit 543bd90

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

src/main/example/ChatServer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public void onError( WebSocket conn, Exception ex ) {
7676
}
7777
}
7878

79+
@Override
80+
public void onStart() {
81+
System.out.println("Server started!");
82+
}
83+
7984
/**
8085
* Sends <var>text</var> to all currently connected WebSocket clients.
8186
*

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,15 @@ public void onWebsocketMessageFragment( WebSocket conn, Framedata frame ) {
271271
*/
272272
@Override
273273
public final void onWebsocketOpen( WebSocket conn, Handshakedata handshake ) {
274-
connectLatch.countDown();
275274
onOpen( (ServerHandshake) handshake );
275+
connectLatch.countDown();
276276
}
277277

278278
/**
279279
* Calls subclass' implementation of <var>onClose</var>.
280280
*/
281281
@Override
282282
public final void onWebsocketClose( WebSocket conn, int code, String reason, boolean remote ) {
283-
connectLatch.countDown();
284-
closeLatch.countDown();
285283
if( writeThread != null )
286284
writeThread.interrupt();
287285
try {
@@ -291,6 +289,8 @@ public final void onWebsocketClose( WebSocket conn, int code, String reason, boo
291289
onWebsocketError( this, e );
292290
}
293291
onClose( code, reason, remote );
292+
connectLatch.countDown();
293+
closeLatch.countDown();
294294
}
295295

296296
/**

src/main/java/org/java_websocket/drafts/Draft_10.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public static int readVersion(Handshakedata handshakedata) {
5353
}
5454

5555
private ByteBuffer incompleteframe;
56-
private Framedata fragmentedframe = null;
5756

5857
private final Random reuseableRandom = new Random();
5958

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public void run() {
280280
socket.bind( address );
281281
selector = Selector.open();
282282
server.register( selector, server.validOps() );
283+
onStart();
283284
} catch ( IOException ex ) {
284285
handleFatal( null, ex );
285286
return;
@@ -684,6 +685,14 @@ public InetSocketAddress getRemoteSocketAddress( WebSocket conn ) {
684685
* Can be null if there error does not belong to one specific websocket. For example if the servers port could not be bound.
685686
**/
686687
public abstract void onError( WebSocket conn, Exception ex );
688+
689+
/**
690+
* Called when the server started up successfully.
691+
*
692+
* If any error occured, onError is called instead.
693+
*/
694+
public abstract void onStart();
695+
687696
/**
688697
* Callback for binary messages received from the remote host
689698
*

src/test/java/org/java_websocket/AutobahnClientScenario.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public void onError(WebSocket conn, Exception ex) {
5050
//throw new UnsupportedOperationException("Not supported yet.");
5151
}
5252

53+
@Override
54+
public void onStart() {
55+
56+
}
57+
5358
}
5459

5560
private class AutobahnClient extends WebSocketClient {

src/test/java/org/java_websocket/example/AutobahnServerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public void onError( WebSocket conn, Exception ex ) {
4242
ex.printStackTrace();
4343
}
4444

45+
@Override
46+
public void onStart() {
47+
System.out.println( "Server started!" );
48+
}
49+
4550
@Override
4651
public void onMessage( WebSocket conn, String message ) {
4752
conn.send( message );

0 commit comments

Comments
 (0)