ko-fx/src/main/java/org/netbeans/html/kofx/LoadWS.java
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 80702021b851
child 365 5c93ad8c7a15
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ko-fx/src/main/java/org/netbeans/html/kofx/LoadWS.java	Mon Dec 16 16:59:43 2013 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +/**
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 + * Other names may be trademarks of their respective owners.
    1.11 + *
    1.12 + * The contents of this file are subject to the terms of either the GNU
    1.13 + * General Public License Version 2 only ("GPL") or the Common
    1.14 + * Development and Distribution License("CDDL") (collectively, the
    1.15 + * "License"). You may not use this file except in compliance with the
    1.16 + * License. You can obtain a copy of the License at
    1.17 + * http://www.netbeans.org/cddl-gplv2.html
    1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 + * specific language governing permissions and limitations under the
    1.20 + * License.  When distributing the software, include this License Header
    1.21 + * Notice in each file and include the License file at
    1.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 + * particular file as subject to the "Classpath" exception as provided
    1.24 + * by Oracle in the GPL Version 2 section of the License file that
    1.25 + * accompanied this code. If applicable, add the following below the
    1.26 + * License Header, with the fields enclosed by brackets [] replaced by
    1.27 + * your own identifying information:
    1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 + *
    1.30 + * Contributor(s):
    1.31 + *
    1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.33 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    1.34 + *
    1.35 + * If you wish your version of this file to be governed by only the CDDL
    1.36 + * or only the GPL Version 2, indicate your decision by adding
    1.37 + * "[Contributor] elects to include this software in this distribution
    1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.39 + * single choice of license, a recipient has the option to distribute
    1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    1.41 + * to extend the choice of license to its licensees as provided above.
    1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.43 + * Version 2 license, then the option applies only if the new code is
    1.44 + * made subject to such option by the copyright holder.
    1.45 + */
    1.46 +package org.netbeans.html.kofx;
    1.47 +
    1.48 +import net.java.html.js.JavaScriptBody;
    1.49 +import org.apidesign.html.json.spi.JSONCall;
    1.50 +import org.json.JSONArray;
    1.51 +import org.json.JSONException;
    1.52 +import org.json.JSONObject;
    1.53 +import org.json.JSONTokener;
    1.54 +
    1.55 +/** Communication with WebSockets for WebView 1.8.
    1.56 + *
    1.57 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.58 + */
    1.59 +final class LoadWS {
    1.60 +    private static final boolean SUPPORTED = isWebSocket();
    1.61 +    private final Object ws;
    1.62 +    private final JSONCall call;
    1.63 +
    1.64 +    LoadWS(JSONCall first, String url) {
    1.65 +        call = first;
    1.66 +        ws = initWebSocket(this, url);
    1.67 +        if (ws == null) {
    1.68 +            first.notifyError(new IllegalArgumentException("Wrong URL: " + url));
    1.69 +        }
    1.70 +    }
    1.71 +    
    1.72 +    static boolean isSupported() {
    1.73 +        return SUPPORTED;
    1.74 +    }
    1.75 +    
    1.76 +    void send(JSONCall call) {
    1.77 +        push(call);
    1.78 +    }
    1.79 +    
    1.80 +    private synchronized void push(JSONCall call) {
    1.81 +        send(ws, call.getMessage());
    1.82 +    }
    1.83 +
    1.84 +    void onOpen(Object ev) {
    1.85 +        if (!call.isDoOutput()) {
    1.86 +            call.notifySuccess(null);
    1.87 +        }
    1.88 +    }
    1.89 +    
    1.90 +    void onMessage(Object ev, String data) {
    1.91 +        Object json;
    1.92 +        try {
    1.93 +            data = data.trim();
    1.94 +            
    1.95 +            JSONTokener tok = new JSONTokener(data);
    1.96 +            Object obj;
    1.97 +            obj = data.startsWith("[") ? new JSONArray(tok) : new JSONObject(tok);
    1.98 +            json = LoadJSON.convertToArray(obj);
    1.99 +        } catch (JSONException ex) {
   1.100 +            json = data;
   1.101 +        }
   1.102 +        call.notifySuccess(json);
   1.103 +    }
   1.104 +    
   1.105 +    void onError(Object ev) {
   1.106 +        call.notifyError(new Exception(ev.toString()));
   1.107 +    }
   1.108 +
   1.109 +    void onClose(boolean wasClean, int code, String reason) {
   1.110 +        call.notifyError(null);
   1.111 +    }
   1.112 +    
   1.113 +    @JavaScriptBody(args = {}, body = "if (window.WebSocket) return true; else return false;")
   1.114 +    private static boolean isWebSocket() {
   1.115 +        return false;
   1.116 +    }
   1.117 +
   1.118 +    @JavaScriptBody(args = { "back", "url" }, javacall = true, body = ""
   1.119 +        + "if (window.WebSocket) {"
   1.120 +        + "  try {"
   1.121 +        + "    var ws = new window.WebSocket(url);"
   1.122 +        + "    ws.onopen = function(ev) { back.@org.netbeans.html.kofx.LoadWS::onOpen(Ljava/lang/Object;)(ev); };"
   1.123 +        + "    ws.onmessage = function(ev) { back.@org.netbeans.html.kofx.LoadWS::onMessage(Ljava/lang/Object;Ljava/lang/String;)(ev, ev.data); };"
   1.124 +        + "    ws.onerror = function(ev) { back.@org.netbeans.html.kofx.LoadWS::onError(Ljava/lang/Object;)(ev); };"
   1.125 +        + "    ws.onclose = function(ev) { back.@org.netbeans.html.kofx.LoadWS::onClose(ZILjava/lang/String;)(ev.wasClean, ev.code, ev.reason); };"
   1.126 +        + "    return ws;"
   1.127 +        + "  } catch (ex) {"
   1.128 +        + "    return null;"
   1.129 +        + "  }"
   1.130 +        + "} else {"
   1.131 +        + "  return null;"
   1.132 +        + "}"
   1.133 +    )
   1.134 +    private static Object initWebSocket(Object back, String url) {
   1.135 +        return null;
   1.136 +    }
   1.137 +    
   1.138 +
   1.139 +    @JavaScriptBody(args = { "ws", "msg" }, body = ""
   1.140 +        + "ws.send(msg);"
   1.141 +    )
   1.142 +    private void send(Object ws, String msg) {
   1.143 +    }
   1.144 +
   1.145 +    @JavaScriptBody(args = { "ws" }, body = "ws.close();")
   1.146 +    private static void close(Object ws) {
   1.147 +    }
   1.148 +
   1.149 +    void close() {
   1.150 +        close(ws);
   1.151 +    }
   1.152 +}