| 48 | /// Use [isSupported] to check at runtime whether the current port supports |
| 49 | /// WebSocket -- older ports return `false` and [build] will throw on them. |
| 50 | public final class WebSocket { |
| 51 | |
| 52 | /// Handler for the connection-established event. |
| 53 | public interface ConnectHandler { |
| 54 | void onConnect(WebSocket ws); |
| 55 | } |
| 56 | |
| 57 | /// Handler for an incoming text frame. |
| 58 | public interface TextHandler { |
| 59 | void onText(WebSocket ws, String message); |
| 60 | } |
| 61 | |
| 62 | /// Handler for an incoming binary frame. |
| 63 | public interface BinaryHandler { |
| 64 | void onBinary(WebSocket ws, byte[] message); |
| 65 | } |
| 66 | |
| 67 | /// Handler for the close event. |
| 68 | /// |
| 69 | /// @param statusCode the RFC 6455 close status code (1000 = normal, etc.) |
| 70 | /// @param reason the human-readable reason, or empty string |
| 71 | public interface CloseHandler { |
| 72 | void onClose(WebSocket ws, int statusCode, String reason); |
| 73 | } |
| 74 | |
| 75 | /// Handler for transport- or protocol-level errors. The connection is |
| 76 | /// closed by the time this fires. |
| 77 | public interface ErrorHandler { |
| 78 | void onError(WebSocket ws, Exception ex); |
| 79 | } |
| 80 | |
| 81 | private final WebSocketImpl impl; |
| 82 | // Handlers are assigned from the thread that configures the socket and |
| 83 | // read from the background dispatch thread, so they are volatile to |
| 84 | // publish writes across threads. |
| 85 | @SuppressWarnings("PMD.AvoidUsingVolatile") |
| 86 | private volatile ConnectHandler connectHandler; |
| 87 | @SuppressWarnings("PMD.AvoidUsingVolatile") |
| 88 | private volatile TextHandler textHandler; |
| 89 | @SuppressWarnings("PMD.AvoidUsingVolatile") |
| 90 | private volatile BinaryHandler binaryHandler; |
| 91 | @SuppressWarnings("PMD.AvoidUsingVolatile") |
| 92 | private volatile CloseHandler closeHandler; |
| 93 | @SuppressWarnings("PMD.AvoidUsingVolatile") |
| 94 | private volatile ErrorHandler errorHandler; |
| 95 | |
| 96 | private WebSocket(WebSocketImpl impl) { |
| 97 | this.impl = impl; |
| 98 | final WebSocket self = this; |
| 99 | impl.setEventSink(new WebSocketEventSink() { |
| 100 | @Override |
| 101 | public void onConnect() { |
| 102 | ConnectHandler h = connectHandler; |
| 103 | if (h != null) { |
| 104 | try { |
| 105 | h.onConnect(self); |
| 106 | } catch (Throwable t) { |
| 107 | dispatchError(t); |
nothing calls this directly
no outgoing calls
no test coverage detected