| Bryan Duxbury | dc4a871 | 2010-07-26 17:18:30 +0000 | [diff] [blame^] | 1 | <!-- | 
 | 2 |   Licensed to the Apache Software Foundation (ASF) under one | 
 | 3 |   or more contributor license agreements. See the NOTICE file | 
 | 4 |   distributed with this work for additional information | 
 | 5 |   regarding copyright ownership. The ASF licenses this file | 
 | 6 |   to you under the Apache License, Version 2.0 (the | 
 | 7 |   "License"); you may not use this file except in compliance | 
 | 8 |   with the License. You may obtain a copy of the License at | 
 | 9 |  | 
 | 10 |     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 11 |  | 
 | 12 |   Unless required by applicable law or agreed to in writing, | 
 | 13 |   software distributed under the License is distributed on an | 
 | 14 |   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
 | 15 |   KIND, either express or implied. See the License for the | 
 | 16 |   specific language governing permissions and limitations | 
 | 17 |   under the License. | 
 | 18 | --> | 
 | 19 |  | 
| T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 20 | <html> | 
 | 21 | <head> | 
 | 22 |   <title>Thrift Javascript Bindings - Example</title> | 
 | 23 |  | 
 | 24 |   <script src="/thrift.js"                 type="text/javascript"></script> | 
 | 25 |   <script src="gen-js/ThriftTest_types.js" type="text/javascript"></script> | 
 | 26 |   <script src="gen-js/ThriftTest.js"       type="text/javascript"></script> | 
 | 27 |  | 
 | 28 |   <!-- for async example --> | 
 | 29 |   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> | 
 | 30 |  | 
 | 31 | </head> | 
 | 32 | <body id="body"> | 
 | 33 |  | 
 | 34 | <script language="javascript"> | 
 | 35 |  | 
 | 36 |   //create client | 
 | 37 |   var transport = new Thrift.Transport("/service") | 
 | 38 |   var protocol  = new Thrift.Protocol(transport) | 
 | 39 |   var client    = new ThriftTest.ThriftTestClient(protocol) | 
 | 40 |  | 
 | 41 |   //create insanity obj | 
 | 42 |   var insanity  = new ThriftTest.Insanity() | 
 | 43 |   insanity.userMap[ThriftTest.Numberz.ONE] = 1 | 
 | 44 |   insanity.userMap[ThriftTest.Numberz.TWO] = 2 | 
 | 45 |  | 
 | 46 |   var xtr = new ThriftTest.Xtruct() | 
 | 47 |   xtr.string_thing = 'worked' | 
 | 48 |   insanity.xtructs.push(xtr) | 
 | 49 |  | 
 | 50 |   var xtr2= new ThriftTest.Xtruct2() | 
 | 51 |   xtr2.struct_thing = xtr | 
 | 52 |  | 
 | 53 |   var list = [1,2,3] | 
 | 54 |  | 
 | 55 |   //run tests synchronously | 
 | 56 |  | 
 | 57 |   document.write("<h2><u>Thrift Javascript Bindings</u></h2>") | 
 | 58 |   document.write("<h2>Synchronous Example</h2>") | 
 | 59 |   document.write("client.testString() => "+(client.testString("works")     == "works")+"<br/>") | 
 | 60 |   document.write("client.testString(utf-8) => "+(client.testString("ae")   == "ae")+"<br/>") | 
 | 61 |   document.write("client.testDouble() => "+(client.testDouble(3.14)        == 3.14)+"<br/>") | 
 | 62 |   document.write("client.testByte()   => "+(client.testByte(0x01)          == 0x01)+"<br/>") | 
 | 63 |   document.write("client.testI32()    => "+(client.testI32(Math.pow(2,30)) == Math.pow(2,30))+"<br/>") | 
 | 64 |   document.write("client.testI64()    => "+(client.testI64(Math.pow(2,60)) == Math.pow(2,60))+"<br/>") | 
 | 65 |   document.write("client.testStruct() => "+(client.testStruct(xtr).string_thing == "worked")+"<br/>") | 
 | 66 |   document.write("client.testNest()   => "+(client.testNest(xtr2).struct_thing.string_thing == "worked")+"<br/>") | 
 | 67 |   document.write("client.testMap()    => "+(client.testMap(insanity.userMap)[ThriftTest.Numberz.ONE] == 1)+"<br/>") | 
| T Jake Luciani | 127909c | 2010-07-10 14:58:51 +0000 | [diff] [blame] | 68 |   document.write("client.testList()   => "+(client.testList(list).length == 3 && client.testList(list)[1] == 2)+"<br/>") | 
| T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 69 |   document.write("client.testSet()    => "+(client.testSet(list).length == 3)+"<br/>") | 
 | 70 |   document.write("client.testEnum()   => "+(client.testEnum(ThriftTest.Numberz.ONE) == ThriftTest.Numberz.ONE)+"<br/>") | 
 | 71 |  | 
 | 72 |   document.write("client.testException() => ") | 
 | 73 |   try{ | 
 | 74 |     client.testException("go") | 
 | 75 |     document.write("false<br/>") | 
 | 76 |   }catch(e){ | 
 | 77 |     document.write("true<br/>") | 
 | 78 |   } | 
 | 79 |  | 
 | 80 |   document.write("client.testInsanity() => ") | 
 | 81 |   var res = client.testInsanity(insanity) | 
 | 82 |  | 
 | 83 |   document.write((res["1"]["1"].xtructs[0].string_thing == "worked")+"<br/>") | 
 | 84 |  | 
 | 85 |   ////////////////////////////////// | 
 | 86 |   //Run same tests asynchronously | 
 | 87 |  | 
 | 88 |   var transport = new Thrift.Transport() | 
 | 89 |   var protocol  = new Thrift.Protocol(transport) | 
 | 90 |   var client    = new ThriftTest.ThriftTestClient(protocol) | 
 | 91 |  | 
 | 92 |   document.write("<h2>Asynchronous Example</h2>") | 
 | 93 |   jQuery.ajax({ | 
 | 94 |      url: "/service", | 
 | 95 |      data: client.send_testI32(Math.pow(2,30)), | 
 | 96 |      type: "POST", | 
 | 97 |      cache: false, | 
 | 98 |      success: function(res){ | 
 | 99 |             var _transport = new Thrift.Transport() | 
 | 100 |             var _protocol  = new Thrift.Protocol(_transport) | 
 | 101 |             var _client    = new ThriftTest.ThriftTestClient(_protocol) | 
 | 102 |  | 
 | 103 |             _transport.setRecvBuffer( res ) | 
 | 104 |  | 
 | 105 |             var v =  _client.recv_testI32() | 
 | 106 |             $("#body").append("client.testI32()    => "+(v == Math.pow(2,30))+"<br/>") | 
 | 107 |  | 
 | 108 |      } | 
 | 109 |   }) | 
 | 110 |  | 
 | 111 |   jQuery.ajax({ | 
 | 112 |      url: "/service", | 
 | 113 |      data: client.send_testI64(Math.pow(2,60)), | 
 | 114 |      type: "POST", | 
 | 115 |      cache: false, | 
 | 116 |      success: function(res){ | 
 | 117 |             var _transport = new Thrift.Transport() | 
 | 118 |             var _protocol  = new Thrift.Protocol(_transport) | 
 | 119 |             var _client    = new ThriftTest.ThriftTestClient(_protocol) | 
 | 120 |  | 
 | 121 |             _transport.setRecvBuffer( res ) | 
 | 122 |  | 
 | 123 |             var v =  _client.recv_testI64() | 
 | 124 |             $("#body").append("client.testI64()    => "+(v == Math.pow(2,60))+"<br/>") | 
 | 125 |  | 
 | 126 |      } | 
 | 127 |   }) | 
 | 128 |  | 
 | 129 |  | 
 | 130 |  | 
 | 131 | </script> | 
 | 132 |  | 
 | 133 | </body> | 
 | 134 | </html> |