conn.go 849 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package wslog
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/gorilla/websocket"
  6. )
  7. // TODO(cryptix): could be extended to be a leveld so that clients control which events they get
  8. type websocketConn struct {
  9. ws *websocket.Conn // the websocket connection
  10. send chan *formattedRec // buffered channel of outbound messages.
  11. }
  12. // get messages from clients
  13. func (c *websocketConn) reader() {
  14. for {
  15. _, message, err := c.ws.ReadMessage()
  16. if err != nil {
  17. fmt.Fprintf(os.Stderr, "ws.ReadMessage error: %v\n", err)
  18. break
  19. }
  20. fmt.Fprintf(os.Stderr, "From Client: %q\n", string(message))
  21. }
  22. c.ws.Close()
  23. }
  24. // push down record to listening clients
  25. func (c *websocketConn) writer() {
  26. for rec := range c.send {
  27. err := c.ws.WriteJSON(rec)
  28. if err != nil {
  29. fmt.Fprintf(os.Stderr, "ws.WriteJSON error: %v\n", err)
  30. break
  31. }
  32. }
  33. c.ws.Close()
  34. }