T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 1 | Thrift Javascript Library |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 2 | ========================= |
| 3 | This browser based Apache Thrift implementation supports |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 4 | RPC clients using the JSON protocol over Http[s] with XHR |
| 5 | and WebSocket. |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 6 | |
| 7 | License |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 8 | ------- |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 9 | Licensed to the Apache Software Foundation (ASF) under one |
| 10 | or more contributor license agreements. See the NOTICE file |
| 11 | distributed with this work for additional information |
| 12 | regarding copyright ownership. The ASF licenses this file |
| 13 | to you under the Apache License, Version 2.0 (the |
| 14 | "License"); you may not use this file except in compliance |
| 15 | with the License. You may obtain a copy of the License at |
| 16 | |
| 17 | http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | |
| 19 | Unless required by applicable law or agreed to in writing, |
| 20 | software distributed under the License is distributed on an |
| 21 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 22 | KIND, either express or implied. See the License for the |
| 23 | specific language governing permissions and limitations |
| 24 | under the License. |
| 25 | |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 26 | Grunt Build |
| 27 | ------------ |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame^] | 28 | This is the base directory for the Apache Thrift JavaScript |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 29 | library. This directory contains a Gruntfile.js and a |
| 30 | package.json. Many of the build and test tools used here |
| 31 | require a recent version of Node.js to be installed. To |
| 32 | install the support files for the Grunt build tool execute |
| 33 | the command: |
| 34 | $ npm install |
| 35 | This reads the package.json and pulls in the appropriate |
| 36 | sources from the internet. To build the JavaScript branch |
| 37 | of Apache Thrift execute the command: |
| 38 | $ grunt |
| 39 | This runs the grunt build tool, linting all of the source |
| 40 | files, setting up and running the tests, concatenating and |
| 41 | minifying the main libraries and generating the html |
| 42 | documentation. |
| 43 | |
| 44 | Tree |
| 45 | ---- |
| 46 | The following directories are present (some only after the |
| 47 | grunt build): |
| 48 | /src - The JavaScript Apache Thrift source |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame^] | 49 | /doc - HTML documentation |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 50 | /dist - Distribution files (thrift.js and thrift.min.js) |
| 51 | /test - Various tests, this is a good place to look for |
| 52 | example code |
| 53 | /node_modules - Build support files installed by npm |
| 54 | |
| 55 | |
| 56 | Example JavaScript Client and Server |
| 57 | ------------------------------------ |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 58 | The listing below demonstrates a simple browser based JavaScript |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 59 | Thrift client and Node.js JavaScript server for the hello_svc |
| 60 | service. |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 61 | |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 62 | ### hello.thrift - Service IDL |
| 63 | ### build with: $ thrift -gen js -gen js:node hello.thrift |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 64 | service hello_svc { |
| 65 | string get_message(1: string name) |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 66 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 67 | |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 68 | ### hello.html - Browser Client |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 69 | <!DOCTYPE html> |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 70 | <html lang="en"> |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 71 | <head> |
| 72 | <meta charset="utf-8"> |
| 73 | <title>Hello Thrift</title> |
| 74 | </head> |
| 75 | <body> |
| 76 | Name: <input type="text" id="name_in"> |
| 77 | <input type="button" id="get_msg" value="Get Message" > |
| 78 | <div id="output"></div> |
| 79 | |
| 80 | <script src="thrift.js"></script> |
| 81 | <script src="gen-js/hello_svc.js"></script> |
| 82 | <script> |
| 83 | (function() { |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame^] | 84 | var transport = new Thrift.TXHRTransport("/hello"); |
| 85 | var protocol = new Thrift.TJSONProtocol(transport); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 86 | var client = new hello_svcClient(protocol); |
| 87 | var nameElement = document.getElementById("name_in"); |
| 88 | var outputElement = document.getElementById("output"); |
| 89 | document.getElementById("get_msg") |
| 90 | .addEventListener("click", function(){ |
| 91 | client.get_message(nameElement.value, function(result) { |
| 92 | outputElement.innerHTML = result; |
| 93 | }); |
| 94 | }); |
| 95 | })(); |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 96 | </script> |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 97 | </body> |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 98 | </html> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 99 | |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 100 | ### hello.js - Node Server |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame^] | 101 | var thrift = require('thrift'); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 102 | var hello_svc = require('./gen-nodejs/hello_svc.js'); |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 103 | |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 104 | var hello_handler = { |
| 105 | get_message: function(name, result) { |
| 106 | var msg = "Hello " + name + "!"; |
| 107 | result(null, msg); |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 108 | } |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 109 | } |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 110 | |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 111 | var hello_svc_opt = { |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame^] | 112 | transport: thrift.TBufferedTransport, |
| 113 | protocol: thrift.TJSONProtocol, |
| 114 | processor: hello_svc, |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 115 | handler: hello_handler |
| 116 | }; |
| 117 | |
| 118 | var server_opt = { |
| 119 | staticFilePath: ".", |
| 120 | services: { |
| 121 | "/hello": hello_svc_opt |
| 122 | } |
| 123 | } |
| 124 | |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame^] | 125 | var server = Thrift.createWebServer(server_opt); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 126 | var port = 9099; |
| 127 | server.listen(port); |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 128 | console.log("Http/Thrift Server running on port: " + port); |
| 129 | |