ws
CI / changes (push) Successful in 1m31s
CI / ad (push) Successful in 32s
CI / admin (push) Successful in 22s
CI / bff (push) Successful in 19s
CI / chore (push) Successful in 18s
CI / equipment (push) Successful in 19s
CI / express (push) Successful in 18s
CI / factory (push) Successful in 17s
CI / message (push) Successful in 18s
CI / order (push) Successful in 17s
CI / payment (push) Successful in 17s
CI / percutaneous (push) Successful in 17s
CI / product (push) Successful in 17s
CI / record (push) Successful in 9m26s
CI / sale (push) Successful in 45s
CI / support (push) Successful in 3m8s
CI / task (push) Successful in 27s
CI / user (push) Successful in 21s
CI / wecom (push) Successful in 19s

This commit is contained in:
2026-10-08 16:37:33 +08:00
parent 6b8500ac5e
commit 0d8cfc695c
14 changed files with 272 additions and 393 deletions
+39 -51
View File
@@ -2,6 +2,7 @@ package wsClient
import (
"context"
"fmt"
"lone-services/bff/internal/request"
"lone-services/bff/internal/wsproxy"
percutaneous "lone-services/rpc/percutaneous/pb"
@@ -12,69 +13,56 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)
// ws handler
func PercutaneousLogin(rpcMgr *wsproxy.RpcClientManager) http.HandlerFunc {
wsHandler := request.UserReadMiddleware(func(w http.ResponseWriter, r *http.Request) {
cli, ready := rpcMgr.GetClient()
if !ready {
logx.Error("ws request: rpc client not ready, return 503")
http.Error(w, "rpc service unavailable", http.StatusServiceUnavailable)
return
}
proxyHandler := wsproxy.NewWsProxyHandler(
func(ctx context.Context) (grpc.ClientStream, error) {
streamCli := percutaneous.NewPercutaneousClient(cli.Conn())
return streamCli.WsLoginStream(ctx)
},
func() protoreflect.ProtoMessage { return &percutaneous.WsStreamRequest{} },
)
proxyHandler(w, r)
})
return wsHandler
}
func PercutaneousAddUser(rpcMgr *wsproxy.RpcClientManager) http.HandlerFunc {
wsHandler := request.UserReadMiddleware(func(w http.ResponseWriter, r *http.Request) {
return request.UserReadMiddleware(func(w http.ResponseWriter, r *http.Request) {
logx.Infof("BFF ws handler start, raw url=%s", r.URL.String())
cli, ready := rpcMgr.GetClient()
if !ready {
logx.Error("ws request: rpc client not ready, return 503")
logx.Error("BFF: rpc client not ready, return 503")
http.Error(w, "rpc service unavailable", http.StatusServiceUnavailable)
return
}
logx.Info("BFF: rpc client ready")
hostCode := r.URL.Query().Get("host_code")
logx.Infof("BFF: get host_code from query, val=%s", hostCode)
if hostCode == "" {
logx.Error("BFF: host_code is empty")
http.Error(w, "missing host_code query param", http.StatusBadRequest)
return
}
proxyHandler := wsproxy.NewWsProxyHandler(
func(ctx context.Context) (grpc.ClientStream, error) {
func(parentCtx context.Context) (grpc.ClientStream, error) {
logx.Info("BFF: enter grpc stream create callback")
streamCli := percutaneous.NewPercutaneousClient(cli.Conn())
return streamCli.WsAddUserStream(ctx)
stream, err := streamCli.WsAddUserStream(parentCtx)
if err != nil {
logx.Errorf("BFF: create WsAddUserStream failed, err=%v", err)
return nil, err
}
logx.Info("BFF: grpc stream create SUCCESS")
// =========核心新增:BFF自动发送第一条host_code消息给percutaneous=========
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
}
logx.Infof("BFF: auto send host_code message success, content=%s", jsonStr)
// =====================================================================
return stream, nil
},
func() protoreflect.ProtoMessage { return &percutaneous.WsStreamRequest{} },
)
proxyHandler(w, r)
})
return wsHandler
}
func PercutaneousUsed(rpcMgr *wsproxy.RpcClientManager) http.HandlerFunc {
wsHandler := request.UserReadMiddleware(func(w http.ResponseWriter, r *http.Request) {
cli, ready := rpcMgr.GetClient()
if !ready {
logx.Error("ws request: rpc client not ready, return 503")
http.Error(w, "rpc service unavailable", http.StatusServiceUnavailable)
return
}
proxyHandler := wsproxy.NewWsProxyHandler(
func(ctx context.Context) (grpc.ClientStream, error) {
streamCli := percutaneous.NewPercutaneousClient(cli.Conn())
return streamCli.WsUsedStream(ctx)
func() protoreflect.ProtoMessage {
return &percutaneous.WsStreamRequest{}
},
func() protoreflect.ProtoMessage { return &percutaneous.WsStreamRequest{} },
)
logx.Info("BFF: start run proxyHandler")
proxyHandler(w, r)
logx.Info("BFF: proxyHandler finished, ws stream closed")
})
return wsHandler
}