Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package websocket
  5. import (
  6. "encoding/json"
  7. "io"
  8. )
  9. // WriteJSON writes the JSON encoding of v as a message.
  10. //
  11. // Deprecated: Use c.WriteJSON instead.
  12. func WriteJSON(c *Conn, v interface{}) error {
  13. return c.WriteJSON(v)
  14. }
  15. // WriteJSON writes the JSON encoding of v as a message.
  16. //
  17. // See the documentation for encoding/json Marshal for details about the
  18. // conversion of Go values to JSON.
  19. func (c *Conn) WriteJSON(v interface{}) error {
  20. w, err := c.NextWriter(TextMessage)
  21. if err != nil {
  22. return err
  23. }
  24. err1 := json.NewEncoder(w).Encode(v)
  25. err2 := w.Close()
  26. if err1 != nil {
  27. return err1
  28. }
  29. return err2
  30. }
  31. // ReadJSON reads the next JSON-encoded message from the connection and stores
  32. // it in the value pointed to by v.
  33. //
  34. // Deprecated: Use c.ReadJSON instead.
  35. func ReadJSON(c *Conn, v interface{}) error {
  36. return c.ReadJSON(v)
  37. }
  38. // ReadJSON reads the next JSON-encoded message from the connection and stores
  39. // it in the value pointed to by v.
  40. //
  41. // See the documentation for the encoding/json Unmarshal function for details
  42. // about the conversion of JSON to a Go value.
  43. func (c *Conn) ReadJSON(v interface{}) error {
  44. _, r, err := c.NextReader()
  45. if err != nil {
  46. return err
  47. }
  48. err = json.NewDecoder(r).Decode(v)
  49. if err == io.EOF {
  50. // One value is expected in the message.
  51. err = io.ErrUnexpectedEOF
  52. }
  53. return err
  54. }