go websockets with gorilla libs - 未知 json 响应所以无法映射它

go websockets with gorilla libs - unknown json response so can't map it

我是一个新手,我正在尝试连接到一个使用 websocket 做很多事情的网站,包括聊天。 我的目标是只获取聊天输出并对其进行处理,而忽略其余部分。

我知道 json 我期待的是聊天,但不是其他的,所以当我 运行 我现在得到所有 websocket 输出的程序时,但我只对以下内容感兴趣:

[null,null,"channel:zRAMDON","message",{"session_id":"ramdon-d655-4ee1-ramdon","body":"TEST!!!!!!!!!!!!!!!!!!!","type":"chat"}]

现在我用它来捕捉所有

var readCHAT []interface{}

go func() {
        for {
            
            err := ws.ReadJSON(&readCHAT)

            if err != nil {
                log.Printf("Error read: %s", err)
                break
            }
            log.Printf("Message received: %+v", readCHAT)
        }
    }()

我遇到的问题是我得到了不同的 websocket 信息,所以不能只写一个结构。

我得到的一个 websocket 示例 我不需要 我想忽略的其他许多人:

[<nil> <nil> channel:zRAMDON naf map[data:map[components:map[0:map[x:18.37785615655833 y:3.849238250917483 z:31.306368728428566] 1:map[x:-2.1127290916182298e-13 y:-14.000000000000544 z:1.6920595533305515e-13] 10:map[x:0 y:0 z:0] 11:map[x:0 y:0 z:0] 12:false 2:map[x:1.0000000000000018 y:1.0000000000000713 z:0.999999999999999] 3:map[avatarSrc:https://somewhere.com/api/v1/avatars/PcJ8Sxb/avatar.gltf?v=63726570330 avatarType:skinnable muted:true] 4:map[left_hand_pose:0 right_hand_pose:0] 5:map[x:0 y:1.6 z:0] 6:map[x:5.21773538958394 y:41.12499999998135 z:-3.292556714059138e-11] 7:map[x:0 y:0 z:0] 8:map[x:0 y:0 z:0] 9:false] creator:f6ee84ab-15af-4bc0-b9df-aa9c71093118 isFirstSync:true lastOwnerTime:1.5933988881835e+12 networkId:m9ms3qv owner:f6ee84ab-15af-4bc0-b9df-aa9c71093118 parent:<nil> persistent:false template:#remote-avatar] dataType:u from_session_id:f6ee84ab-15af-4bc0-b9df-aa9c71093118]]

如果我不能将它转换为结构以使用 map[] 等,当我不确定我得到的是什么时,我如何只使用我需要的 json。

理想:如果我能以某种方式动态搜索界面,并且如果 json 是我期望的那个,那么将它放入结构中的对象...或类似的..

谢谢

[编辑]

我需要忽略的差异示例:

[<nil> <nil> channel:zRAMDON naf map[data:map[components:map[0:map[x:4.001814822517539 y:2.644030847036413 z:30.97952900553247] 1:map[x:2.4521572390556376e-13 y:-14.000000000000105 z:8.633353546977925e-15] 10:map[x:0 y:0 z:0] 11:map[x:0 y:0 z:0] 12:false 2:map[x:1 y:1.0000000000000715 z:1.0000000000000009] 3:map[avatarSrc:https://somesite.com/api/v1/avatars/3IADk9x/avatar.gltf?v=63743924756 avatarType:skinnable muted:true] 4:map[left_hand_pose:0 right_hand_pose:0] 5:map[x:0 y:1.6 z:0] 6:map[x:-7.540983606551728 y:111.09374999994142 z:-4.904411738530524e-11] 7:map[x:0 y:0 z:0] 8:map[x:0 y:0 z:0] 9:false] creator:de96a126-cf5f-48b2-8c0c-02d79b4382d5 isFirstSync:true lastOwnerTime:1.5934383934451e+12 networkId:5jwrz57 owner:de96a126-cf5f-48b2-8c0c-02d79b4382d5 parent:<nil> persistent:false template:#remote-avatar] dataType:u from_session_id:de96a126-cf5f-48b2-8c0c-02d79b4382d5]]

另一个:

[<nil>  phoenix phx_reply map[response:map[] status:ok]]

另一个这个是我得到的最多的,但我不需要,所以需要过滤掉所有的:

[<nil> <nil> channel:zRAMDON naf map[data:map[components:map[0:map[x:4.001814822517539 y:2.644030847036413 z:30.97952900553247] 1:map[x:2.4521572390556376e-13 y:-14.000000000000105 z:8.633353546977925e-15] 10:map[x:0 y:0 z:0] 11:map[x:0 y:0 z:0] 12:false 2:map[x:1 y:1.0000000000000715 z:1.0000000000000009] 3:map[avatarSrc:https://somesite.com/api/v1/avatars/3IADk9x/avatar.gltf?v=63743924756 avatarType:skinnable muted:true] 4:map[left_hand_pose:0

那些 AND LIKE 100 其他甚至更不同甚至不相同的模式,如果它们具有相同的模式但内容不同那么很容易编写结构并了解 json 文档。

这是我真正需要的:

[null,null,"channel:zRAMDON","message",{"session_id":"ramdon-d655-4ee1-ramdon","body":"TEST!!!!!!!!!!!!!!!!!!!","type":"chat"}]

所以我需要以某种方式 filter/get 摆脱然后从这个响应中我也许可以编写一个结构来放入 null、null、通道、消息等

感谢您的帮助。这让我发疯

编辑2:

好的,我已经打开 chrome 开发工具的 websocket 网络选项卡: 我注意到总是有一个包含 4 个元素的数组,然后最后打开一个 json。但是这个 json 总是不同的,到目前为止的解密是模糊的:

["2", "238", "hub:zWXK8U6", "nafr", {,…}]
0: "2"
1: "238"
2: "room:zRAMDON"
3: "nafr"
4: {,…}
naf: "{"dataType":"um","data":{"d":[{"networkId":"A8AFC029-A3AF-459B-AC89-83557814DEAC","owner":"fa477e9d-7e92-4c1d-9b7b-6e9e0e975495","creator":"","lastOwnerTime":1593446129608,"template":"#static-controlled-media","persistent":true,"parent":null,"components":{"1":169.509434}}]}}"

另一个相似但在 3 个元素上不同的元素:

"2", "237", "room:zRAMDON", "naf",…]
0: "2"
1: "237"
2: "room:zRAMDON"
3: "naf"
4: {dataType: "u", data: {networkId: "mi2p4c5", owner: "fa477e9d-7e92-4c1d-9b7b-6e9e0e975495",…}}
data: {networkId: "mi2p4c5", owner: "fa477e9d-7e92-4c1d-9b7b-6e9e0e975495",…}
components: {0: {x: -0.4637117156600495, y: 2.255, z: 8.3544556410586}, 1: {x: 0, y: -14.000000000000009, z: 0},…}
0: {x: -0.4637117156600495, y: 2.255, z: 8.3544556410586}
1: {x: 0, y: -14.000000000000009, z: 0}
2: {x: 1, y: 1, z: 1}
3: {,…}
4: {left_hand_pose: 0, right_hand_pose: 0}
5: {x: 0, y: 1.6, z: 0}
6: {x: 0, y: 0, z: 0}
7: {x: 0, y: 0, z: 0}
8: {x: 0, y: 0, z: 0}
9: false
10: {x: 0, y: 0, z: 0}
11: {x: 0, y: 0, z: 0}
12: false
creator: "fa477e9d-7e92-4c1d-9b7b-6e9e0e975495"
isFirstSync: true
lastOwnerTime: 1593446133299
networkId: "mi2p4c5"
owner: "fa477e9d-7e92-4c1d-9b7b-6e9e0e975495"
parent: null
persistent: false
template: "#remote-avatar"
dataType: "u"

还有另一个例子:

[null, "235", "phoenix", "heartbeat", {}]
0: null
1: "235"
2: "phoenix"
3: "heartbeat"
4: {}

以及我真正需要的那个:

[null, null, "room:zRAMDON", "message", {session_id: "fa477e9d-7e92-4c1d-9b7b-6e9e0e975495",…}]
0: null
1: null
2: "room:zRAMDON"
3: "message"
4: {session_id: "fa477e9d-7e92-4c1d-9b7b-6e9e0e975495",…}
body: "testtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"
session_id: "fa477e9d-7e92-4c1d-9b7b-6e9e0e975495"
type: "chat"

很明显有一个包含 5 个元素的 json 数组问题出在元素 3 之后。我尝试将所有这些解析为自动 json 去结构工具甚至工具感到困惑并吐出错误..

好的,所以我不确定我在做什么,但我将 3 个差异 json 输出组合成一个,例如:

type WebSocketHubs []WebSocketHubElement

type WebSocketHubClass struct {
    SessionID string `json:"session_id"`
    Body      string `json:"body"`
    Type      string `json:"type"`
    DataType  string `json:"dataType"`
    Data      Data   `json:"data"`
}

type WebSocketHubElement struct {
    String            *string
    WebSocketHubClass *WebSocketHubClass
}

type Data struct {
    NetworkID     string                     `json:"networkId"`
    Owner         string                     `json:"owner"`
    Creator       string                     `json:"creator"`
    LastOwnerTime int64                      `json:"lastOwnerTime"`
    Template      string                     `json:"template"`
    Persistent    bool                       `json:"persistent"`
    Parent        interface{}                `json:"parent"`
    Components    map[string]*ComponentValue `json:"components"`
    IsFirstSync   bool                       `json:"isFirstSync"`
}

type ComponentClass struct {
    X             *float64 `json:"x,omitempty"`
    Y             *float64 `json:"y,omitempty"`
    Z             *float64 `json:"z,omitempty"`
    AvatarSrc     *string  `json:"avatarSrc,omitempty"`
    AvatarType    *string  `json:"avatarType,omitempty"`
    Muted         *bool    `json:"muted,omitempty"`
    LeftHandPose  *int64   `json:"left_hand_pose,omitempty"`
    RightHandPose *int64   `json:"right_hand_pose,omitempty"`
}

type ComponentValue struct {
    Bool           *bool
    ComponentClass *ComponentClass
}

但现在我得到:

读取错误:json:无法将字符串解组为 main.WebSocketHubClass

类型的 Go 值

:(

好的,所以我厌倦了没有帮助和清晰的文档,只是采用了粗暴的方法并使用了对数组和映射的调用,然后比较字符串以获得我需要的内容。

      var leerCHAT []interface{}

    for {
        //line, message, err := ws.ReadMessage()
        err := ws.ReadJSON(&leerCHAT)

        if err != nil {
            log.Printf("Error read: %s", err)
            break
        }
        log.Printf("Message received: %v", leerCHAT[2])
        if leerCHAT[3] == "message" {
            //var cookie string
            cookie := leerCHAT[4].(map[string]interface{})
            log.Printf("el chat dice: %s", cookie["body"].(string))
            chatMSG := cookie["body"].(string)
            go MatrixSay(matrixClient, chatMSG, matrixChannel)

        }

    }

我在另一个问题的帮助下改变了这个,我有另一个问题但最终重构了整个事情。