forked from TooTallNate/Java-WebSocket
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocketListener.java
More file actions
187 lines (166 loc) · 6.54 KB
/
WebSocketListener.java
File metadata and controls
187 lines (166 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package org.java_websocket;
import java.nio.ByteBuffer;
import org.java_websocket.drafts.Draft;
import org.java_websocket.exeptions.InvalidDataException;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.Handshakedata;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.handshake.ServerHandshakeBuilder;
import java.util.Date;
/**
* Implemented by <tt>WebSocketClient</tt> and <tt>WebSocketServer</tt>.
* The methods within are called by <tt>WebSocket</tt>.
* Almost every method takes a first parameter conn which represents the source of the respective event.
*/
public interface WebSocketListener {
enum MessageType { HANDSHAKE, CLOSE, TEXT, BINARY }
/**
* Called on the server side when the socket connection is first established, and the WebSocket
* handshake has been received.
*
* @param conn
* The WebSocket related to this event
* @param draft
* The protocol draft the client uses to connect
* @param request
* The opening http message send by the client. Can be used to access additional fields like cookies.
* @return Returns an incomplete handshake containing all optional fields
* @throws InvalidDataException
* Throwing this exception will cause this handshake to be rejected
*/
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, ClientHandshake request ) throws InvalidDataException;
/**
* Called on the client side when the socket connection is first established, and the WebSocket
* handshake response has been received.
*
* @param conn
* The WebSocket related to this event
* @param request
* The handshake initially send out to the server by this websocket.
* @param response
* The handshake the server sent in response to the request.
* @throws InvalidDataException
* Allows the client to reject the connection with the server in respect of its handshake response.
*/
public void onWebsocketHandshakeReceivedAsClient( WebSocket conn, ClientHandshake request, ServerHandshake response ) throws InvalidDataException;
/**
* Called on the client side when the socket connection is first established, and the WebSocket
* handshake has just been sent.
*
* @param conn
* The WebSocket related to this event
* @param request
* The handshake sent to the server by this websocket
* @throws InvalidDataException
* Allows the client to stop the connection from progressing
*/
public void onWebsocketHandshakeSentAsClient( WebSocket conn, ClientHandshake request ) throws InvalidDataException;
/**
* Called when an entire text frame has been received. Do whatever you want
* here...
*
* @param conn
* The <tt>WebSocket</tt> instance this event is occurring on.
* @param message
* The UTF-8 decoded message that was received.
*/
public void onWebsocketMessage( WebSocket conn, String message );
/**
* Called when an entire binary frame has been received. Do whatever you want
* here...
*
* @param conn
* The <tt>WebSocket</tt> instance this event is occurring on.
* @param blob
* The binary message that was received.
*/
public void onWebsocketMessage( WebSocket conn, ByteBuffer blob );
/**
* Called after <var>onHandshakeReceived</var> returns <var>true</var>.
* Indicates that a complete WebSocket connection has been established,
* and we are ready to send/receive data.
*
* @param conn
* The <tt>WebSocket</tt> instance this event is occuring on.
*/
public void onWebsocketOpen( WebSocket conn, Handshakedata d );
/**
* Called after <tt>WebSocket#close</tt> is explicity called, or when the
* other end of the WebSocket connection is closed.
*
* @param conn
* The <tt>WebSocket</tt> instance this event is occuring on.
*/
public void onWebsocketClose( WebSocket conn, int code, String reason, boolean remote );
/**
* Called if an exception worth noting occurred.
* If an error causes the connection to fail onClose will be called additionally afterwards.
*
* @param ex
* The exception that occurred. <br>
* Might be null if the exception is not related to any specific connection. For example if the server port could not be bound.
*/
public void onWebsocketError( WebSocket conn, Exception ex );
/**
* Called a ping frame has been received.
* This method must send a corresponding pong by itself.
*
* @param f
* The ping frame. Control frames may contain payload.
*/
public void onWebsocketPing( WebSocket conn, Framedata f );
/**
* Called when a pong frame is received.
**/
public void onWebsocketPong( WebSocket conn, Framedata f );
/**
* Gets the XML string that should be returned if a client requests a Flash
* security policy.
*/
public String getFlashPolicy( WebSocket conn );
/** This method is used to inform the selector thread that there is data queued to be written to the socket. */
public void onWriteDemand( WebSocket conn );
/** Informs the Listener how long it took to open a socket connection
*
* @param startTime
* Time at which the socket started connecting
*
* @param endTime
* Time at which the socket connection was established
*/
public void connectTiming( Date startTime, Date endTime );
/** Informs the Listener how long it took to send a Message.
* This will also inform about how long it took to send the initial WebSocket HTTP Handshake.
*
* @param startTime
* Time at which it started sending a message
*
* @param endTime
* Time at which if finished sending that message
*
* @param type
* Type of message sent
*
* @param bytesAmount
* Amount of bytes sent
*/
public void messageSendTiming( Date startTime, Date endTime, MessageType type, int bytesAmount );
/** Informs the Listener how long it took to receive a Message.
* This will also inform about how long it took to receive the initial WebSocket HTTP Handshake.
* Time is measured from when data are ready/waiting to be read on the socket.
*
* @param startTime
* Time at which it started receiving a message
*
* @param endTime
* Time at which if finished receiving that message
*
* @param type
* Type of message sent
*
* @param bytesAmount
* Amount of bytes received
*/
public void messageReceiveTiming( Date startTime, Date endTime, MessageType type, int bytesAmount );
}