Flutter Engine
The Flutter Engine
WebSocketRequestSink.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, the Dart project authors.
3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package org.dartlang.vm.service.internal;
15
16import com.google.gson.JsonObject;
17import de.roderick.weberknecht.WebSocket;
18import de.roderick.weberknecht.WebSocketException;
19import org.dartlang.vm.service.logging.Logging;
20
21/**
22 * An {@link WebSocket} based implementation of {@link RequestSink}.
23 */
24public class WebSocketRequestSink implements RequestSink {
25
26 private WebSocket webSocket;
27
28 public WebSocketRequestSink(WebSocket webSocket) {
29 this.webSocket = webSocket;
30 }
31
32 @Override
33 public void add(JsonObject json) {
34 String request = json.toString();
35 if (webSocket == null) {
36 Logging.getLogger().logInformation("Dropped: " + request);
37 return;
38 }
39 Logging.getLogger().logInformation("Sent: " + request);
40 try {
41 webSocket.send(request);
42 } catch (WebSocketException e) {
43 Logging.getLogger().logError("Failed to send request: " + request, e);
44 }
45 }
46
47 @Override
48 public void close() {
49 if (webSocket != null) {
50 try {
51 webSocket.close();
52 } catch (WebSocketException e) {
53 Logging.getLogger().logError("Failed to close websocket", e);
54 }
55 webSocket = null;
56 }
57 }
58}
const CatchEntryMove de[]