1 /* 2 * Copyright (C) 2014 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp.ws; 17 18 import com.squareup.okhttp.Response; 19 import java.io.IOException; 20 import okio.Buffer; 21 import okio.BufferedSource; 22 23 import static com.squareup.okhttp.ws.WebSocket.PayloadType; 24 25 /** Listener for server-initiated messages on a connected {@link WebSocket}. */ 26 public interface WebSocketListener { 27 /** 28 * Called when the request has successfully been upgraded to a web socket. This method is called 29 * on the message reading thread to allow setting up any state before the 30 * {@linkplain #onMessage message}, {@linkplain #onPong pong}, and {@link #onClose close} 31 * callbacks start. 32 * <p> 33 * <b>Do not</b> use this callback to write to the web socket. Start a new thread or use 34 * another thread in your application. 35 */ onOpen(WebSocket webSocket, Response response)36 void onOpen(WebSocket webSocket, Response response); 37 38 /** 39 * Called when the transport or protocol layer of this web socket errors during communication. 40 * 41 * @param response Present when the failure is a direct result of the response (e.g., failed 42 * upgrade, non-101 response code, etc.). {@code null} otherwise. 43 */ onFailure(IOException e, Response response)44 void onFailure(IOException e, Response response); 45 46 /** 47 * Called when a server message is received. The {@code type} indicates whether the 48 * {@code payload} should be interpreted as UTF-8 text or binary data. 49 * 50 * <p>Implementations <strong>must</strong> call {@code source.close()} before returning. This 51 * indicates completion of parsing the message payload and will consume any remaining bytes in 52 * the message. 53 */ onMessage(BufferedSource payload, PayloadType type)54 void onMessage(BufferedSource payload, PayloadType type) throws IOException; 55 56 /** 57 * Called when a server pong is received. This is usually a result of calling {@link 58 * WebSocket#sendPing(Buffer)} but might also be unsolicited. 59 */ onPong(Buffer payload)60 void onPong(Buffer payload); 61 62 /** 63 * Called when the server sends a close message. This may have been initiated 64 * from a call to {@link WebSocket#close(int, String) close()} or as an unprompted 65 * message from the server. 66 * 67 * @param code The <a href="http://tools.ietf.org/html/rfc6455#section-7.4.1">RFC-compliant</a> 68 * status code. 69 * @param reason Reason for close or an empty string. 70 */ onClose(int code, String reason)71 void onClose(int code, String reason); 72 } 73