use of okhttp3.WebSocket in project apollo-android by apollographql.
the class WebSocketSubscriptionTransport method send.
@Override
public void send(OperationClientMessage message) {
WebSocket socket = webSocket.get();
if (socket == null) {
throw new IllegalStateException("Not connected");
}
socket.send(message.toJsonString());
}
use of okhttp3.WebSocket in project vertx-openshift-it by cescoffier.
the class SimpleHttpIT method testWithWebSocket.
@Test
public void testWithWebSocket() throws MalformedURLException {
ensureThat("the websocket connection works", () -> {
OkHttpClient client = new OkHttpClient.Builder().readTimeout(0, TimeUnit.MILLISECONDS).build();
Request request = new Request.Builder().url(RestAssured.baseURI + "/ws").build();
List<String> messages = new ArrayList<>();
WebSocketListener webSocketListener = new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, okhttp3.Response response) {
webSocket.send("Hello...");
webSocket.send("...World!");
webSocket.close(1000, "Goodbye, World!");
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, okhttp3.Response response) {
System.err.println("An error has occured: " + t.getMessage());
t.printStackTrace();
}
@Override
public void onMessage(WebSocket webSocket, String text) {
messages.add(text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
messages.add(bytes.utf8());
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(1000, null);
}
};
client.newWebSocket(request, webSocketListener);
await().until(() -> messages.size() >= 2);
Assertions.assertThat(messages).hasSize(2).contains("Hello...", "...World!");
});
}
use of okhttp3.WebSocket in project Signal-Android by WhisperSystems.
the class CdshService method getRegisteredUsers.
public Single<ServiceResponse<Map<String, ACI>>> getRegisteredUsers(String username, String password, Set<String> e164Numbers) {
return Single.create(emitter -> {
AtomicReference<Stage> stage = new AtomicReference<>(Stage.WAITING_TO_INITIALIZE);
List<String> addressBook = e164Numbers.stream().map(e -> e.substring(1)).collect(Collectors.toList());
String url = String.format("%s/discovery/%s/%s", baseUrl, hexPublicKey, hexCodeHash);
Request request = new Request.Builder().url(url).build();
WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
switch(stage.get()) {
case WAITING_TO_INITIALIZE:
enclave.completeHandshake(bytes.toByteArray());
byte[] request = enclave.establishedSend(buildPlaintextRequest(username, password, addressBook));
stage.set(Stage.WAITING_FOR_RESPONSE);
webSocket.send(ByteString.of(request));
break;
case WAITING_FOR_RESPONSE:
byte[] response = enclave.establishedRecv(bytes.toByteArray());
try {
Map<String, ACI> out = parseResponse(addressBook, response);
emitter.onSuccess(ServiceResponse.forResult(out, 200, null));
} catch (IOException e) {
emitter.onSuccess(ServiceResponse.forUnknownError(e));
} finally {
webSocket.close(1000, "OK");
}
break;
case FAILURE:
Log.w(TAG, "Received a message after we entered the failure state! Ignoring.");
webSocket.close(1000, "OK");
break;
}
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
if (code != 1000) {
Log.w(TAG, "Remote side is closing with non-normal code " + code);
webSocket.close(1000, "Remote closed with code " + code);
stage.set(Stage.FAILURE);
emitter.onSuccess(ServiceResponse.forApplicationError(new NonSuccessfulResponseCodeException(code), code, null));
}
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
emitter.onSuccess(ServiceResponse.forApplicationError(t, response != null ? response.code() : 0, null));
stage.set(Stage.FAILURE);
webSocket.close(1000, "OK");
}
});
webSocket.send(ByteString.of(enclave.initialRequest()));
emitter.setCancellable(() -> webSocket.close(1000, "OK"));
});
}
use of okhttp3.WebSocket in project java by kubernetes-client.
the class WebSocketsExample method main.
public static void main(String... args) throws ApiException, IOException {
final ApiClient client = Config.defaultClient();
WebSockets.stream(args[0], "GET", client, new WebSockets.SocketListener() {
private volatile WebSocket socket;
@Override
public void open(String protocol, WebSocket socket) {
this.socket = socket;
}
@Override
public void close() {
}
@Override
public void bytesMessage(InputStream is) {
}
@Override
public void failure(Throwable t) {
t.printStackTrace();
}
@Override
public void textMessage(Reader in) {
try {
BufferedReader reader = new BufferedReader(in);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
System.out.println(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
use of okhttp3.WebSocket in project DevRing by LJYcoder.
the class WebSocketManager method removeUrlWebSocketMapping.
/**
* 移除Url和WebSocket的映射
*/
private void removeUrlWebSocketMapping(WebSocket webSocket) {
for (Map.Entry<String, WebSocket> entry : mWebSocketPool.entrySet()) {
if (entry.getValue() == webSocket) {
String url = entry.getKey();
mObservableCacheMap.remove(url);
mWebSocketPool.remove(url);
}
}
}
Aggregations