1 回答
TA贡献1851条经验 获得超5个赞
您确实需要遵循连接池的相同原则。
这是 Echo 示例,其中包含一个非常基本的池实现:
package main
import (
"fmt"
"sync"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"golang.org/x/net/websocket"
)
var connectionPool = struct {
sync.RWMutex
connections map[*websocket.Conn]struct{}
}{
connections: make(map[*websocket.Conn]struct{}),
}
func main() {
e := echo.New()
e.Use(mw.Logger())
e.Use(mw.Recover())
e.Static("/", "public")
e.WebSocket("/ws", func(c *echo.Context) (err error) {
ws := c.Socket()
connectionPool.Lock()
connectionPool.connections[ws] = struct{}{}
defer func(connection *websocket.Conn){
connectionPool.Lock()
delete(connectionPool.connections, connection)
connectionPool.Unlock()
}(ws)
connectionPool.Unlock()
msg := ""
for {
if err = websocket.Message.Receive(ws, &msg); err != nil {
return err
}
err = sendMessageToAllPool(msg)
if err != nil {
return err
}
fmt.Println(msg)
}
return err
})
e.Run(":1323")
}
func sendMessageToAllPool(message string) error {
connectionPool.RLock()
defer connectionPool.RUnlock()
for connection := range connectionPool.connections {
if err := websocket.Message.Send(connection, message); err != nil {
return err
}
}
return nil
}
- 1 回答
- 0 关注
- 303 浏览
添加回答
举报