WebSocketTest passes OK against modified version of 0.5 net.java.html APIs - rev. b04268c0ded9
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.ko2brwsr;
20 import net.java.html.js.JavaScriptBody;
21 import org.apidesign.html.json.spi.JSONCall;
23 /** Communication with WebSockets for WebView 1.8.
25 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 private static final boolean SUPPORTED = isWebSocket();
29 private final Object ws;
30 private final JSONCall call;
31 LoadWS(JSONCall first, String url) {
33 ws = initWebSocket(this, url);
35 first.notifyError(new IllegalArgumentException("Wrong URL: " + url));
39 static boolean isSupported() {
43 void send(JSONCall call) {
47 private synchronized void push(JSONCall call) {
48 send(ws, call.getMessage());
51 void onOpen(Object ev) {
52 if (!call.isDoOutput()) {
53 call.notifySuccess(null);
58 @JavaScriptBody(args = { "data" }, body = "try {\n"
59 + " return eval('(' + data + ')');\n"
60 + " } catch (error) {;\n"
64 private static native Object toJSON(String data);
66 void onMessage(Object ev, String data) {
67 Object json = toJSON(data);
68 call.notifySuccess(json);
71 void onError(Object ev) {
72 call.notifyError(new Exception(ev.toString()));
75 void onClose(boolean wasClean, int code, String reason) {
76 call.notifyError(null);
79 @JavaScriptBody(args = {}, body = "if (window.WebSocket) return true; else return false;")
80 private static boolean isWebSocket() {
84 @JavaScriptBody(args = { "back", "url" }, javacall = true, body = ""
85 + "if (window.WebSocket) {\n"
87 + " var ws = new window.WebSocket(url);\n"
88 + " ws.onopen = function(ev) {\n"
89 + " back.@org.apidesign.bck2brwsr.ko2brwsr.LoadWS::onOpen(Ljava/lang/Object;)(ev);\n"
91 + " ws.onmessage = function(ev) {\n"
92 + " back.@org.apidesign.bck2brwsr.ko2brwsr.LoadWS::onMessage(Ljava/lang/Object;Ljava/lang/String;)(ev, ev.data);\n"
94 + " ws.onerror = function(ev) {\n"
95 + " back.@org.apidesign.bck2brwsr.ko2brwsr.LoadWS::onError(Ljava/lang/Object;)(ev);\n"
97 + " ws.onclose = function(ev) {\n"
98 + " back.@org.apidesign.bck2brwsr.ko2brwsr.LoadWS::onClose(ZILjava/lang/String;)(ev.wasClean, ev.code, ev.reason);\n"
101 + " } catch (ex) {\n"
108 private static Object initWebSocket(Object back, String url) {
113 @JavaScriptBody(args = { "ws", "msg" }, body = ""
116 private void send(Object ws, String msg) {
119 @JavaScriptBody(args = { "ws" }, body = "ws.close();")
120 private static void close(Object ws) {