1 Star 0 Fork 0

zhonglin.liu / dao_socket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.go 964 Bytes
一键复制 编辑 原始数据 按行查看 历史
zhonglin.liu 提交于 2022-05-31 00:32 . cache
package main
import (
"fmt"
socket "gitee.com/zhonglin-liu/dao_socket/websocket"
"github.com/go-redis/redis"
"net/http"
"strconv"
"time"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
hub := socket.NewHub(
socket.WithLogin(func(w http.ResponseWriter, r *http.Request) (uid int64, err error) {
token := r.Header.Get("token")
fmt.Println(token)
uid, err = strconv.ParseInt(token, 10, 64)
return
}),
socket.WithReceive(func(uid int64, msg []byte) {
// your logic
fmt.Println("WithDispatch", string(msg), uid)
uid = 456
socket.GetHub().Send2User(uid, time.Now().Unix(), msg)
}),
// 发送失败的消息持久化,若不传此参数,消息不会持久化
socket.WithRedis(1000, 3600, client),
)
go hub.Run()
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
socket.ServeWs(hub, w, r)
})
fmt.Println("start :8080")
http.ListenAndServe(":8080", nil)
}
Go
1
https://gitee.com/zhonglin-liu/dao_socket.git
git@gitee.com:zhonglin-liu/dao_socket.git
zhonglin-liu
dao_socket
dao_socket
4f29f3d82524

搜索帮助

53164aa7 5694891 3bd8fe86 5694891