这是一个分享于 的项目,其中的信息可能已经有所发展或是发生改变。
Melody 是一个 Go 语言的微型 WebSocket 框架,基于 [github.com/gorilla/websocket](https://github.com/gorilla/websocket) 开发,主要特性:
*
接口简单易用,类似 net/http 或者 [Gin](http://www.oschina.net/p/gin-web-framework)
*
提供给所有广播以及给选择连接会话广播的简单途径
*
消息缓冲对并发写是安全的
*
可自动处理 ping/pong 和会话超时
一个简单的实例:
[](https://github.com/olahol/melody/tree/master/examples/multichat)
代码:
<pre class="brush:cpp ;toolbar: true; auto-links: false;">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")
}</pre>
10915 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传