[add] first
This commit is contained in:
21
test/try/server/api/ApiTest.ts
Normal file
21
test/try/server/api/ApiTest.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { TsrpcError } from "tsrpc-proto";
|
||||
import { ApiCallHttp } from '../../../src/server/http/HttpCall';
|
||||
import { ReqTest } from "../../proto/PtlTest";
|
||||
import { ResTest } from '../../proto/a/b/c/PtlTest';
|
||||
|
||||
export async function ApiTest(call: ApiCallHttp<ReqTest, ResTest>) {
|
||||
if (Math.random() > 0.75) {
|
||||
call.succ({
|
||||
reply: 'Hello, ' + call.req.name
|
||||
})
|
||||
}
|
||||
else if (Math.random() > 0.5) {
|
||||
call.error('What the fuck??', { msg: '哈哈哈哈' })
|
||||
}
|
||||
else if (Math.random() > 0.25) {
|
||||
throw new Error('这应该是InternalERROR')
|
||||
}
|
||||
else {
|
||||
throw new TsrpcError('返回到前台的错误', 'ErrInfo');
|
||||
}
|
||||
}
|
||||
5
test/try/server/api/a/b/c/ApiTest.ts
Normal file
5
test/try/server/api/a/b/c/ApiTest.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export async function ApiTest(call: any) {
|
||||
call.succ({
|
||||
reply: 'Api Test1 Succ'
|
||||
})
|
||||
}
|
||||
22
test/try/server/http.ts
Normal file
22
test/try/server/http.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { serviceProto } from '../proto/serviceProto';
|
||||
import * as path from "path";
|
||||
import { TsrpcServer } from '../../index';
|
||||
|
||||
let server = new TsrpcServer({
|
||||
proto: serviceProto
|
||||
});
|
||||
|
||||
server.dataFlow.push((data, conn) => {
|
||||
let httpReq = conn.options.httpReq;
|
||||
if (httpReq.method === 'GET') {
|
||||
conn.logger.log('url', httpReq.url);
|
||||
conn.logger.log('host', httpReq.headers.host);
|
||||
conn.options.httpRes.end('Hello~~~');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
|
||||
server.autoImplementApi(path.resolve(__dirname, 'api'));
|
||||
server.start();
|
||||
18
test/try/server/ws.ts
Normal file
18
test/try/server/ws.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { serviceProto, ServiceType } from '../proto/serviceProto';
|
||||
import * as path from "path";
|
||||
import { TsrpcServerWs } from '../../index';
|
||||
|
||||
let server = new TsrpcServerWs<ServiceType>({
|
||||
proto: serviceProto
|
||||
});
|
||||
server.start();
|
||||
|
||||
server.autoImplementApi(path.resolve(__dirname, 'api'));
|
||||
server.listenMsg('Chat', v => {
|
||||
v.conn.sendMsg('Chat', {
|
||||
channel: v.msg.channel,
|
||||
userName: 'SYSTEM',
|
||||
content: '收到',
|
||||
time: Date.now()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user