websocketgolang客户端服务端搭建实例

更新时间:2023-06-02 21:19:16 阅读: 评论: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()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)
}

本文发布于:2023-06-02 21:19:16,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/131813.html

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

标签:服务端   客户端   轻蔑
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图