Melody 介绍
Melody 是一个 Go 语言的微型 WebSocket 框架,基于
github.com/gorilla/websocket 开发,主要特性:
-
接口简单易用,类似 net/http 或者 Gin
-
提供给所有广播以及给选择连接会话广播的简单途径
-
消息缓冲对并发写是安全的
-
可自动处理 ping/pong 和会话超时
一个简单的实例:
代码:
package main import ( "github.com/olahol/melody" "github.com/gin-gonic/gin" "net/http" ) func main() { r := gin.Default() m := melody.New() r.GET("/", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "index.html") }) r.GET("/channel/:name", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "chan.html") }) r.GET("/channel/:name/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) m.HandleMessage(func(s *melody.Session, msg []byte) { m.BroadcastFilter(msg, func(q *melody.Session) bool { return q.Request.URL.Path == s.Request.URL.Path }) }) r.Run(":5000") }
Melody 官网
https://github.com/olahol/melody
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。