@@ -47,6 +47,10 @@ public abstract class WebSocketServer implements Runnable, WebSocketListener {
4747 * The socket channel for this WebSocket server.
4848 */
4949 private ServerSocketChannel server ;
50+ /**
51+ * The 'Selector' used to get event keys from the underlying socket.
52+ */
53+ private Selector selector ;
5054 /**
5155 * The Draft of the WebSocket protocol the Server is running adhering to.
5256 */
@@ -169,18 +173,6 @@ public Draft getDraft() {
169173 return draft ;
170174 }
171175
172- /**
173- * @return A BlockingQueue that should be used by a WebSocket
174- * to hold data that is waiting to be sent to the client.
175- * The default implementation returns an unbounded
176- * LinkedBlockingQueue, but you may choose to override
177- * this to return a bounded queue to protect against
178- * running out of memory.
179- */
180- protected BlockingQueue <ByteBuffer > newBufferQueue () {
181- return new LinkedBlockingQueue <ByteBuffer >();
182- }
183-
184176
185177 // Runnable IMPLEMENTATION /////////////////////////////////////////////////
186178 public void run () {
@@ -189,71 +181,72 @@ public void run() {
189181 server .configureBlocking (false );
190182 server .socket ().bind (new java .net .InetSocketAddress (port ));
191183
192- Selector selector = Selector .open ();
184+ selector = Selector .open ();
193185 server .register (selector , server .validOps ());
186+ } catch (IOException ex ) {
187+ ex .printStackTrace ();
188+ return ;
189+ }
194190
195- while (true ) {
196- try {
197- selector .select (100L );
198- Set <SelectionKey > keys = selector .selectedKeys ();
199- Iterator <SelectionKey > i = keys .iterator ();
200-
201- while (i .hasNext ()) {
202- SelectionKey key = i .next ();
203-
204- // Remove the current key
205- i .remove ();
206-
207- // if isAcceptable == true
208- // then a client required a connection
209- if (key .isAcceptable ()) {
210- SocketChannel client = server .accept ();
211- client .configureBlocking (false );
212- WebSocket c = new WebSocket (client , newBufferQueue (), this );
213- client .register (selector , SelectionKey .OP_READ , c );
214- }
215-
216- // if isReadable == true
217- // then the server is ready to read
218- if (key .isReadable ()) {
219- WebSocket conn = (WebSocket )key .attachment ();
220- conn .handleRead ();
221- }
191+ while (true ) {
192+ try {
193+ selector .select ();
194+ Set <SelectionKey > keys = selector .selectedKeys ();
195+ Iterator <SelectionKey > i = keys .iterator ();
196+
197+ while (i .hasNext ()) {
198+ SelectionKey key = i .next ();
199+
200+ // Remove the current key
201+ i .remove ();
202+
203+ // if isAcceptable == true
204+ // then a client required a connection
205+ if (key .isAcceptable ()) {
206+ SocketChannel client = server .accept ();
207+ client .configureBlocking (false );
208+ WebSocket c = new WebSocket (client , new LinkedBlockingQueue <ByteBuffer >(), this );
209+ client .register (selector , SelectionKey .OP_READ , c );
210+ }
222211
223- // if isWritable == true
224- // then we need to send the rest of the data to the client
225- if (key .isWritable ()) {
226- WebSocket conn = (WebSocket )key .attachment ();
227- if (conn .handleWrite ()) {
228- conn .socketChannel ().register (selector ,
229- SelectionKey .OP_READ , conn );
230- }
231- }
212+ // if isReadable == true
213+ // then the server is ready to read
214+ if (key .isReadable ()) {
215+ WebSocket conn = (WebSocket )key .attachment ();
216+ conn .handleRead ();
232217 }
233218
234- for (WebSocket conn : this .connections ) {
235- // We have to do this check here, and not in the thread that
236- // adds the buffered data to the WebSocket, because the
237- // Selector is not thread-safe, and can only be accessed
238- // by this thread.
239- if (conn .hasBufferedData ()) {
219+ // if isWritable == true
220+ // then we need to send the rest of the data to the client
221+ if (key .isWritable ()) {
222+ WebSocket conn = (WebSocket )key .attachment ();
223+ if (conn .handleWrite ()) {
240224 conn .socketChannel ().register (selector ,
241- SelectionKey .OP_READ | SelectionKey . OP_WRITE , conn );
225+ SelectionKey .OP_READ , conn );
242226 }
243227 }
228+ }
244229
245- } catch (IOException e ) {
246- e .printStackTrace ();
247- } catch (RuntimeException e ) {
248- e .printStackTrace ();
230+ for (WebSocket conn : this .connections ) {
231+ // We have to do this check here, and not in the thread that
232+ // adds the buffered data to the WebSocket, because the
233+ // Selector is not thread-safe, and can only be accessed
234+ // by this thread.
235+ if (conn .hasBufferedData ()) {
236+ conn .socketChannel ().register (selector ,
237+ SelectionKey .OP_READ | SelectionKey .OP_WRITE , conn );
238+ }
249239 }
240+ } catch (IOException ex ) {
241+ ex .printStackTrace ();
242+ } catch (RuntimeException ex ) {
243+ ex .printStackTrace ();
244+ } catch (NoSuchAlgorithmException ex ) {
245+ ex .printStackTrace ();
250246 }
251- } catch (IOException ex ) {
252- ex .printStackTrace ();
253- } catch (NoSuchAlgorithmException e ) {
254- // TODO Auto-generated catch block
255- e .printStackTrace ();
256247 }
248+
249+ //System.err.println("WebSocketServer thread ended!");
257250 }
258251
259252 /**
0 commit comments