Ai
1 Star 0 Fork 1

啥大师/golang simple gather

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
client.go 1.30 KB
Copy Edit Raw Blame History
你çfrog authored 2023-04-17 23:04 +08:00 . feat websocket 服务端
package gsws
import (
"gitee.com/Sxiaobai/gs/gstool"
"github.com/gorilla/websocket"
"github.com/spf13/cast"
"net/url"
"time"
)
type GsWsClient struct {
Host string
Port string
Uri string
RecMsgChan chan string
CallFunc func(string) string
}
func (h *GsWsClient) Start() error {
h.RecMsgChan = make(chan string)
u := url.URL{Scheme: "ws", Host: h.Host + `:` + h.Port, Path: `/` + h.Uri}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return err
}
defer c.Close()
go func() {
defer close(h.RecMsgChan)
for {
_, message, err := c.ReadMessage()
if err != nil {
gstool.FmtPrintlnLog(`接收消息失败 %s`, err.Error())
return
}
h.RecMsgChan <- cast.ToString(message)
}
}()
//ticker := time.NewTicker(time.Second)
//defer ticker.Stop()
for {
select {
// if the goroutine is done , all are out
case msg := <-h.RecMsgChan: //正常消息处理
h.CallFunc(msg)
case t := <-time.Tick(time.Second): //主动发起心跳
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
if err != nil {
gstool.FmtPrintlnLog(`发送心跳失败 %s`, err.Error())
break
}
return nil
}
}
}
// SetCallFunc 设置回调
func (h *GsWsClient) SetCallFunc(callFunc func(string) string) {
h.CallFunc = callFunc
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/Sxiaobai/gs.git
git@gitee.com:Sxiaobai/gs.git
Sxiaobai
gs
golang simple gather
v1.9.9

Search