You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
832 B

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package ws
  2. import (
  3. "encoding/json"
  4. "gopkg.in/olahol/melody.v1"
  5. )
  6. type Client struct {
  7. Id string // account_typ:account_id
  8. AccountId int64
  9. AccountType string
  10. Pid int64
  11. Online bool
  12. RoomId string
  13. node *Node
  14. *melody.Session
  15. }
  16. func NewClient(session *melody.Session, node *Node, roomId string) *Client {
  17. client := new(Client)
  18. client.Session = session
  19. client.node = node
  20. client.Online = false
  21. client.RoomId = roomId
  22. return client
  23. }
  24. func (c *Client) BroadcastAll(msg *Message) {
  25. c.node.BroadcastAll(msg, c)
  26. }
  27. func (c *Client) Register() error {
  28. return c.node.RegisterClient(c)
  29. }
  30. func (c *Client) Send(msg *Message) {
  31. c.node.Send(msg)
  32. }
  33. func (c *Client) WriteJson(body interface{}) error {
  34. bs, err := json.Marshal(&body)
  35. if err != nil {
  36. return err
  37. }
  38. return c.Write(bs)
  39. }