Skip to content

Commit f259695

Browse files
authored
Merge pull request TooTallNate#649 from marci4/master
Update Readme and examples
2 parents a5ff3b4 + bd8e3c8 commit f259695

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

README.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ server-side of the
8383
A WebSocket server by itself doesn't do anything except establish socket
8484
connections though HTTP. After that it's up to **your** subclass to add purpose.
8585

86+
An example for a WebSocketServer can be found in both the [wiki](https://github.com/TooTallNate/Java-WebSocket/wiki#server-example) and the [example](https://github.com/TooTallNate/Java-WebSocket/tree/master/src/main/example) folder.
8687

8788
Writing your own WebSocket Client
8889
---------------------------------
@@ -93,6 +94,8 @@ connect to. Important events `onOpen`, `onClose`, `onMessage` and `onError`
9394
get fired throughout the life of the WebSocketClient, and must be implemented
9495
in **your** subclass.
9596

97+
An example for a WebSocketClient can be found in both the [wiki](https://github.com/TooTallNate/Java-WebSocket/wiki#client-example) and the [example](https://github.com/TooTallNate/Java-WebSocket/tree/master/src/main/example) folder.
98+
9699
WSS Support
97100
---------------------------------
98101
This library supports wss.

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)