@@ -16,6 +16,12 @@ const (
1616 FnTeardownRequest = "TeardownRequest"
1717)
1818
19+ // websocket 定义函数名
20+ const (
21+ FnWebSocketCommunicate = "Communicate"
22+ FnWebSocketBaseHandler = "WSBaseHandler"
23+ )
24+
1925// UrlPatternHandle 是URL路由句柄,用来驱动url路由以及其映射的handler
2026type UrlPatternHandle struct {
2127 Handler reflect.Type
@@ -73,6 +79,17 @@ func (urlPattern *UrlPattern) AppendRouterPattern(pattern Pattern, v interface {
7379 fileRouter .PathPrefix ("/" ).Handler (http .StripPrefix (pattern .Url , fileServer ))
7480 return
7581 }
82+ // 判断是否是Websocket
83+ if _ , isWSHandler := v .(* WSPatternHandle ); isWSHandler {
84+ var wsMiddleware []middleware
85+ for _ , mv := range pattern .Middleware {
86+ m := convertHandleFuncMV (mv )
87+ wsMiddleware = append (wsMiddleware , m )
88+ }
89+ wsMiddlewares := chainMiddleware (wsMiddleware ... )
90+ urlPattern .router .HandleFunc (pattern .Url , wsMiddlewares (v .Handle ))
91+ return
92+ }
7693 // 判断是否是handler
7794 baseMiddleware := []middleware {HttpContextLogMiddleware , InternalServerErrorMiddleware }
7895 for _ , mv := range pattern .Middleware {
@@ -90,6 +107,16 @@ func (urlPattern *UrlPattern) Init() {
90107 if handlerType .Kind () == reflect .Ptr {
91108 handlerType = handlerType .Elem ()
92109 }
110+ // 判断是否是 WSHandlerInterface 接口类型
111+ // 判断 handlerType 是否包含成员变量 WSBaseHandler
112+ _ , isWSHandler := handlerType .FieldByName (FnWebSocketBaseHandler )
113+ if isWSHandler {
114+ urlPattern .AppendRouterPattern (pattern , & WSPatternHandle {
115+ Handler : handlerType ,
116+ requestUrl : pattern .Url ,
117+ })
118+ continue
119+ }
93120 urlPattern .AppendRouterPattern (pattern , & UrlPatternHandle {
94121 Handler : handlerType ,
95122 requestUrl : pattern .Url ,
0 commit comments