使用midwayjs Koa3调用c#的grpc服务一直报错provider response timeout in 1000,第一次写这种,检查了很多地方都没检查出问题,请大家帮忙看一下,感激不尽!
1.下面是调用服务的代码
@Processor('zktecoqu',{removeOnFail: false,delay: 3000})
export class ZktecoProcessor implements IProcessor {
@Inject()
grpcClients: Clients;
greeterService: greet.ZKTecoClient;
@Init()
async init() {
// 赋值一个服务实例
this.greeterService = this.grpcClients.getService<greet.ZKTecoClient>(
'greet.ZKTeco'
);
}
async execute(data: any) {
const c = this.greeterService.connect({ timeout: 60000 });
const datas = {
id: 'zhls3c',
hostname: '10.100.200.201',
port: 4370,
password: 244525,
action: 0
}
const result = await new Promise<number>((resolve, reject) => {
// 第一次调用和返回
c.sendMessage(datas)
.then(res => {
console.log('res',res)
})
.catch(err => console.error('err',err));
});
console.log('connectresult', result)
}
}
2.下面是c#服务端的代码
public override Task Connect(IAsyncStreamReader<ConnectParam> requestStream, IServerStreamWriter<ConnectStatus> responseStream, ServerCallContext context)
{
Console.WriteLine(requestStream);
// 先獲取一下設備狀態
foreach (var kvp in instances)
{
int dwErrorCode = 0;
kvp.Value.GetConnectStatus(ref dwErrorCode);
if (dwErrorCode == 0)
{
var status = new ConnectStatus();
status.Id = kvp.Key;
status.Connected = dwErrorCode == 0;
status.Message = dwErrorCode == 0 ? null : $"error code: {dwErrorCode}";
responseStream.WriteAsync(status);
}
}
using (var subscription = statusSubject.Subscribe(res =>
{
responseStream.WriteAsync(res);
}))
{
// 在客戶端取消請求的時候,停止訂閱
context.CancellationToken.Register(() =>
{
subscription.Dispose();
});
}
return Task.Run(async () =>
{
await foreach (var message in requestStream.ReadAllAsync())
{
switch (message.Action)
{
case 0:
var ins = new zkemkeeper.CZKEMClass();
if (message.Password != null)
{
ins.SetCommPassword((int)message.Password);
}
ins.Connect_Net(message.Hostname, (int)message.Port);
break;
}
// Process request.
}
});
}