T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame^] | 1 | <html> |
| 2 | <head> |
| 3 | <title>Thrift Javascript Bindings - Example</title> |
| 4 | |
| 5 | <script src="/thrift.js" type="text/javascript"></script> |
| 6 | <script src="gen-js/ThriftTest_types.js" type="text/javascript"></script> |
| 7 | <script src="gen-js/ThriftTest.js" type="text/javascript"></script> |
| 8 | |
| 9 | <!-- for async example --> |
| 10 | <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> |
| 11 | |
| 12 | </head> |
| 13 | <body id="body"> |
| 14 | |
| 15 | <script language="javascript"> |
| 16 | |
| 17 | //create client |
| 18 | var transport = new Thrift.Transport("/service") |
| 19 | var protocol = new Thrift.Protocol(transport) |
| 20 | var client = new ThriftTest.ThriftTestClient(protocol) |
| 21 | |
| 22 | //create insanity obj |
| 23 | var insanity = new ThriftTest.Insanity() |
| 24 | insanity.userMap[ThriftTest.Numberz.ONE] = 1 |
| 25 | insanity.userMap[ThriftTest.Numberz.TWO] = 2 |
| 26 | |
| 27 | var xtr = new ThriftTest.Xtruct() |
| 28 | xtr.string_thing = 'worked' |
| 29 | insanity.xtructs.push(xtr) |
| 30 | |
| 31 | var xtr2= new ThriftTest.Xtruct2() |
| 32 | xtr2.struct_thing = xtr |
| 33 | |
| 34 | var list = [1,2,3] |
| 35 | |
| 36 | //run tests synchronously |
| 37 | |
| 38 | document.write("<h2><u>Thrift Javascript Bindings</u></h2>") |
| 39 | document.write("<h2>Synchronous Example</h2>") |
| 40 | document.write("client.testString() => "+(client.testString("works") == "works")+"<br/>") |
| 41 | document.write("client.testString(utf-8) => "+(client.testString("ae") == "ae")+"<br/>") |
| 42 | document.write("client.testDouble() => "+(client.testDouble(3.14) == 3.14)+"<br/>") |
| 43 | document.write("client.testByte() => "+(client.testByte(0x01) == 0x01)+"<br/>") |
| 44 | document.write("client.testI32() => "+(client.testI32(Math.pow(2,30)) == Math.pow(2,30))+"<br/>") |
| 45 | document.write("client.testI64() => "+(client.testI64(Math.pow(2,60)) == Math.pow(2,60))+"<br/>") |
| 46 | document.write("client.testStruct() => "+(client.testStruct(xtr).string_thing == "worked")+"<br/>") |
| 47 | document.write("client.testNest() => "+(client.testNest(xtr2).struct_thing.string_thing == "worked")+"<br/>") |
| 48 | document.write("client.testMap() => "+(client.testMap(insanity.userMap)[ThriftTest.Numberz.ONE] == 1)+"<br/>") |
| 49 | document.write("client.testList() => "+(client.testList(list).length == 3)+"<br/>") |
| 50 | document.write("client.testSet() => "+(client.testSet(list).length == 3)+"<br/>") |
| 51 | document.write("client.testEnum() => "+(client.testEnum(ThriftTest.Numberz.ONE) == ThriftTest.Numberz.ONE)+"<br/>") |
| 52 | |
| 53 | document.write("client.testException() => ") |
| 54 | try{ |
| 55 | client.testException("go") |
| 56 | document.write("false<br/>") |
| 57 | }catch(e){ |
| 58 | document.write("true<br/>") |
| 59 | } |
| 60 | |
| 61 | document.write("client.testInsanity() => ") |
| 62 | var res = client.testInsanity(insanity) |
| 63 | |
| 64 | document.write((res["1"]["1"].xtructs[0].string_thing == "worked")+"<br/>") |
| 65 | |
| 66 | ////////////////////////////////// |
| 67 | //Run same tests asynchronously |
| 68 | |
| 69 | var transport = new Thrift.Transport() |
| 70 | var protocol = new Thrift.Protocol(transport) |
| 71 | var client = new ThriftTest.ThriftTestClient(protocol) |
| 72 | |
| 73 | document.write("<h2>Asynchronous Example</h2>") |
| 74 | jQuery.ajax({ |
| 75 | url: "/service", |
| 76 | data: client.send_testI32(Math.pow(2,30)), |
| 77 | type: "POST", |
| 78 | cache: false, |
| 79 | success: function(res){ |
| 80 | var _transport = new Thrift.Transport() |
| 81 | var _protocol = new Thrift.Protocol(_transport) |
| 82 | var _client = new ThriftTest.ThriftTestClient(_protocol) |
| 83 | |
| 84 | _transport.setRecvBuffer( res ) |
| 85 | |
| 86 | var v = _client.recv_testI32() |
| 87 | $("#body").append("client.testI32() => "+(v == Math.pow(2,30))+"<br/>") |
| 88 | |
| 89 | } |
| 90 | }) |
| 91 | |
| 92 | jQuery.ajax({ |
| 93 | url: "/service", |
| 94 | data: client.send_testI64(Math.pow(2,60)), |
| 95 | type: "POST", |
| 96 | cache: false, |
| 97 | success: function(res){ |
| 98 | var _transport = new Thrift.Transport() |
| 99 | var _protocol = new Thrift.Protocol(_transport) |
| 100 | var _client = new ThriftTest.ThriftTestClient(_protocol) |
| 101 | |
| 102 | _transport.setRecvBuffer( res ) |
| 103 | |
| 104 | var v = _client.recv_testI64() |
| 105 | $("#body").append("client.testI64() => "+(v == Math.pow(2,60))+"<br/>") |
| 106 | |
| 107 | } |
| 108 | }) |
| 109 | |
| 110 | |
| 111 | |
| 112 | </script> |
| 113 | |
| 114 | </body> |
| 115 | </html> |