websocketgolang客户端服务端搭建实例

更新时间:2023-05-24 23:20:15 阅读: 评论:0

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()
for {            //most important nd receive function always
mt,message ,err := ws.ReadMessage()
fmt.Println(string(message))
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 mainip地址配置
import (
"/gorilla/websocket"
"log"
)
func main (){
client, _, err := websocket.DefaultDialer.Dial("ws://127.0.0.1:8383/ping",nil)
if err != nil {
log.Fatal("dial: ",err)
}
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)
}

本文发布于:2023-05-24 23:20:15,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/927339.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:服务端   客户端   风吹   失灵   故事   驱驰   食物
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图