11package net .tootallnate .websocket ;
22
33import java .io .IOException ;
4+ import java .net .InetAddress ;
5+ import java .net .InetSocketAddress ;
6+ import java .net .UnknownHostException ;
47import java .nio .channels .SelectionKey ;
58import java .nio .channels .Selector ;
69import java .nio .channels .ServerSocketChannel ;
@@ -29,7 +32,7 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
2932 * The port number that this WebSocket server should listen on. Default is
3033 * WebSocket.DEFAULT_PORT.
3134 */
32- private int port ;
35+ private InetSocketAddress address ;
3336 /**
3437 * The socket channel for this WebSocket server.
3538 */
@@ -50,8 +53,8 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
5053 * Nullary constructor. Creates a WebSocketServer that will attempt to
5154 * listen on port WebSocket.DEFAULT_PORT.
5255 */
53- public WebSocketServer () {
54- this ( WebSocket .DEFAULT_PORT , null );
56+ public WebSocketServer () throws UnknownHostException {
57+ this ( new InetSocketAddress ( InetAddress . getLocalHost (), WebSocket .DEFAULT_PORT ) , null );
5558 }
5659
5760 /**
@@ -61,8 +64,8 @@ public WebSocketServer() {
6164 * @param port
6265 * The port number this server should listen on.
6366 */
64- public WebSocketServer ( int port ) {
65- this ( port , null );
67+ public WebSocketServer ( InetSocketAddress address ) {
68+ this ( address , null );
6669 }
6770
6871 /**
@@ -75,17 +78,18 @@ public WebSocketServer( int port ) {
7578 * The version of the WebSocket protocol that this server
7679 * instance should comply to.
7780 */
78- public WebSocketServer ( int port , Draft draft ) {
81+ public WebSocketServer ( InetSocketAddress address , Draft draft ) {
7982 this .connections = new CopyOnWriteArraySet <WebSocket >();
8083 this .draft = draft ;
81- setPort ( port );
84+ setAddress ( address );
8285 }
8386
8487 /**
8588 * Starts the server thread that binds to the currently set port number and
8689 * listeners for WebSocket connection requests.
90+ * @throws IllegalStateException
8791 */
88- public void start () throws IllegalStateException {
92+ public void start () {
8993 if ( thread != null )
9094 throw new IllegalStateException ( "Already started" );
9195 new Thread ( this ).start ();
@@ -181,20 +185,25 @@ public WebSocket[] connections() {
181185 * @param port
182186 * The port number to listen on.
183187 */
184- public void setPort ( int port ) {
185- this .port = port ;
188+ public void setAddress ( InetSocketAddress address ) {
189+ this .address = address ;
186190 }
187191
192+ public InetSocketAddress getAddress () {
193+ return this .address ;
194+ }
195+
188196 /**
189197 * Gets the port number that this server listens on.
190198 *
191199 * @return The port number.
192200 */
193201 public int getPort () {
194- return this . port ;
202+ return getAddress (). getPort () ;
195203 }
204+
196205
197- public Draft getDraft () {
206+ public Draft getDraft (){
198207 return this .draft ;
199208 }
200209
@@ -206,8 +215,8 @@ public void run() {
206215 try {
207216 server = ServerSocketChannel .open ();
208217 server .configureBlocking ( false );
209- server .socket ().bind ( new java . net . InetSocketAddress ( port ) );
210-
218+ server .socket ().bind ( address );
219+ //InetAddress.getLocalHost()
211220 selector = Selector .open ();
212221 server .register ( selector , server .validOps () );
213222 } catch ( IOException ex ) {
@@ -242,14 +251,14 @@ public void run() {
242251 // then the server is ready to read
243252 if ( key .isReadable () ) {
244253 conn = (WebSocket ) key .attachment ();
245- conn .handleRead ( );
254+ conn .handleRead ();
246255 }
247256
248257 // if isWritable == true
249258 // then we need to send the rest of the data to the client
250259 if ( key .isValid () && key .isWritable () ) {
251260 conn = (WebSocket ) key .attachment ();
252- conn .handleWrite ( );
261+ conn .handleWrite ();
253262 key .channel ().register ( selector , SelectionKey .OP_READ , conn );
254263 }
255264 }
@@ -262,7 +271,7 @@ public void run() {
262271 conn = it .next ();
263272 if ( conn .hasBufferedData () ) {
264273 conn .handleWrite ();
265- //key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
274+ // key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
266275 }
267276 }
268277 } catch ( IOException ex ) {
0 commit comments