dpw5865 2015-10-16 04:41
浏览 170
已采纳

Websocket在Echo Framework中向所有客户端发送消息

I believe that this question is almost the same with this.

But I use websocket in Echo framework instead of Gorilla. So I think the approach will be different.

Echo does provide the example. But it only shows how to connect with single client. When there are more than one client, the other clients don't receive message from server.

How do I make the server broadcasts message to all connected clients?

The accepted answer from referred link says that I have to use connection pool to broadcast messages to all connections. How can I do this in Echo framework?

  • 写回答

1条回答 默认 最新

  • douyingbei1458 2015-10-16 06:26
    关注

    You indeed need to follow the same principle of connection pool.

    Here is the Echo example, with a very basic implementation of a pool:

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决