Golang Websocket 自定义 JSON 消息

huangapple go评论179阅读模式
英文:

Golang Websocket Custom JSON Messages

问题

我正在尝试发送/接收自定义的JSON消息。有三种情况下JSON结构会发生变化,因此我有三个不同的结构体。我必须访问作为RawMessage发送的room字符串。我的问题是channel Broadcast应该是什么类型?

  1. type Message struct {
  2. Type int64 `json:"type"`
  3. Msg json.RawMessage
  4. }
  1. case m := <-r.Broadcast:
  2. // chan Broadcast应该是什么类型?
  3. // 如果m的类型是json.RawMessage,我应该在这里进行解组吗?
  4. connections := r.Clients[m.Room]
  5. for c := range connections {
  6. select {
  7. case c.send <- m:
  8. default:
  9. close(c.send)
  10. delete(connections, c)
  11. if len(connections) == 0 {
  12. delete(r.Clients, m.Room)
  13. }
  14. }
  15. }
  1. for {
  2. msg := &Message{}
  3. err := c.conn.ReadJSON(&msg)
  4. // _, msg, err := c.conn.ReadMessage()
  5. if err != nil {
  6. if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
  7. log.Printf("error: %v", err)
  8. }
  9. break
  10. }
  11. if msg.Type == 0 {
  12. newVideo := &NewVideo{}
  13. if err = json.Unmarshal(msg.Msg, &newVideo); err != nil {
  14. fmt.Println(err)
  15. }
  16. Roomb.Broadcast <- msg.Msg // ??? 我应该发送RawMessage吗?
  17. online[msg.Room] = msg
  18. } else if msg.Type == 1 {
  19. if _, ok := online[msg.Room]; ok {
  20. online[msg.Room].Start = float64(time.Now().Unix() - online[msg.Room].Timestamp)
  21. c.send <- online[msg.Room]
  22. }
  23. } else if msg.Type == 2 {
  24. Roomb.Broadcast <- msg.Msg // ??? 我应该发送RawMessage吗?
  25. }
  26. fmt.Println(msg)
  27. }
英文:

I'm trying to send/receive custom JSON messages. There are 3 cases in which the JSON struct changes, therefore I have 3 different structs. I have to access the room string which is sent as RawMessage. My question is what type should the channel Broadcast be?

  1. type Message struct {
  2. Type int64 `json:&quot;type&quot;`
  3. Msg json.RawMessage
  4. }

Broadcast chan interface{} // ??? RawMessage or maybe interface

  1. case m := &lt;-r.Broadcast:
  2. // What type should chan Broadcast be?
  3. // If m is of type json.RawMessage should I deal with unmarshalling here?
  4. connections := r.Clients[m.Room] //
  5. for c := range connections {
  6. select {
  7. case c.send &lt;- m:
  8. default:
  9. close(c.send)
  10. delete(connections, c)
  11. if len(connections) == 0 {
  12. delete(r.Clients, m.Room)
  13. }
  14. }
  15. }
  1. for {
  2. msg := &amp;Message{}
  3. err := c.conn.ReadJSON(&amp;msg)
  4. // _, msg, err := c.conn.ReadMessage()
  5. if err != nil {
  6. if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
  7. log.Printf(&quot;error: %v&quot;, err)
  8. }
  9. break
  10. }
  11. if msg.Type == 0 {
  12. newVideo := &amp;NewVideo{}
  13. if err = json.Unmarshal(msg.Msg, &amp;newVideo); err != nil {
  14. fmt.Println(err)
  15. }
  16. Roomb.Broadcast &lt;- msg.Msg // ??? should i send the RawMessage
  17. online[msg.Room] = msg
  18. } else if msg.Type == 1 {
  19. if _, ok := online[msg.Room]; ok {
  20. online[msg.Room].Start = float64(time.Now().Unix() - online[msg.Room].Timestamp)
  21. c.send &lt;- online[msg.Room]
  22. }
  23. } else if msg.Type == 2 {
  24. Roomb.Broadcast &lt;- msg.Msg // ??? should i send the RawMessage
  25. }
  26. fmt.Println(msg)
  27. }

答案1

得分: 0

  1. msg之前删除"&"。msg已经是一个指针。这可能会引起问题。

  2. 你的问题不是很清楚。如果msg.Type定义了msg.Msg的类型,那么我建议你使用实际的消息类型来定义通道,解析msg.Msg,然后通过通道发送它。

英文:
  1. Remove the "&" before msg. msg is already a pointer. This may make problems.

  2. Your question is not very clear. If msg.Type defines what the msg.Msg is, then I recommend you make channels typed with the actual message type, parse the msg.Msg, then send it through the channel.

huangapple
  • 本文由 发表于 2019年6月11日 10:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/56535748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定