github.com/gogf/gf@v1.16.9/.example/net/ghttp/server/websocket/echo/main-group.go (about) 1 package main 2 3 import ( 4 "github.com/gogf/gf/frame/g" 5 "github.com/gogf/gf/net/ghttp" 6 "github.com/gogf/gf/os/gfile" 7 "github.com/gogf/gf/os/glog" 8 ) 9 10 func ws(r *ghttp.Request) { 11 ws, err := r.WebSocket() 12 if err != nil { 13 glog.Error(err) 14 return 15 } 16 for { 17 msgType, msg, err := ws.ReadMessage() 18 if err != nil { 19 return 20 } 21 if err = ws.WriteMessage(msgType, msg); err != nil { 22 return 23 } 24 } 25 } 26 27 func main() { 28 s := g.Server() 29 s.Group("").Bind([]ghttp.GroupItem{ 30 {"ALL", "/ws", ws}, 31 }) 32 s.SetAccessLogEnabled(true) 33 s.SetServerRoot(gfile.MainPkgPath()) 34 s.SetPort(8199) 35 s.Run() 36 }