Files
gjs 9260e75aa4
CI / changes (push) Successful in 1m29s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / bff (push) Successful in 35s
CI / chore (push) Successful in 1s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 1s
CI / message (push) Successful in 0s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 1s
CI / percutaneous (push) Successful in 21s
CI / product (push) Successful in 0s
CI / record (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / support (push) Successful in 0s
CI / user (push) Successful in 0s
CI / task (push) Successful in 0s
CI / wecom (push) Successful in 0s
ws
2026-10-08 17:53:16 +08:00

59 lines
1.6 KiB
Go

package wsClient
import (
"context"
"fmt"
"lone-services/bff/internal/request"
"lone-services/bff/internal/wsproxy"
percutaneous "lone-services/rpc/percutaneous/pb"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc"
"google.golang.org/protobuf/reflect/protoreflect"
)
func PercutaneousAddUser(rpcMgr *wsproxy.RpcClientManager) http.HandlerFunc {
return request.UserReadMiddleware(func(w http.ResponseWriter, r *http.Request) {
cli, ready := rpcMgr.GetClient()
if !ready {
logx.Error("BFF: rpc client not ready, return 503")
http.Error(w, "rpc service unavailable", http.StatusServiceUnavailable)
return
}
hostCode := r.URL.Query().Get("host_code")
if hostCode == "" {
logx.Error("BFF: host_code is empty")
http.Error(w, "missing host_code query param", http.StatusBadRequest)
return
}
proxyHandler := wsproxy.NewWsProxyHandler(
func(parentCtx context.Context) (grpc.ClientStream, error) {
streamCli := percutaneous.NewPercutaneousClient(cli.Conn())
stream, err := streamCli.WsAddUserStream(parentCtx)
if err != nil {
logx.Errorf("BFF: create WsAddUserStream failed, err=%v", err)
return nil, err
}
jsonStr := fmt.Sprintf(`{"host_code":"%s"}`, hostCode)
reqMsg := &percutaneous.WsStreamRequest{
RawBody: []byte(jsonStr),
}
err = stream.SendMsg(reqMsg)
if err != nil {
logx.Errorf("BFF: auto send host_code msg failed, err=%v, json=%s", err, jsonStr)
return nil, err
}
return stream, nil
},
func() protoreflect.ProtoMessage {
return &percutaneous.WsStreamRequest{}
},
)
proxyHandler(w, r)
})
}