1234567891011121314151617181920212223242526272829303132333435363738 |
- package main
- import (
- "fmt"
- "log"
- // "sync"
- "github.com/gorilla/websocket"
- )
- type Client struct {
- ID string
- Conn *websocket.Conn
- Pool *Pool
- }
- type Message struct {
- Type int `json:"type"`
- Body string `json:"body"`
- }
- func (c *Client) Read() {
- defer func() {
- c.Pool.Unregister <- c
- c.Conn.Close()
- }()
- for {
- messageType, p, err := c.Conn.ReadMessage()
- if err != nil {
- log.Printf("Error from Read(): %v\n", err)
- return
- }
- message := Message{Type: messageType, Body: string(p)}
- c.Pool.Broadcast <- message
- fmt.Printf("Message Received: %+v\n", message)
- }
- }
|