K
K
Kagtaviy2017-02-26 20:06:20
go
Kagtaviy, 2017-02-26 20:06:20

How to properly work with gorilla/websocket?

Hello, please help me deal with gorilla/websocket.
I don't understand how to work with him.
Here's what we have:
Create a hub:

type Hub struct {
  clients map[*Client]bool
  broadcast chan []byte
  register chan *Client
  unregister chan *Client
}

func NewHub() *Hub {
  return &Hub{
    broadcast:  make(chan []byte),
    register:   make(chan *Client),
    unregister: make(chan *Client),
    clients:    make(map[*Client]bool),
  }
}

func Run(h *Hub) {
  for {
    select {
    case client := <-h.register:
      h.clients[client] = true
    case client := <-h.unregister:
      if _, ok := h.clients[client]; ok {
        delete(h.clients, client)
        close(client.send)
      }
      
    }
  }
}

In main.go:
hub := ws.NewHub()
go ws.Run(hub)
....
r.HandleFunc("/socket", func(w http.ResponseWriter, r *http.Request) {
    ws.ServeWs(hub, w, r)
  })

client:
const (
  writeWait = 10 * time.Second
  pongWait = 60 * time.Second
  pingPeriod     = (pongWait * 9) / 10
  maxMessageSize = 512
)

var upgrader = websocket.Upgrader{
  ReadBufferSize:  1024,
  WriteBufferSize: 1024,
}

type Client struct {
  hub *Hub
  conn *websocket.Conn
  send chan []byte
}

func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
  conn, err := upgrader.Upgrade(w, r, nil)
  if err != nil {
    log.Println(err)
    return
  }
  client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
  client.hub.register <- client
}

Here, too, everything is clear, but what's next?
As I understand it, I need to play "ping / pong", but I don’t understand at all how can I do this?
I also do not understand how I can correctly track messages from the client on the server side and respond to it?
Please tell me :3

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey K, 2017-02-27
@mututunus

You can look at https://github.com/onrik/wshub , this is a wrapper over gorilla/websocket

A
Alexander Pavlyuk, 2017-02-27
@pav5000

In the ServeWs function, you register the client and immediately end the connection with it by exiting this function.
It needs to be different. Inside ServeWs, register a client and read his messages in a loop and send your own to him.
As a rough approximation, something like this:

func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
  conn, err := wsupgrader.Upgrade(w, r, nil)
  if err != nil {
    log.Println(err)
    return
  }
  client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
  client.hub.register <- client

  for {
    var msg MsgType
    err := conn.ReadJSON(&msg)
    if err != nil {
      client.hub.unregister <- client
      return
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question