github => https://github.com/link1st/gowebsocket
文档: https://blog.csdn.net/m0_70556273/article/details/127306181
https://blog.csdn.net/u010750137/article/details/128439424
package main
import (
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"net/http"
)
//设置websocket
//CheckOrigin防止跨站点的请求伪造
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
//websocket实现
func ping(c *gin.Context) {
//升级get请求为webSocket协议
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
return
}
defer ws.Close() //返回前关闭
for {
//读取ws中的数据
mt, message, err := ws.ReadMessage()
if err != nil {
break
}
//写入ws数据
err = ws.WriteMessage(mt, message)
if err != nil {
break
}
}
}
func main() {
r := gin.Default()
r.GET("/ping", ping)
r.Run(":12345")
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net