Add time RPC call to pomelo's chat room

Source: Internet
Author: User
Tags add time

Add an RPC call and a time-type server to the chat application. In servers/time/remote/timeremote. JS, add the following code:

module.exports.getCurrentTime = function (arg1, arg2, cb) {  console.log("timeRemote - arg1: " + arg1+ "; " + "arg2: " + arg2);  var d = new Date();  var hour = d.getHours();  var min = d.getMinutes();  var sec = d.getSeconds();  cb( hour, min, sec);};

After being called in gate, console comes out:

// gateHandler.jsvar routeParam = Math.floor(Math.random()*10);app.rpc.time.timeRemote.getCurrentTime(routeParam, arg1, arg2,  function(hour, min, sec) {  // ...});

When there are multiple time servers, you need to configure the route rule in APP. JS:

var router = function(routeParam, msg, context, cb) {  var timeServers = app.getServersByType(‘time‘);  var id = timeServers[routeParam % timeServers.length].id;  cb(null, id);}app.route(‘time‘, router);

Add the time server to config/servers. JSON in the server configuration as follows:

"time":[  {"id": "time-server-1", "host":"127.0.0.1", "port" : 7000},  {"id": "time-server-2", "host":"127.0.0.1", "port" : 7001}]

Note that there are two export methods: the export object and Function

module.exports = function(app) {    return new ChatRemote(app);};// timeRemote.jsmodule.exports.getCurrentTime(arg1, arg2, cb) {  // ...};

When the front-end Server accepts client requests and routes requests to the backend server, pomelo uses a system-level Remote Call method. In this case, session is used as the routing parameter of the RPC request, this is also what we see before When configuring routing for chat, the first parameter of the routing function is always session. In time, we use a random Integer as the routing parameter. Therefore, the first parameter of the time routing function is the random integer. In fact, the RPC framework of pomelo has no restrictions on the use of Routing Parameters, and is not limited to the continuous use of sessions.

After version 0.8, when calling RPC, you can skip route computing and directly send the call to a specific server or broadcast the call method to a type of server. The sample code is as follows:

// Routevar routeparam = session; app. RPC. area. playerremote. leaveteam (routeparam, argS ..., CB); // route calculation based on routeparam // to specified server 'area-server-1' app. RPC. area. playerremote. leaveteam. toserver ('area-server-1 ', argS ..., CB); // directly specify the route // broadcast to all the area serversapp. RPC. area. playerremote. leaveteam. toserver ('*', argS ..., CB); // specify all routes

  

Add time RPC call to pomelo's chat room

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.