Skip to content

Commit 362f2da

Browse files
committed
Clear up examples
Make some methods more clear
1 parent 26cb0ff commit 362f2da

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/main/example/ChatServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public ChatServer( InetSocketAddress address ) {
5151

5252
@Override
5353
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
54-
broadcast( "new connection: " + handshake.getResourceDescriptor() );
54+
conn.send("Welcome to the server!"); //This method sends a message to the new client
55+
broadcast( "new connection: " + handshake.getResourceDescriptor() ); //This method sends a message to all clients connected
5556
System.out.println( conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!" );
5657
}
5758

src/main/example/ChatServerAttachmentExample.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838

3939
/**
4040
* A simple WebSocketServer implementation. Keeps track of a "chatroom".
41+
*
42+
* Shows how to use the attachment for a WebSocket. This example just uses a simple integer as ID.
43+
* Setting an attachment also works in the WebSocketClient
4144
*/
4245
public class ChatServerAttachmentExample extends WebSocketServer {
4346
Integer index = 0;
@@ -52,13 +55,15 @@ public ChatServerAttachmentExample( InetSocketAddress address ) {
5255

5356
@Override
5457
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
55-
conn.setAttachment( index );
58+
conn.setAttachment( index ); //Set the attachment to the current index
5659
index++;
60+
// Get the attachment of this connection as Integer
5761
System.out.println( conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room! ID: " + conn.<Integer>getAttachment() );
5862
}
5963

6064
@Override
6165
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
66+
// Get the attachment of this connection as Integer
6267
System.out.println( conn + " has left the room! ID: " + conn.<Integer>getAttachment() );
6368
}
6469

src/main/example/ExampleClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public ExampleClient( URI serverURI ) {
4545

4646
@Override
4747
public void onOpen( ServerHandshake handshakedata ) {
48+
send("Hello, it is me. Mario :)");
4849
System.out.println( "opened connection" );
4950
// if you plan to refuse connection based on ip or httpfields overload: onWebsocketHandshakeReceivedAsClient
5051
}

0 commit comments

Comments
 (0)