mikudos-socketio-app
v1.0.0
Published
mikudos-socketio-app for connection and call methods and sync events on mikudos-socketio-app server, which is provided only for socket.io connection.
Downloads
42
Readme
MIKUDOS SOCKETIO GATE
mikudos-socketio-gate.
Example
Usage
npm install mikudos-socketio-app --save
Import the mikudos-socketio-app module:
import {
Application,
Authentication,
AuthenticationRequest,
RpcServiceMethods,
JSON_RPC_HANDLER,
CHAT_HANDLER,
DUPLEX_HANDLER,
} from 'mikudos-socketio-app';
import http from 'http';
import socket from 'socket.io';
import rpcs from './rpcs';
import publish from './publish';
import authentication from './authentication';
import message from './message';
import duplexs from './duplexs';
import inter_service_clients from './inter_service_clients';
const server = http.createServer();
const io = socket(server);
const app = new Application(io);
app.configure(inter_service_clients);
app.configure(authentication);
app.configure(rpcs);
app.configure(publish);
app.configure(message);
app.configure(duplexs);
app.init();
server.listen(app.get('port'));
console.log('socket.io server started at port: ' + app.get('port'));
var {
Application,
Authentication,
AuthenticationRequest,
RpcServiceMethods,
JSON_RPC_HANDLER,
CHAT_HANDLER,
DUPLEX_HANDLER,
} = require('mikudos-socketio-app');
implement duplex services
import { Application, DUPLEX_HANDLER } from 'mikudos-socketio-app';
import { StreamService } from './stream_service';
// register duplext service on the application
export = function (app: Application): void {
app.duplex_services = new DUPLEX_HANDLER(app, [StreamService]);
};
// file:: stream_service
import { EventEmitter } from 'events';
import { Application, DUPLEX_HANDLER, mikudos } from 'mikudos-socketio-app';
const JsonRpcError = require('json-rpc-error');
const grpc_caller = require('grpc-caller');
import path from 'path';
const LOAD = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const key = 'stream_service';
const serverIp = '127.0.0.1';
const port = '40051';
export class StreamService implements mikudos.DuplexService {
static serviceKey = key;
public before = {
all: [
(methodPath: string, data: any, socket: mikudos.Socket) => {
if (!socket?.mikudos?.user) {
throw new JsonRpcError('Not Authenticated', -32088, {
info: '未认证的请求',
});
}
return data;
},
],
method1: [
(methodPath: string, data: any, socket: mikudos.Socket) => {
// stransport data can be modified before service method
return data;
},
],
};
private protoFile = path.resolve(
__dirname,
'../../proto/stream/stream.proto',
);
private serviceCaller: any;
constructor(private handler: DUPLEX_HANDLER, private app: Application) {
this.serviceCaller = grpc_caller(
`${serverIp}:${port}`,
{ file: this.protoFile, load: LOAD },
'StreamService',
);
}
/**
*
* @param pathStr 请求方法路径: [空间].[方法名]
* @param data 请求参数
* @param event socketIo同步客户端触发事件
*/
async SyncSceneInstance(
eventName: string,
data: any,
socketEvent: EventEmitter,
) {}
}
example stream.proto file
syntax = "proto3";
// import "proto/include/google/protobuf/any.proto";
package stream;
service StreamService {
rpc SyncSceneInstance(stream ClientGameEventMessage)
returns (stream ServerGameEventMessage) {}
}
new update
Add redisAdapter for horizontal spread
The Architecture show the way to cross centralization with help of grpc duplex communication and socketio with Event manager.
Migrate from 0.2.3 to 0.3.x
// 0.2.3
import { Application, DUPLEX_HANDLER } from 'mikudos-socketio-app';
import { key, RealTime } from './real_time_game';
export = function (app: Application): void {
app.duplex_services = new DUPLEX_HANDLER(app, [{ key, sc: RealTime }]);
};
// 0.3.x
import { Application, DUPLEX_HANDLER } from 'mikudos-socketio-app';
import { key, RealTime } from './real_time_game';
export = function (app: Application): void {
app.duplex_services = new DUPLEX_HANDLER(app, [RealTime]);
};