MockServer
是OKHttp
中的一个子模块,如其名,用来模拟服务器的,用于测试。
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp3:mockwebserver:3.9.0'
具体的版本号,请查看OKHttp在GitHub上的网址
示例:
示例:
package com.fpliu.newton.websocket;
import okhttp3.*;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okio.ByteString;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class WebSocketTest {
public static void main(String[] args) {
//模拟服务器端:
ExecutorService writeExecutor = Executors.newSingleThreadExecutor();
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().withWebSocketUpgrade(new WebSocketListener() {
@Override
public void onOpen(final WebSocket webSocket, Response response) {
//打印一些内容
System.out.println("server onOpen");
System.out.println("server request header:" + response.request().headers());
System.out.println("server response header:" + response.headers());
System.out.println("server response:" + response);
}
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
String string = bytes.toString();
System.out.println("onMessage() message = " + string);
//当收到客户端的command 1时回复replay command 1
if ("command 1".equals(string)) {
writeExecutor.execute(new Runnable() {
@Override
public void run() {
try {
webSocket.send(RequestBody.create(WebSocket.TEXT, "replay command 1"));
} catch (IOException e) {
e.printStackTrace();
}
}
});
} else if ("command 2".equals(string)) {
//当收到客户端的command 2时,发送ping帧
writeExecutor.execute(new Runnable() {
@Override
public void run() {
webSocket.send(ByteString.of("ping from server...".getBytes()));
}
});
}
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
super.onClosing(webSocket, code, reason);
System.out.println("onClosing()" + "code = " + code + ", reason = " + reason);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason);
System.out.println("onClosed()" + "code = " + code + ", reason = " + reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable throwable, Response response) {
super.onFailure(webSocket, throwable, response);
//出现异常会进入此回调
System.out.println("onFailure()");
System.out.println("throwable:" + throwable);
System.out.println("response:" + response);
}
}));
//模拟客户端:
String hostName = mockWebServer.getHostName();
int port = mockWebServer.getPort();
System.out.println("hostName:" + hostName + ", port:" + port);
OkHttpClient client = new OkHttpClient.Builder().build();
//构造request对象
Request request = new Request.Builder().url("ws://" + hostName + ":" + port + "/").build();
//new 一个websocket调用对象并建立连接
client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(final WebSocket webSocket, Response response) {
//打印一些内容
System.out.println("client onOpen");
System.out.println("client request header:" + response.request().headers());
System.out.println("client response header:" + response.headers());
System.out.println("client response:" + response);
//注意下面都是write线程回写给客户端
//建立连接成功后,发生command 1给服务器端
writeExecutor.execute(new Runnable() {
@Override
public void run() {
try {
webSocket.send(RequestBody.create(WebSocket.TEXT, "command 1"));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
super.onClosing(webSocket, code, reason);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
super.onFailure(webSocket, t, response);
}
});
}
}