Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop
This commit is contained in:
		
						commit
						39d311c858
					
				| @ -9,9 +9,6 @@ const ( | ||||
| 	WEBSOCKET_CONNECT_SUCCESS Websocket = "WEBSOCKET_CONNECT_SUCCESS" //ws连接成功 (1级消息,单向通信) | ||||
| ) | ||||
| 
 | ||||
| // 心跳 | ||||
| const WEBSOCKET_HEARTBEAT Websocket = "WEBSOCKET_HEARTBEAT" | ||||
| 
 | ||||
| // websocket消息类型(通用通知类别) | ||||
| const ( | ||||
| 	WEBSOCKET_COMMON_NOTIFY Websocket = "WEBSOCKET_COMMON_NOTIFY" //通用回调通知(1级消息,单向通信) | ||||
|  | ||||
							
								
								
									
										35
									
								
								server/websocket/internal/handler/getstathandler.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								server/websocket/internal/handler/getstathandler.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| package handler | ||||
| 
 | ||||
| import ( | ||||
| 	"net/http" | ||||
| 	"reflect" | ||||
| 
 | ||||
| 	"fusenapi/utils/basic" | ||||
| 
 | ||||
| 	"fusenapi/server/websocket/internal/logic" | ||||
| 	"fusenapi/server/websocket/internal/svc" | ||||
| 	"fusenapi/server/websocket/internal/types" | ||||
| ) | ||||
| 
 | ||||
| func GetStatHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 
 | ||||
| 		var req types.GetStatReq | ||||
| 		userinfo, err := basic.RequestParse(w, r, svcCtx, &req) | ||||
| 		if err != nil { | ||||
| 			return | ||||
| 		} | ||||
| 
 | ||||
| 		// 创建一个业务逻辑层实例 | ||||
| 		l := logic.NewGetStatLogic(r.Context(), svcCtx) | ||||
| 
 | ||||
| 		rl := reflect.ValueOf(l) | ||||
| 		basic.BeforeLogic(w, r, rl) | ||||
| 
 | ||||
| 		resp := l.GetStat(&req, userinfo) | ||||
| 
 | ||||
| 		if !basic.AfterLogic(w, r, rl, resp) { | ||||
| 			basic.NormalAfterLogic(w, r, resp) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @ -32,6 +32,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | ||||
| 				Path:    "/api/websocket/close_websocket", | ||||
| 				Handler: CloseWebsocketHandler(serverCtx), | ||||
| 			}, | ||||
| 			{ | ||||
| 				Method:  http.MethodGet, | ||||
| 				Path:    "/api/websocket/get_stat", | ||||
| 				Handler: GetStatHandler(serverCtx), | ||||
| 			}, | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| @ -213,7 +213,7 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use | ||||
| 			} | ||||
| 			//存在是不能给他申请重新绑定 | ||||
| 			if _, ok := mapConnPool.Load(oldWid); ok { | ||||
| 				logx.Error("复用的连接标识已被其他客户端使用,不符合重用条件") | ||||
| 				logx.Error("复用的连接标识已被其他客户端使用,不符合重用条件,用户id:", userInfo.UserId, "  guest_id:", userInfo.GuestId) | ||||
| 				break | ||||
| 			} | ||||
| 			logx.Info("====复用旧的ws连接成功====") | ||||
| @ -321,15 +321,7 @@ func (w *wsConnectItem) heartbeat() { | ||||
| 			if w.debug != nil && w.debug.Exp != nil && *w.debug.Exp < time.Now().UTC().Unix() { | ||||
| 				w.debug = nil | ||||
| 			} | ||||
| 			//发送心跳信息 | ||||
| 			var d interface{} | ||||
| 			if w.debug != nil { | ||||
| 				d = websocket_data.HeartBeatMsg{ | ||||
| 					WsCount:      currentWebsocketConnectCount, | ||||
| 					CombineCount: currentRequestCombineApiCount, | ||||
| 				} | ||||
| 			} | ||||
| 			if err := w.conn.WriteMessage(websocket.PongMessage, w.respondDataFormat(constants.WEBSOCKET_HEARTBEAT, d)); err != nil { | ||||
| 			if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil { | ||||
| 				logx.Error("发送心跳信息异常,关闭连接:", w.uniqueId, err) | ||||
| 				w.close() | ||||
| 				return | ||||
|  | ||||
							
								
								
									
										45
									
								
								server/websocket/internal/logic/getstatlogic.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								server/websocket/internal/logic/getstatlogic.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,45 @@ | ||||
| package logic | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fusenapi/utils/auth" | ||||
| 	"fusenapi/utils/basic" | ||||
| 
 | ||||
| 	"fusenapi/server/websocket/internal/svc" | ||||
| 	"fusenapi/server/websocket/internal/types" | ||||
| 
 | ||||
| 	"github.com/zeromicro/go-zero/core/logx" | ||||
| ) | ||||
| 
 | ||||
| type GetStatLogic struct { | ||||
| 	logx.Logger | ||||
| 	ctx    context.Context | ||||
| 	svcCtx *svc.ServiceContext | ||||
| } | ||||
| 
 | ||||
| func NewGetStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStatLogic { | ||||
| 	return &GetStatLogic{ | ||||
| 		Logger: logx.WithContext(ctx), | ||||
| 		ctx:    ctx, | ||||
| 		svcCtx: svcCtx, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // 处理进入前逻辑w,r | ||||
| // func (l *GetStatLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { | ||||
| // } | ||||
| 
 | ||||
| func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (resp *basic.Response) { | ||||
| 	if req.Password != "fusen1314" { | ||||
| 		return resp.SetStatusWithMessage(basic.CodeOK, "你干嘛,哎哟") | ||||
| 	} | ||||
| 	return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{ | ||||
| 		WsTotalCount:           currentWebsocketConnectCount, | ||||
| 		CurRequestCombineCount: currentRequestCombineApiCount, | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 | ||||
| // func (l *GetStatLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { | ||||
| // // httpx.OkJsonCtx(r.Context(), w, resp) | ||||
| // } | ||||
| @ -29,6 +29,15 @@ type CloseWebsocketReq struct { | ||||
| 	Wid string `json:"wid"` | ||||
| } | ||||
| 
 | ||||
| type GetStatReq struct { | ||||
| 	Password string `form:"password"` | ||||
| } | ||||
| 
 | ||||
| type GetStatRsp struct { | ||||
| 	WsTotalCount           int `json:"ws_total_count"` //ws连接数 | ||||
| 	CurRequestCombineCount int `json:"cur_request_combine_count"` | ||||
| } | ||||
| 
 | ||||
| type Request struct { | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -21,6 +21,9 @@ service websocket { | ||||
| 	//关闭某个连接 | ||||
| 	@handler CloseWebsocketHandler | ||||
| 	post /api/websocket/close_websocket(CloseWebsocketReq) returns (response); | ||||
| 	//获取ws统计信息 | ||||
| 	@handler GetStatHandler | ||||
| 	get /api/websocket/get_stat(GetStatReq) returns (response); | ||||
| } | ||||
| 
 | ||||
| //websocket数据交互[ | ||||
| @ -46,4 +49,12 @@ type CommonNotifyReq { | ||||
| //关闭连接 | ||||
| type CloseWebsocketReq { | ||||
| 	Wid string `json:"wid"` | ||||
| } | ||||
| //获取ws统计信息 | ||||
| type GetStatReq { | ||||
| 	Password string `form:"password"` | ||||
| } | ||||
| type GetStatRsp { | ||||
| 	WsTotalCount           int `json:"ws_total_count"` //ws连接数 | ||||
| 	CurRequestCombineCount int `json:"cur_request_combine_count"` | ||||
| } | ||||
| @ -23,9 +23,3 @@ type ConnectErrMsg struct { | ||||
| type ConnectUnAuth struct { | ||||
| 	Message string `json:"message"` | ||||
| } | ||||
| 
 | ||||
| // 心跳数据 | ||||
| type HeartBeatMsg struct { | ||||
| 	WsCount      int `json:"ws_count"` | ||||
| 	CombineCount int `json:"combine_count"` | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user