|
| 1 | +# RPC Server-side Websocket Responder |
| 2 | +# RPC 서버사이드 웹소켓 Responder |
| 3 | + |
| 4 | +The RPC Responder |
| 5 | +allows you |
| 6 | +to call functions on the server |
| 7 | +from the browser over the websocket, |
| 8 | +returning a response if requested. |
| 9 | +RPC Responder는 당신이 브러우저의 웹소켓을 통해 서버의 함수들의 호출을 한 경우에 response를 반환 합니다. |
| 10 | + |
| 11 | +It is powerful when used in conjunction with [Request Middleware](https://github.com/socketstream/socketstream/blob/master/doc/guide/en/request_middleware.md). |
| 12 | +그것은 [Request Middleware](https://github.com/socketstream/socketstream/blob/master/doc/guide/en/request_middleware.md)와 함께 할때 강력합니다. |
| 13 | + |
| 14 | +To make a Remote Procedure Call from the browser use the `ss.rpc()` function. |
| 15 | +브라우저에서 원격 프로시져 콜을 할수 있는 `ss.rpc()` 함수를 사용하세요. |
| 16 | + |
| 17 | +Let's assume we want to return an array of the latest products in an online store. |
| 18 | +우리가 온라인 스토어의 최근의 제품들의 배열을 반환 하고 싶다고 가정합니다. |
| 19 | + |
| 20 | +We would want to call the following command from the browser: |
| 21 | +브라우저에서 다음 명령을 호출 하도록 합시다. |
| 22 | + |
| 23 | +``` javascript |
| 24 | +ss.rpc('products.latest', function(result){ console.log('The latest products are:', result); }) |
| 25 | +``` |
| 26 | + |
| 27 | +This command will be sent over the websocket and sent directly to the RPC Responder. But how will it know which function to call on the server? |
| 28 | +이 명령은 웹소켓을 통해 전송되고 직접적으로 RPC Responder에게 전달 될 것입니다. |
| 29 | + |
| 30 | +The RPC responder loads all commands in `server/rpc` into an API Tree. |
| 31 | +RPC Responder는 `server/rpc`아래 모든 명령들을 API 트리로 로드 합니다. |
| 32 | + |
| 33 | + Thus the command to call 'products.latest' will be matched to the 'latest' function in the 'products' file `/server/rpc/products.js`. |
| 34 | +그래서 그명령어는 `/server/rpc/products.js`.파일에서 'products' 안에 'latest'와 일치 되는 'products.latest'를 호출 할 것입니다. |
| 35 | + |
| 36 | +The `products.js` file should contain the available actions as so: |
| 37 | +`products.js` 파일은 이처럼 적당한 action들을 포함해야 함. |
| 38 | + |
| 39 | +``` javascript |
| 40 | +// server/rpc/products.js |
| 41 | +exports.actions = function(req, res, ss){ |
| 42 | + |
| 43 | + return { |
| 44 | + |
| 45 | + latest: function(){ |
| 46 | + res(['iPhone 4S', 'Dell LCD TV', 'HP Printer']); |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Sending Arguments |
| 54 | +### 매개변수 전달 |
| 55 | + |
| 56 | +The RPC Responder can take and pass unlimited arguments intuitively. |
| 57 | +RPC Responder 는 무제한의 인자를 지관적으로 얻거나 전달 할 수 있습니다. |
| 58 | + |
| 59 | +For example let's write another action on the server: |
| 60 | +예로 서버에 또 다른 액션을 작성하자: |
| 61 | + |
| 62 | +``` javascript |
| 63 | +// server/rpc/products.js |
| 64 | +exports.actions = function(req, res, ss){ |
| 65 | + |
| 66 | + return { |
| 67 | + |
| 68 | + topSelling: function(startDate, endDate, productType){ |
| 69 | + // do calculations then send multiple args back to client... |
| 70 | + res(['iPad', 'iPhone', ...], 'Scooby Doo'); |
| 71 | + }, |
| 72 | + |
| 73 | + latest: function(){ |
| 74 | + res(['iPhone', 'Dell LCD TV', 'HP Printer']); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +To call this from the browser we'd use: |
| 82 | +이것을 우리가 사용하는 브러우저에서 호출: |
| 83 | + |
| 84 | +``` javascript |
| 85 | +// client/code/main/products.js |
| 86 | +var productType = 'electronics'; |
| 87 | +ss.rpc('products.topSelling', '2012-01-01', '2012-01-31', productType, function(products, bestSalesperson) { |
| 88 | + console.log('The top selling products in ' + productType + ' were:', products); |
| 89 | + console.log('And the best salesperson was:', bestSalesperson); |
| 90 | +}) |
| 91 | + |
| 92 | +``` |
| 93 | + |
| 94 | +You may pass as many arguments as you want - just remember the last argument should always be the callback if you're expecting a response from the server. |
| 95 | +당신이 원하는 만큼 많이 전달 할수 있습니다. - 단 서버로부터 응답을 기대한다면 마지막 인자는 항상 콜백(함수)라는 것을기억하세요. |
| 96 | + |
| 97 | +### How does it work under the hood? |
| 98 | +### 그것은 내부에서 어떻게 동작하는 것일까요? |
| 99 | + |
| 100 | +The RPC Responder serializes messages in both directions using JSON. |
| 101 | +RPC Responder는 JSON을 이용하여 쌍방향으로 메세지들을 직렬화 합니다. |
| 102 | + |
| 103 | +Thus the actual message sent over the wire is a string which looks like this: |
| 104 | +그래서 실제로 보내지 선을 넘어로(네트워크를 넘어로)보내지는 내용은 이런식의 문자열입니다: |
| 105 | + |
| 106 | + |
| 107 | + {id: 1, m: 'method.to.call', p: [param1, param2, ...]} |
0 commit comments