Skip to content

Commit 6aa3b1f

Browse files
authored
Merge pull request #856 from marci4/Issue855
Move the startup of the WebSocketWorker inside of run()
2 parents 8a89706 + d417e04 commit 6aa3b1f

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ public void start() {
228228
if( selectorthread != null )
229229
throw new IllegalStateException( getClass().getName() + " can only be started once." );
230230
new Thread( this ).start();
231-
232-
for( WebSocketWorker ex : decoders ){
233-
ex.start();
234-
}
235231
}
236232

237233
/**
@@ -510,6 +506,9 @@ private boolean doSetupSelectorAndServerThread() {
510506
selector = Selector.open();
511507
server.register( selector, server.validOps() );
512508
startConnectionLostTimer();
509+
for( WebSocketWorker ex : decoders ){
510+
ex.start();
511+
}
513512
onStart();
514513
} catch ( IOException ex ) {
515514
handleFatal( null, ex );
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2010-2019 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*
25+
*/
26+
27+
package org.java_websocket.issues;
28+
29+
import org.java_websocket.WebSocket;
30+
import org.java_websocket.client.WebSocketClient;
31+
import org.java_websocket.handshake.ClientHandshake;
32+
import org.java_websocket.handshake.ServerHandshake;
33+
import org.java_websocket.server.WebSocketServer;
34+
import org.java_websocket.util.SocketUtil;
35+
import org.junit.Test;
36+
37+
import java.net.InetSocketAddress;
38+
import java.net.URI;
39+
import java.util.concurrent.CountDownLatch;
40+
41+
public class Issue855Test {
42+
43+
CountDownLatch countServerDownLatch = new CountDownLatch(1);
44+
CountDownLatch countDownLatch = new CountDownLatch(1);
45+
46+
@Test(timeout = 2000)
47+
public void testIssue() throws Exception {
48+
int port = SocketUtil.getAvailablePort();
49+
WebSocketClient webSocket = new WebSocketClient(new URI("ws://localhost:" + port)) {
50+
@Override
51+
public void onOpen(ServerHandshake handshakedata) {
52+
countDownLatch.countDown();
53+
}
54+
55+
@Override
56+
public void onMessage(String message) {
57+
58+
}
59+
60+
@Override
61+
public void onClose(int code, String reason, boolean remote) {
62+
}
63+
64+
@Override
65+
public void onError(Exception ex) {
66+
67+
}
68+
};
69+
WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) {
70+
@Override
71+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
72+
conn.close();
73+
}
74+
75+
@Override
76+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
77+
}
78+
79+
@Override
80+
public void onMessage(WebSocket conn, String message) {
81+
82+
}
83+
84+
@Override
85+
public void onError(WebSocket conn, Exception ex) {
86+
87+
}
88+
89+
@Override
90+
public void onStart() {
91+
countServerDownLatch.countDown();
92+
}
93+
};
94+
new Thread(server).start();
95+
countServerDownLatch.await();
96+
webSocket.connectBlocking();
97+
server.stop();
98+
}
99+
}

0 commit comments

Comments
 (0)