前言
正文
如何将Gin框架的后端与WebSocket服务写在一起, 以达到共用一个端口的情况呢?
我们来看单纯使用 net 包和 WS 结合
package mainimport ("flag""fmt""log""net/http""github.com/gorilla/websocket")var addr = flag.String("addr", "localhost:8080", "http service address")var upgrader = websocket.Upgrader{// 解决跨域问题CheckOrigin: func(r *http.Request) bool {return true},} // use default optionsfunc ws(w http.ResponseWriter, r *http.Request) {c, err := upgrader.Upgrade(w, r, nil)if err != nil {log.Print("upgrade:", err)return}defer c.Close()for {mt, message, err := c.ReadMessage()if err != nil {log.Println("read:", err)break}log.Printf("recv: %s", message)err = c.WriteMessage(mt, message)if err != nil {log.Println("write:", err)break}}}func main() {flag.Parse()log.SetFlags(0)http.HandleFunc("/ws", ws)fmt.Println(*addr)log.Fatal(http.ListenAndServe(*addr, nil))}
其实本质来说, WS服务在握手的过程中最开始也是HTTP请求, 然后再 Upgrade 到WS模式的, Upgrade函数接受三个参数, 一个是返回值套接字, 一个是请求套接字, 一个是返回值的头, 而对于 Gin 的上下文 gin.Context 来说也是有这些数据的, 因此我们修改成
package controlimport ("log""net/http""github.com/gin-gonic/gin""github.com/gorilla/websocket")var upgrader = websocket.Upgrader{// 解决跨域问题CheckOrigin: func(r *http.Request) bool {return true},} // use default optionsfunc cmdWebSocket(c *gin.Context) {ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)if err != nil {log.Print("upgrade:", err)return}defer ws.Close()for {mt, message, err := ws.ReadMessage()if err != nil {log.Println("read:", err)break}log.Printf("recv: %s", message)err = ws.WriteMessage(mt, message)if err != nil {log.Println("write:", err)break}}}
即可
————————————————
作者:ChnMig
出处:https://www.cnblogs.com/chnmig/p/14463837.html
