websocketgolang客户端服务端搭建实例客户端服务端通信:
ver:
package main
import (
"fmt"
"/gin-gonic/gin"
"/gorilla/websocket"
"net/http"
)
var upgrader = websocket.Upgrader{ //define upgrade(http-->websocket)
ReadBufferSize: 1024,
轻蔑的意思WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
英语四级准考证号找回
}
func ping (c *gin.Context){
ws ,err := upgrader.Upgrade(c.Writer,c.Request,nil) //upgrade
if err != nil{
return
}
defer ws.Clo()bbc听力
for { //most important nd receive function always
mt,message ,err := ws.ReadMessage()
fmt.Println(string(message))
mp3小说下载
if err != nil{
break
}
err = ws.WriteMessage(mt,message)
if err != nil{
break
}
}
}
func main (){
r := gin.Default()
r.GET("/ping",ping)
r.Run(":8383")
}
client:
package main
import (
"/gorilla/websocket"
投资收益是什么科目
"log"
)
d ofunc main (){
client, _, err := websocket.DefaultDialer.Dial("ws://127.0.0.1:8383/ping",nil)
if err != nil {
服装店运营
ray charleslog.Fatal("dial: ",err)
}hrd
defer client.Clo()
err = client.WriteMessage(websocket.TextMessage,[]byte("hello websoccket")) if err != nil {
log.Println(err)
}
_,msg,err := client.ReadMessage()
if err != nil {
log.Println("read:",err)
追女孩子的方法
return
}
log.Printf("receive :%s\n",msg)
}