Skip to content

Commit 8669096

Browse files
committed
did some documenting stuff ( beware humble english)
1 parent 0f49015 commit 8669096

2 files changed

Lines changed: 109 additions & 16 deletions

File tree

‎src/net/tootallnate/websocket/WebSocketAdapter.java‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,71 @@
11
package net.tootallnate.websocket;
22

3-
import java.io.IOException;
4-
53
import net.tootallnate.websocket.Framedata.Opcode;
4+
import net.tootallnate.websocket.exeptions.InvalidDataException;
65

76
public abstract class WebSocketAdapter implements WebSocketListener {
87

8+
/**
9+
* This default implementation does not do anything. Go ahead and overwrite it.
10+
*
11+
* @see net.tootallnate.websocket.WebSocketListener#onHandshakeRecievedAsServer(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Draft, net.tootallnate.websocket.Handshakedata)
12+
*/
913
@Override
10-
public HandshakeBuilder onHandshakeRecievedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws IOException {
14+
public HandshakeBuilder onHandshakeRecievedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws InvalidDataException {
1115
return new HandshakedataImpl1();
1216
}
1317

18+
/**
19+
* This default implementation does not do anything which will cause connections to be accepted. Go ahead and overwrite it.
20+
*
21+
* @see net.tootallnate.websocket.WebSocketListener#onHandshakeRecievedAsClient(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Handshakedata, net.tootallnate.websocket.Handshakedata)
22+
*/
1423
@Override
15-
public boolean onHandshakeRecievedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws IOException {
16-
return true;
24+
public void onHandshakeRecievedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws InvalidDataException {
1725
}
1826

27+
/**
28+
* This default implementation does not do anything. Go ahead and overwrite it.
29+
*
30+
* @see net.tootallnate.websocket.WebSocketListener#onMessage(net.tootallnate.websocket.WebSocket, java.lang.String)
31+
*/
1932
@Override
2033
public void onMessage( WebSocket conn, String message ) {
2134
}
2235

36+
/**
37+
* This default implementation does not do anything. Go ahead and overwrite it.
38+
*
39+
* @see net.tootallnate.websocket.WebSocketListener#onOpen(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Handshakedata)
40+
*/
2341
@Override
2442
public void onOpen( WebSocket conn, Handshakedata handshake ) {
2543
}
2644

45+
/**
46+
* This default implementation does not do anything. Go ahead and overwrite it.
47+
*
48+
* @see net.tootallnate.websocket.WebSocketListener#onClose(net.tootallnate.websocket.WebSocket, int, java.lang.String, boolean)
49+
*/
2750
@Override
2851
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
2952
}
3053

54+
/**
55+
* This default implementation does not do anything. Go ahead and overwrite it.
56+
*
57+
* @see net.tootallnate.websocket.WebSocketListener#onMessage(net.tootallnate.websocket.WebSocket, byte[])
58+
*/
3159
@Override
3260
public void onMessage( WebSocket conn, byte[] blob ) {
3361
}
3462

63+
/**
64+
* This default implementation will send a pong in response to the received ping.
65+
* The pong frame will have the same payload as the ping frame.
66+
*
67+
* @see net.tootallnate.websocket.WebSocketListener#onPing(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Framedata)
68+
*/
3569
@Override
3670
public void onPing( WebSocket conn, Framedata f ) {
3771
FramedataImpl1 resp = new FramedataImpl1 ( f );
@@ -43,6 +77,11 @@ public void onPing( WebSocket conn, Framedata f ) {
4377
}
4478
}
4579

80+
/**
81+
* This default implementation does not do anything. Go ahead and overwrite it.
82+
*
83+
* @see net.tootallnate.websocket.WebSocketListener#onPong(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Framedata)
84+
*/
4685
@Override
4786
public void onPong( WebSocket conn, Framedata f ) {
4887
}
@@ -65,6 +104,10 @@ public String getFlashPolicy( WebSocket conn ) {
65104
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + conn.getLocalSocketAddress().getPort() + "\" /></cross-domain-policy>\0";
66105
}
67106

107+
/**
108+
* This default implementation does not do anything. Go ahead and overwrite it.
109+
* @see net.tootallnate.websocket.WebSocketListener#onError(net.tootallnate.websocket.WebSocket, java.lang.Exception)
110+
*/
68111
@Override
69112
public void onError( WebSocket conn, Exception ex ) {
70113
}

‎src/net/tootallnate/websocket/WebSocketListener.java‎

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,60 @@
11
package net.tootallnate.websocket;
22

3-
import java.io.IOException;
3+
import net.tootallnate.websocket.exeptions.InvalidDataException;
44

55
/**
66
* Implemented by <tt>WebSocketClient</tt> and <tt>WebSocketServer</tt>.
77
* The methods within are called by <tt>WebSocket</tt>.
8-
*
9-
* @author Nathan Rajlich
8+
* Almost every method takes a first parameter conn which represents the source of the respective event.
109
*/
11-
interface WebSocketListener {
10+
public interface WebSocketListener {
1211

1312
/**
14-
* Called when the socket connection is first established, and the WebSocket
15-
* handshake has been recieved.
13+
* Called on the server side when the socket connection is first established, and the WebSocket
14+
* handshake has been received.
15+
*
16+
* @param draft
17+
* The protocol draft the client uses to connect
18+
* @param request
19+
* The opening http message send by the client. Can be used to access additional fields like cookies.
20+
* @return Returns an incomplete handshake containing all optional fields
21+
* @throws InvalidDataException
22+
* Throwing this exception will cause this handshake to be rejected
1623
*/
17-
public HandshakeBuilder onHandshakeRecievedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws IOException;
18-
public boolean onHandshakeRecievedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws IOException;
24+
public HandshakeBuilder onHandshakeRecievedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws InvalidDataException;
25+
/**
26+
* Called on the client side when the socket connection is first established, and the WebSocket
27+
* handshake response has been received.
28+
*
29+
* @param request
30+
* The handshake initially send out to the server by this websocket.
31+
* @param response
32+
* The handshake the server sent in response to the request.
33+
* @throws InvalidDataException
34+
* Allows the client to reject the connection with the server in respect of its handshake response.
35+
*/
36+
public void onHandshakeRecievedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws InvalidDataException;
1937

2038
/**
21-
* Called when an entire text frame has been recieved. Do whatever you want
39+
* Called when an entire text frame has been received. Do whatever you want
2240
* here...
2341
*
2442
* @param conn
25-
* The <tt>WebSocket</tt> instance this event is occuring on.
43+
* The <tt>WebSocket</tt> instance this event is occurring on.
2644
* @param message
27-
* The UTF-8 decoded message that was recieved.
45+
* The UTF-8 decoded message that was received.
2846
*/
2947
public void onMessage( WebSocket conn, String message );
3048

49+
/**
50+
* Called when an entire binary frame has been received. Do whatever you want
51+
* here...
52+
*
53+
* @param conn
54+
* The <tt>WebSocket</tt> instance this event is occurring on.
55+
* @param message
56+
* The binary message that was received.
57+
*/
3158
public void onMessage( WebSocket conn, byte[] blob );
3259

3360
/**
@@ -49,13 +76,36 @@ interface WebSocketListener {
4976
*/
5077
public void onClose( WebSocket conn, int code, String reason, boolean remote );
5178

79+
/**
80+
* Called if an exception worth noting occurred.
81+
* If an error causes the connection to fail onClose will be called additionally afterwards.
82+
*
83+
* @param ex
84+
* The exception that occurred. <br>
85+
* Might be null if the exception is not related to any specific connection. For example if the server port could not be bound.
86+
*/
5287
public void onError( WebSocket conn, Exception ex );
5388

89+
/**
90+
* Called a ping frame has been received.
91+
* This method must send a corresponding pong by itself.
92+
*
93+
* @param f
94+
* The ping frame. Control frames may contain payload.
95+
*/
5496
public void onPing( WebSocket conn, Framedata f );
5597

98+
/**
99+
* Called when a pong frame is received.
100+
**/
56101
public void onPong( WebSocket conn, Framedata f );
57102

103+
/**
104+
* Gets the XML string that should be returned if a client requests a Flash
105+
* security policy.
106+
*/
58107
public String getFlashPolicy( WebSocket conn );
59108

109+
/** This method is used to inform the selector thread that there is data queued to be written to the socket. */
60110
public void onWriteDemand( WebSocket conn );
61111
}

0 commit comments

Comments
 (0)