blob: 971f6da9214a08c717f0e9acd3d98dc66789dfa5 [file] [log] [blame]
T Jake Luciani322caa22010-02-15 03:24:55 +00001Thrift Javascript Library
Henrique Mendonça095ddb72013-09-20 19:38:03 +02002=========================
3This browser based Apache Thrift implementation supports
henriquea2de4102014-02-07 14:12:56 +01004RPC clients using the JSON protocol over Http[s] with XHR.
T Jake Luciani322caa22010-02-15 03:24:55 +00005
6License
Henrique Mendonça095ddb72013-09-20 19:38:03 +02007-------
T Jake Luciani322caa22010-02-15 03:24:55 +00008Licensed to the Apache Software Foundation (ASF) under one
9or more contributor license agreements. See the NOTICE file
10distributed with this work for additional information
11regarding copyright ownership. The ASF licenses this file
12to you under the Apache License, Version 2.0 (the
13"License"); you may not use this file except in compliance
14with the License. You may obtain a copy of the License at
15
16 http://www.apache.org/licenses/LICENSE-2.0
17
18Unless required by applicable law or agreed to in writing,
19software distributed under the License is distributed on an
20"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21KIND, either express or implied. See the License for the
22specific language governing permissions and limitations
23under the License.
24
Henrique Mendonça095ddb72013-09-20 19:38:03 +020025Example
26-------
27The listing below demonstrates a simple browser based JavaScript
henriquea2de4102014-02-07 14:12:56 +010028Thrift client and Node.js JavaScript server for the hello_svc
29service.
T Jake Luciani322caa22010-02-15 03:24:55 +000030
Henrique Mendonça095ddb72013-09-20 19:38:03 +020031### hello.thrift - Service IDL
henriquea2de4102014-02-07 14:12:56 +010032 service hello_svc {
33 string get_message(1: string name)
Henrique Mendonça095ddb72013-09-20 19:38:03 +020034 }
T Jake Luciani322caa22010-02-15 03:24:55 +000035
Henrique Mendonça095ddb72013-09-20 19:38:03 +020036### hello.html - Browser Client
henriquea2de4102014-02-07 14:12:56 +010037 <!DOCTYPE html>
Henrique Mendonça095ddb72013-09-20 19:38:03 +020038 <html lang="en">
henriquea2de4102014-02-07 14:12:56 +010039 <head>
40 <meta charset="utf-8">
41 <title>Hello Thrift</title>
42 </head>
43 <body>
44 Name: <input type="text" id="name_in">
45 <input type="button" id="get_msg" value="Get Message" >
46 <div id="output"></div>
47
48 <script src="thrift.js"></script>
49 <script src="gen-js/hello_svc.js"></script>
50 <script>
51 (function() {
52 var transport = new Thrift.Transport("/hello");
53 var protocol = new Thrift.Protocol(transport);
54 var client = new hello_svcClient(protocol);
55 var nameElement = document.getElementById("name_in");
56 var outputElement = document.getElementById("output");
57 document.getElementById("get_msg")
58 .addEventListener("click", function(){
59 client.get_message(nameElement.value, function(result) {
60 outputElement.innerHTML = result;
61 });
62 });
63 })();
Henrique Mendonça095ddb72013-09-20 19:38:03 +020064 </script>
henriquea2de4102014-02-07 14:12:56 +010065 </body>
Henrique Mendonça095ddb72013-09-20 19:38:03 +020066 </html>
T Jake Luciani322caa22010-02-15 03:24:55 +000067
Henrique Mendonça095ddb72013-09-20 19:38:03 +020068### hello.js - Node Server
henriquea2de4102014-02-07 14:12:56 +010069 var Thrift = require('thrift');
70 var TBufferedTransport = require('thrift/lib/thrift/transport').TBufferedTransport;
71 var TJSONProtocol = require('thrift/lib/thrift/protocol').TJSONProtocol;
72 var hello_svc = require('./gen-nodejs/hello_svc.js');
Henrique Mendonça095ddb72013-09-20 19:38:03 +020073
henriquea2de4102014-02-07 14:12:56 +010074 var hello_handler = {
75 get_message: function(name, result) {
76 var msg = "Hello " + name + "!";
77 result(null, msg);
Henrique Mendonça095ddb72013-09-20 19:38:03 +020078 }
henriquea2de4102014-02-07 14:12:56 +010079 }
Henrique Mendonça095ddb72013-09-20 19:38:03 +020080
henriquea2de4102014-02-07 14:12:56 +010081 var hello_svc_opt = {
82 transport: TBufferedTransport,
83 protocol: TJSONProtocol,
84 cls: hello_svc,
85 handler: hello_handler
86 };
87
88 var server_opt = {
89 staticFilePath: ".",
90 services: {
91 "/hello": hello_svc_opt
92 }
93 }
94
95 var server = Thrift.createStaticHttpThriftServer(server_opt);
96 var port = 9099;
97 server.listen(port);
98 console.log("Http/Thrift Server running on port: " + port);`