ws
CI / changes (push) Successful in 1m29s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / bff (push) Successful in 29s
CI / chore (push) Successful in 0s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 1s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 0s
CI / product (push) Successful in 1s
CI / record (push) Successful in 0s
CI / sale (push) Successful in 20s
CI / support (push) Successful in 1s
CI / task (push) Successful in 0s
CI / user (push) Successful in 0s
CI / wecom (push) Successful in 1s
CI / changes (push) Successful in 1m29s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / bff (push) Successful in 29s
CI / chore (push) Successful in 0s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 1s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 0s
CI / product (push) Successful in 1s
CI / record (push) Successful in 0s
CI / sale (push) Successful in 20s
CI / support (push) Successful in 1s
CI / task (push) Successful in 0s
CI / user (push) Successful in 0s
CI / wecom (push) Successful in 1s
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/utils"
|
||||
"time"
|
||||
|
||||
"lone-services/rpc/percutaneous/pb"
|
||||
"lone-services/services/percutaneous/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type WsAddUserStreamLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewWsAddUserStreamLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WsAddUserStreamLogic {
|
||||
return &WsAddUserStreamLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 添加用户
|
||||
func (l *WsAddUserStreamLogic) WsAddUserStream(stream percutaneous.Percutaneous_WsAddUserStreamServer) error {
|
||||
l.Logger.Error("in coming ws tunnel stream")
|
||||
ctx := stream.Context()
|
||||
// 扫码超时30s
|
||||
timeout := 30 * time.Second
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
for {
|
||||
// 先判断上下文是否已经结束
|
||||
if err := ctx.Err(); err != nil {
|
||||
if err == context.DeadlineExceeded {
|
||||
return status.Errorf(codes.DeadlineExceeded, "扫码超时")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 阻塞等待客户端上报扫码消息
|
||||
recvFrame, err := stream.Recv()
|
||||
if err != nil {
|
||||
// 客户端主动关闭流
|
||||
return err
|
||||
}
|
||||
|
||||
// 解析扫码payload
|
||||
type res struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
var resData res
|
||||
if err := utils.JsonStringToStruct(string(recvFrame.RawBody), &resData); !err {
|
||||
l.Logger.Errorf("parse scan payload failed: %v", err)
|
||||
return status.Errorf(codes.InvalidArgument, "解析扫码数据失败")
|
||||
}
|
||||
l.Logger.Errorf("receive scan success data: %+v", resData)
|
||||
|
||||
// 下发结果给ws客户端(同步设备)
|
||||
sendFrame := &percutaneous.WsStreamRequest{
|
||||
RawBody: []byte(utils.StructToJson(resData)),
|
||||
}
|
||||
if err := stream.Send(sendFrame); err != nil {
|
||||
l.Logger.Errorf("send result frame failed: %v", err)
|
||||
return err
|
||||
}
|
||||
// 收发完成,直接关闭grpc双向流(ws连接随之断开)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user