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.Request; 19 import com.squareup.okhttp.Response; 20 import java.io.IOException; 21 import okio.Buffer; 22 import okio.BufferedSource; 23 24 import static com.squareup.okhttp.ws.WebSocket.PayloadType; 25 26 /** Listener for server-initiated messages on a connected {@link WebSocket}. */ 27 public interface WebSocketListener { 28 void onOpen(WebSocket webSocket, Request request, Response response) throws IOException; 29 30 /** 31 * Called when a server message is received. The {@code type} indicates whether the 32 * {@code payload} should be interpreted as UTF-8 text or binary data. 33 * 34 * <p>Implementations <strong>must</strong> call {@code source.close()} before returning. This 35 * indicates completion of parsing the message payload and will consume any remaining bytes in 36 * the message. 37 */ 38 void onMessage(BufferedSource payload, PayloadType type) throws IOException; 39 40 /** 41 * Called when a server pong is received. This is usually a result of calling {@link 42 * WebSocket#sendPing(Buffer)} but might also be unsolicited. 43 */ 44 void onPong(Buffer payload); 45 46 /** 47 * Called when the server sends a close message. This may have been initiated 48 * from a call to {@link WebSocket#close(int, String) close()} or as an unprompted 49 * message from the server. 50 * 51 * @param code The <a href="http://tools.ietf.org/html/rfc6455#section-7.4.1">RFC-compliant</a> 52 * status code. 53 * @param reason Reason for close or an empty string. 54 */ 55 void onClose(int code, String reason); 56 57 /** Called when the transport or protocol layer of this web socket errors during communication. */ 58 void onFailure(IOException e); 59 } 60