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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | /* * Copyright (C) 2015-2017 Neo Visionaries Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package NeoVisionaries.WebSockets; import java.util.List; import java.util.Map; /** * Listener interface to receive WebSocket events. * * <EMBED CLASS='external-html' DATA-FILE-ID=LICENSE><BR /> * * <p> * An implementation of this interface should be added by {@link * WebSocket#addListener(WebSocketListener)} to a {@link WebSocket} * instance before calling {@link WebSocket#connect()}. * </p> * * <p> * {@link WebSocketAdapter} is an empty implementation of this interface. * </p> * * @see WebSocket#addListener(WebSocketListener) * @see WebSocketAdapter */ public interface WebSocketListener { /** * Called after the state of the WebSocket changed. * * @param websocket * The WebSocket. * * @param newState * The new state of the WebSocket. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 1.1 */ void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception; /** * Called after the opening handshake of the WebSocket connection succeeded. * * @param websocket * The WebSsocket. * * @param headers * HTTP headers received from the server. Keys of the map are * HTTP header names such as {@code "Sec-WebSocket-Accept"}. * Note that the comparator used by the map is {@link * String#CASE_INSENSITIVE_ORDER}. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception; /** * Called when {@link WebSocket#connectAsynchronously()} failed. * * <p> * Note that this method is called only when {@code connectAsynchronously()} * was used and the {@link WebSocket#connect() connect()} executed in the * background thread failed. Neither direct synchronous {@code connect()} * nor {@link WebSocket#connect(java.util.concurrent.ExecutorService) * connect(ExecutorService)} will trigger this callback method. * </p> * * @param websocket * The WebSocket. * * @param cause * The exception thrown by {@link WebSocket#connect() connect()} * method. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 1.8 */ void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception; /** * Called after the WebSocket connection was closed. * * @param websocket * The WebSocket. * * @param serverCloseFrame * The <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1" * >close frame</a> which the server sent to this client. * This may be {@code null}. * * @param clientCloseFrame * The <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1" * >close frame</a> which this client sent to the server. * This may be {@code null}. * * @param closedByServer * {@code true} if the closing handshake was started by the server. * {@code false} if the closing handshake was started by the client. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception; /** * Called when a frame was received. This method is called before * an <code>on<i>Xxx</i>Frame</code> method is called. * * @param websocket * The WebSocket. * * @param frame * The frame. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a continuation frame (opcode = 0x0) was received. * * @param websocket * The WebSocket. * * @param frame * The continuation frame. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onContinuationFrame(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a text frame (opcode = 0x1) was received. * * @param websocket * The WebSocket. * * @param frame * The text frame. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a binary frame (opcode = 0x2) was received. * * @param websocket * The WebSocket. * * @param frame * The binary frame. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onBinaryFrame(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1" * >close frame</a> (opcode = 0x8) was received. * * @param websocket * The WebSocket. * * @param frame * The <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a>. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2" * >ping frame</a> (opcode = 0x9) was received. * * @param websocket * The WebSocket. * * @param frame * The <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">ping frame</a>. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3" * >pong frame</a> (opcode = 0xA) was received. * * @param websocket * The WebSocket. * * @param frame * The <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">pong frame</a>. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a text message was received. * * <p> * When {@link WebSocket#isDirectTextMessage()} returns {@code true}, * {@link #onTextMessage(WebSocket, byte[])} will be called instead of * this method (since version 2.6). * </p> * * @param websocket * The WebSocket. * * @param text * The text message. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onTextMessage(WebSocket websocket, String text) throws Exception; /** * Called when a text message was received instead of * {@link #onTextMessage(WebSocket, String)} when {@link WebSocket#isDirectTextMessage()} * returns {@code true}. * * @param websocket * The WebSocket. * * @param data * The UTF-8 byte sequence of the text message. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 2.6 */ void onTextMessage(WebSocket websocket, byte[] data) throws Exception; /** * Called when a binary message was received. * * @param websocket * The WebSocket. * * @param binary * The binary message. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception; /** * Called before a WebSocket frame is sent. * * @param websocket * The WebSocket. * * @param frame * The WebSocket frame to be sent. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 1.15 */ void onSendingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a WebSocket frame was sent to the server * (but not flushed yet). * * @param websocket * The WebSocket. * * @param frame * The sent frame. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called when a WebSocket frame was not sent to the server * because a <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1" * >close frame</a> has already been sent. * * <p> * Note that {@code onFrameUnsent} is not called when {@link * #onSendError(WebSocket, WebSocketException, WebSocketFrame) * onSendError} is called. * </p> * * @param websocket * The WebSocket. * * @param frame * The unsent frame. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onFrameUnsent(WebSocket websocket, WebSocketFrame frame) throws Exception; /** * Called between after a thread is created and before the thread's * {@code start()} method is called. * * @param websocket * The WebSocket. * * @param threadType * The thread type. * * @param thread * The newly created thread instance. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 2.0 */ void onThreadCreated(WebSocket websocket, ThreadType threadType, Thread thread) throws Exception; /** * Called at the very beginning of the thread's {@code run()} method implementation. * * @param websocket * The WebSocket. * * @param threadType * The thread type. * * @param thread * The thread instance. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 2.0 */ void onThreadStarted(WebSocket websocket, ThreadType threadType, Thread thread) throws Exception; /** * Called at the very end of the thread's {@code run()} method implementation. * * @param websocket * The WebSocket. * * @param threadType * The thread type. * * @param thread * The thread instance. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 2.0 */ void onThreadStopping(WebSocket websocket, ThreadType threadType, Thread thread) throws Exception; /** * Call when an error occurred. This method is called before * an <code>on<i>Xxx</i>Error</code> method is called. * * @param websocket * The WebSocket. * * @param cause * An exception that represents the error. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onError(WebSocket websocket, WebSocketException cause) throws Exception; /** * Called when a WebSocket frame failed to be read from the WebSocket. * * <p> * Some WebSocket server implementations close a WebSocket connection without sending * a <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a> to a * client in some cases. Strictly speaking, this behavior is a violation against the * specification (<a href="https://tools.ietf.org/html/rfc6455">RFC 6455</a>). However, * this library has allowed the behavior by default since the version 1.29. Even if * the end of the input stream of a WebSocket connection were reached without a * close frame being received, it would trigger neither {@link #onError(WebSocket, * WebSocketException) onError()} method nor {@link #onFrameError(WebSocket, * WebSocketException, WebSocketFrame) onFrameError()} method. If you want to make * this library report an error in the case, pass {@code false} to {@link * WebSocket#setMissingCloseFrameAllowed(boolean)} method. * </p> * * @param websocket * The WebSocket. * * @param cause * An exception that represents the error. When the error occurred * because of {@link java.io.InterruptedIOException InterruptedIOException}, * {@code exception.getError()} returns {@link WebSocketError#INTERRUPTED_IN_READING}. * For other IO errors, {@code exception.getError()} returns {@link * WebSocketError#IO_ERROR_IN_READING}. Other error codes denote * protocol errors, which imply that some bugs may exist in either * or both of the client-side and the server-side implementations. * * @param frame * The WebSocket frame. If this is not {@code null}, it means that * verification of the frame failed. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception; /** * Called when it failed to concatenate payloads of multiple frames * to construct a message. The reason of the failure is probably * out-of-memory. * * @param websocket * The WebSocket. * * @param cause * An exception that represents the error. * * @param frames * The list of frames that form a message. The first element * is either a text frame and a binary frame, and the other * frames are continuation frames. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onMessageError(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) throws Exception; /** * Called when a message failed to be decompressed. * * @param websocket * The WebSocket. * * @param cause * An exception that represents the error. * * @param compressed * The compressed message that failed to be decompressed. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 1.16 */ void onMessageDecompressionError(WebSocket websocket, WebSocketException cause, byte[] compressed) throws Exception; /** * Called when it failed to convert payload data into a string. * The reason of the failure is probably out-of-memory. * * @param websocket * The WebSocket. * * @param cause * An exception that represents the error. * * @param data * The payload data that failed to be converted to a string. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) throws Exception; /** * Called when an error occurred when a frame was tried to be sent * to the server. * * @param websocket * The WebSocket. * * @param cause * An exception that represents the error. * * @param frame * The frame which was tried to be sent. This is {@code null} * when the error code of the exception is {@link * WebSocketError#FLUSH_ERROR FLUSH_ERROR}. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception; /** * Called when an uncaught throwable was detected in either the * reading thread (which reads frames from the server) or the * writing thread (which sends frames to the server). * * @param websocket * The WebSocket. * * @param cause * The cause of the error. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. */ void onUnexpectedError(WebSocket websocket, WebSocketException cause) throws Exception; /** * Called when an <code>on<i>Xxx</i>()</code> method threw a {@code Throwable}. * * @param websocket * The WebSocket. * * @param cause * The {@code Throwable} an <code>on<i>Xxx</i></code> method threw. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is just ignored. * * @since 1.9 */ void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception; /** * Called before an opening handshake is sent to the server. * * @param websocket * The WebSocket. * * @param requestLine * The request line. For example, {@code "GET /chat HTTP/1.1"}. * * @param headers * The HTTP headers. * * @throws Exception * An exception thrown by an implementation of this method. * The exception is passed to {@link #handleCallbackError(WebSocket, Throwable)}. * * @since 1.21 */ void onSendingHandshake(WebSocket websocket, String requestLine, List<String[]> headers) throws Exception; } |