drake900918
2015-07-21 07:18
浏览 742
已采纳

GO Websocket向所有客户发送消息

Everything works fine with this code (shortened it for better reading).

When Client1 sends a request to the Server, the Server responses to him instantly. But, the other clients can not see the response message.

So I want to make it go further: When a client sends a request to the server, the server will response to all clients so that all clients can see the message.

How can I do that? Any examples or nice tutorials for beginners?

Thanks in advance!

Server:

import (
        "github.com/gorilla/websocket"
       )

func main() {
    http.Handle("/server", websocket.Handler(echoHandler)) 
}

func echoHandler(ws *websocket.Conn) {
    conn, err := upgrader.Upgrade(w, r, nil) 
    if err != nil { 
      return
    }
    for {
      messageType, p, err := conn.ReadMessage() 
      if err != nil {
        return
      }

      print_binary(p) // simple print of the message

      err = conn.WriteMessage(messageType, p);
      if err != nil {
        return
      }
    }
}

图片转代码服务由CSDN问答提供 功能建议

此代码的所有功能都可以正常工作(为了便于阅读而将其简化)。

Client1 向服务器发送请求时,服务器会立即对其进行响应。 但是,其他客户端看不到响应消息。

因此,我想进一步说明:当一个客户端向服务器发送请求时,服务器将响应所有客户端,因此 所有客户都可以看到该消息。

我该怎么做?

是否有任何示例或不错的入门教程?

服务器:

  导入(
“ github.com/gorilla/websocket"
)
 
func main(){
 http.Handle(” / server“,websocket.Handler(echoHandler))
} 
 
func echoHandler  (ws * websocket.Conn){
 conn,err:= upgrader.Upgrade(w,r,nil)
 if err!= nil {
 return 
} 
 for {
 messageType,p,err  := conn.ReadMessage()
 if err!= nil {
 return 
} 
 
 print_binary(p)//消息的简单打印
 
 err = conn.WriteMessage(messageType,p)  ; 
 if err!= nil {
 return 
} 
} 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyao5843 2015-07-21 07:22
    已采纳

    You have to use connection pool to broadcast messages to all connections. You can use that as tutorial/sample http://gary.burd.info/go-websocket-chat

    Simplifying:
    Connection pool is a collection of registered connections. See hub.connections:

    type connection struct {
        // The websocket connection.
        ws *websocket.Conn
    
        // Buffered channel of outbound messages.
        send chan []byte
    
        // The hub.
        h *hub
    }
    
    type hub struct {
        // Registered connections. That's a connection pool
        connections map[*connection]bool
    
        ...
    }
    

    To broadcast message for all clients, we iterate over connection pool like this:

        case m := <-h.broadcast:
            for c := range h.connections {
                select {
                case c.send <- m:
                default:
                    delete(h.connections, c)
                    close(c.send)
                }
            }
        }
    

    h.broadcast in that example is a channel with messages we need to broadcast.
    We use default section of the select statement to delete connections with full or blocked send channels. See What is the benefit of sending to a channel by using select in Go?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题