Skip to content

Commit 48cc2d3

Browse files
committed
improved server functionality
1 parent e9424f1 commit 48cc2d3

5 files changed

Lines changed: 355 additions & 264 deletions

File tree

example/ChatServer.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,23 @@ public ChatServer( InetSocketAddress address ) {
2424

2525
@Override
2626
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
27-
try {
28-
this.sendToAll( "new connection" );
29-
} catch ( InterruptedException ex ) {
30-
ex.printStackTrace();
31-
}
27+
this.sendToAll( "new connection" );
3228
System.out.println( conn + " entered the room!" );
3329
}
3430

3531
@Override
3632
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
37-
try {
38-
this.sendToAll( conn + " has left the room!" );
39-
} catch ( InterruptedException ex ) {
40-
ex.printStackTrace();
41-
}
33+
this.sendToAll( conn + " has left the room!" );
4234
System.out.println( conn + " has left the room!" );
4335
}
4436

4537
@Override
4638
public void onMessage( WebSocket conn, String message ) {
47-
try {
39+
this.sendToAll( message );
40+
4841
this.sendToAll( message );
49-
} catch ( InterruptedException ex ) {
50-
ex.printStackTrace();
51-
}
52-
System.out.println( conn + ": " + message );
42+
43+
// System.out.println( conn + ": " + message );
5344
}
5445

5546
public static void main( String[] args ) throws InterruptedException , IOException {
@@ -83,7 +74,7 @@ public void onError( WebSocket conn, Exception ex ) {
8374
* @throws InterruptedException
8475
* When socket related I/O errors occur.
8576
*/
86-
public void sendToAll( String text ) throws InterruptedException {
77+
public void sendToAll( String text ) {
8778
Set<WebSocket> con = connections();
8879
synchronized ( con ) {
8980
for( WebSocket c : con ) {

src/org/java_websocket/WebSocket.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public enum Role {
1616
CLIENT, SERVER
1717
}
1818

19-
public static int RCVBUF = 512;
19+
public static int RCVBUF = 64;
2020

2121
public static/*final*/boolean DEBUG = false; // must be final in the future in order to take advantage of VM optimization
2222

@@ -89,11 +89,6 @@ public enum Role {
8989
*/
9090
public abstract long bufferedDataAmount();
9191

92-
/**
93-
* Empty the internal buffer, sending all the pending data before continuing.
94-
*/
95-
public abstract void flush() throws IOException;
96-
9792
/** Returns whether the batch process all available data */
9893
public abstract boolean batch() throws IOException;
9994

src/org/java_websocket/WebSocketClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ protected final void interruptableRun() {
196196
return;
197197
}
198198
conn = (WebSocketImpl) wf.createWebSocket( this, draft, client );
199+
ByteBuffer buff = ByteBuffer.allocate( WebSocket.RCVBUF );
199200
try/*IO*/{
200201
while ( !conn.isClosed() ) {
201202
if( Thread.interrupted() ) {
@@ -209,9 +210,8 @@ protected final void interruptableRun() {
209210
while ( i.hasNext() ) {
210211
key = i.next();
211212
i.remove();
212-
if( key.isReadable() ) {
213-
conn.read();
214-
conn.decode();
213+
if( key.isReadable() && conn.read( buff ) ) {
214+
conn.decode( buff );
215215
}
216216
if( !key.isValid() ) {
217217
continue;

0 commit comments

Comments
 (0)