1
0

client.go 699 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. // "sync"
  6. "github.com/gorilla/websocket"
  7. )
  8. type Client struct {
  9. ID string
  10. Conn *websocket.Conn
  11. Pool *Pool
  12. }
  13. type Message struct {
  14. Type int `json:"type"`
  15. Body string `json:"body"`
  16. }
  17. func (c *Client) Read() {
  18. defer func() {
  19. c.Pool.Unregister <- c
  20. c.Conn.Close()
  21. }()
  22. for {
  23. messageType, p, err := c.Conn.ReadMessage()
  24. if err != nil {
  25. log.Printf("Error from Read(): %v\n", err)
  26. return
  27. }
  28. message := Message{Type: messageType, Body: string(p)}
  29. c.Pool.Broadcast <- message
  30. fmt.Printf("Message Received: %+v\n", message)
  31. }
  32. }