A RPC PLATFORM BASE ON SOCKET.IO
INSTALLATION
npm install @ym/rpc [--save]
引入
import RPC from '@ym/rpc'
RPC.install(server, {
users: {
name: {
appid: appid,
appsecret: appsecret
}
...
},
port: port
});
RPC.register({
protocol: protocol,
hostname: hostname,
port: port,
appid: appid,
appsecret: appsecret
});
Get a rpc of a client
let rpc = global.rpcMap.get(username);
Handle a event listener
Emit a socket event
Emit a socket event and got a callback
rpc.route(event, data, callback);
EXAMPLE
import express from 'express';
import http from 'http';
import RPC from '@ym/rpc';
const app = express();
const server = http.createServer(app);
RPC.install(server, {
users:{
xuezi: {
appid: 'xuezi',
appsecret: 123456
}
},
port: 2001
});
app.get('/rpc', function(req, res){
let rpc = global.rpcMap.get('xuezi');
rpc.route('pid', {}, function(data){
res.send(data);
});
});
server.listen(3000);
import express from 'express';
import RPC from '@ym/rpc';
const app = express();
RPC.register( {
protocol: 'http',
hostname: '127.0.0.1',
port: 2001,
appid: 'xuezi',
appsecret: 123456
}).then(function(clientRpc){
clientRpc.on('pid', function(data){
let pid = process.pid;
clientRpc.emit('pid', {pid: pid});
});
app.listen(2000);
}).catch(e=>{
throw e;
});
route a stream
rpc.stream(event, function(stream, data){
console.log(data);
stream.pipe(require('fs').createWriteStream(path));
});
rpc.pipe(event, require('fs').createReadStream(path), data, callback);