examples: usable with Crosswalk extension and web socket
[contrib/cloudeebus.git] / doc / agent / client.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <!-- Do not load autobahn or cloudeebus if already provided by Crosswalk browser extensions. -->
5         <script type="text/javascript">
6           typeof cloudeebus != 'undefined' ||
7           document.write(unescape('%3Cscript src=%27../../lib/autobahn.min.js%27%3E %3C/script%3E %3Cscript src=%27../../cloudeebus/cloudeebus.js%27%3E%3C/script%3E'));
8         </script>
9         <script type="text/javascript">
10                 function evalScript() {
11                         eval(
12                                 "var lambda = function() {" +
13                                         document.getElementById('script').value +
14                                 "};" + 
15                                 "lambda();"
16                         );
17                 }
18         </script>
19     </head>
20    <body>
21         <center><h1>cloudeebus</h1></center>
22         <br>
23                 <textarea style="width:80%" rows="32" id="script">
24 var manifest = {
25         name: "cloudeebus",
26         key: "secret",
27         permissions: [
28                 "org.cloudeebus.Sample"
29         ]
30 };
31
32
33 cloudeebus.log = function(msg) {
34   document.getElementById("log").innerHTML += msg + "\n";
35 }
36
37 function logCB(result) {
38   cloudeebus.log(JSON.stringify(result));
39 }
40
41 function errorCB(error) {
42   cloudeebus.log(error.desc ? error.desc : error);
43 }
44
45 function gotDivResult(result) {
46   cloudeebus.log("gotDivResult: " + result);
47 }
48
49 function gotAddResult(result) {
50   cloudeebus.log("gotAddResult: " + result);
51   window.sampleProxy.Div(result,3.33).then(gotDivResult,errorCB);
52 }
53
54 function signalHandler(result) {
55   cloudeebus.log("signal 'ResultChanged': " + result);
56 }
57
58 function gotProxy(proxy) {
59         proxy.connectToSignal("org.cloudeebus.Sample", "ResultChanged", signalHandler);
60         logCB(proxy);
61         window.sampleProxy = proxy;
62         testProxy();
63 }
64
65 function testProxy() {
66         var dictionary = {Name: "Mickey",
67                                          Sisters: [""], 
68                                          Married: true,
69                                          Divorced: 0,
70                                          Friends: ["Donald", "Dingo"],
71                                          Others: [""]};
72         window.sampleProxy.Variant(dictionary).then(logCB,errorCB);
73           
74         logCB('Addition');
75         for (var i=-2; i<2; i++)
76           window.sampleProxy.Add(i,i*2).then(gotAddResult,errorCB);
77 }
78
79 function connectSuccess() {
80         cloudeebus.SessionBus().getObject("org.cloudeebus.Sample", "/org/cloudeebus/Sample", gotProxy, errorCB);
81 }
82
83 if (!window.sampleProxy)
84         cloudeebus.connect("ws://localhost:9002", manifest, connectSuccess, errorCB);
85 else
86         testProxy();
87
88 </textarea>
89                 <br>
90                 <input type="button" value="run script" onclick="evalScript()"/>
91                 <input type="button" value="clear log" onclick="document.getElementById('log').innerHTML='';"/>
92                 <br>
93                 <pre id="log" style="height: 20em; overflow-x: auto; overflow-y: auto; background-color: #faa;"></pre>
94    </body>
95 </html>