Skip to content

Commit 257acfe

Browse files
committed
Adding "onHandshakeSentAsClient()" callback/signal in the WebSocketListener interface.
This is fired BEFORE the Http Handshake Request is sent. The same request object is passed again, together with the Response, when the Http Handshake Response is received.
1 parent eab25a4 commit 257acfe

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,21 @@ else if( request.limit() < Draft.FLASH_POLICY_REQUEST.length ) {
492492

493493
public void startHandshake( HandshakeBuilder handshakedata ) throws InvalidHandshakeException , InterruptedException {
494494
if( handshakeComplete )
495-
throw new IllegalStateException( "Handshake has allready been sent." );
496-
this.handshakerequest = handshakedata;
497-
channelWrite( draft.createHandshake( draft.postProcessHandshakeRequestAsClient( handshakedata ), role ) );
495+
throw new IllegalStateException( "Handshake has already been sent." );
496+
497+
// Store the Handshake Request we are about to send
498+
this.handshakerequest = draft.postProcessHandshakeRequestAsClient( handshakedata );
499+
500+
// Notify Listener
501+
try {
502+
wsl.onHandshakeSentAsClient( this, this.handshakerequest );
503+
} catch (InvalidDataException e) {
504+
// Stop if the client code throws an exception
505+
throw new InvalidHandshakeException( "Handshake data rejected by client." );
506+
}
507+
508+
// Send
509+
channelWrite( draft.createHandshake( this.handshakerequest, role ) );
498510
}
499511

500512
private void channelWrite( ByteBuffer buf ) throws InterruptedException {

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,29 @@ public abstract class WebSocketAdapter implements WebSocketListener {
88
/**
99
* This default implementation does not do anything. Go ahead and overwrite it.
1010
*
11-
* @see net.tootallnate.websocket.WebSocketListener#onHandshakeRecievedAsServer(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Draft, net.tootallnate.websocket.Handshakedata)
11+
* @see net.tootallnate.websocket.WebSocketListener#onHandshakeReceivedAsServer(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Draft, net.tootallnate.websocket.Handshakedata)
1212
*/
1313
@Override
14-
public HandshakeBuilder onHandshakeRecievedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws InvalidDataException {
14+
public HandshakeBuilder onHandshakeReceivedAsServer(WebSocket conn, Draft draft, Handshakedata request) throws InvalidDataException {
1515
return new HandshakedataImpl1();
1616
}
1717

1818
/**
1919
* This default implementation does not do anything which will cause connections to be accepted. Go ahead and overwrite it.
2020
*
21-
* @see net.tootallnate.websocket.WebSocketListener#onHandshakeRecievedAsClient(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Handshakedata, net.tootallnate.websocket.Handshakedata)
21+
* @see net.tootallnate.websocket.WebSocketListener#onHandshakeReceivedAsClient(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Handshakedata, net.tootallnate.websocket.Handshakedata)
2222
*/
2323
@Override
24-
public void onHandshakeRecievedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws InvalidDataException {
24+
public void onHandshakeReceivedAsClient(WebSocket conn, Handshakedata request, Handshakedata response) throws InvalidDataException {
25+
}
26+
27+
/**
28+
* This default implementation does not do anything which will cause the connections to always progress.
29+
*
30+
* @see net.tootallnate.websocket.WebSocketListener#onHandshakeSentAsClient(net.tootallnate.websocket.WebSocket, net.tootallnate.websocket.Handshakedata)
31+
*/
32+
@Override
33+
public void onHandshakeSentAsClient( WebSocket conn, Handshakedata request ) throws InvalidDataException {
2534
}
2635

2736
/**

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public interface WebSocketListener {
1212
/**
1313
* Called on the server side when the socket connection is first established, and the WebSocket
1414
* handshake has been received.
15-
*
15+
*
16+
* @param conn
17+
* The WebSocket related to this event
1618
* @param draft
1719
* The protocol draft the client uses to connect
1820
* @param request
@@ -21,19 +23,35 @@ public interface WebSocketListener {
2123
* @throws InvalidDataException
2224
* Throwing this exception will cause this handshake to be rejected
2325
*/
24-
public HandshakeBuilder onHandshakeRecievedAsServer( WebSocket conn, Draft draft, Handshakedata request ) throws InvalidDataException;
26+
public HandshakeBuilder onHandshakeReceivedAsServer(WebSocket conn, Draft draft, Handshakedata request) throws InvalidDataException;
27+
2528
/**
2629
* Called on the client side when the socket connection is first established, and the WebSocket
2730
* handshake response has been received.
28-
*
31+
*
32+
* @param conn
33+
* The WebSocket related to this event
2934
* @param request
3035
* The handshake initially send out to the server by this websocket.
3136
* @param response
3237
* The handshake the server sent in response to the request.
3338
* @throws InvalidDataException
3439
* Allows the client to reject the connection with the server in respect of its handshake response.
3540
*/
36-
public void onHandshakeRecievedAsClient( WebSocket conn, Handshakedata request, Handshakedata response ) throws InvalidDataException;
41+
public void onHandshakeReceivedAsClient(WebSocket conn, Handshakedata request, Handshakedata response) throws InvalidDataException;
42+
43+
/**
44+
* Called on the client side when the socket connection is first established, and the WebSocket
45+
* handshake has just been sent.
46+
*
47+
* @param conn
48+
* The WebSocket related to this event
49+
* @param request
50+
* The handshake sent to the server by this websocket
51+
* @throws InvalidDataException
52+
* Allows the client to stop the connection from progressing
53+
*/
54+
public void onHandshakeSentAsClient( WebSocket conn, Handshakedata request ) throws InvalidDataException;
3755

3856
/**
3957
* Called when an entire text frame has been received. Do whatever you want
@@ -58,9 +76,9 @@ public interface WebSocketListener {
5876
public void onMessage( WebSocket conn, byte[] blob );
5977

6078
/**
61-
* Called after <var>onHandshakeRecieved</var> returns <var>true</var>.
79+
* Called after <var>onHandshakeReceived</var> returns <var>true</var>.
6280
* Indicates that a complete WebSocket connection has been established,
63-
* and we are ready to send/recieve data.
81+
* and we are ready to send/receive data.
6482
*
6583
* @param conn
6684
* The <tt>WebSocket</tt> instance this event is occuring on.

0 commit comments

Comments
 (0)