gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:22:03 25 4
gpt4 key购买 nike

我是新手,我正在尝试连接到一个使用 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)
}
}()
我遇到的问题是我得到了 diff websocket msg,所以不能只写一个结构。
我得到了一个 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。
理想:如果我能以某种方式动态搜索接口(interface),并且如果 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"}]
所以我需要以某种方式获得过滤器/摆脱,然后从这个响应中我可以编写一个结构来放入空、空、 channel 、消息等
谢谢您的帮助。这真让我抓狂
编辑2:
好的,我已经打开了 chrome 开发工具的 websocket 网络选项卡:
我注意到总是有一个由 4 个元素组成的数组,然后在最后打开一个 json。但是这个 json 总是 diff 并且到目前为止的解密是模糊的:
["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 来运行 struct 工具,甚至该工具也会感到困惑并吐出错误。
好的,所以我不确定我在做什么,但我已将 3 个 diff 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 值
:(

最佳答案

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

      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)

}

}
我在另一个问题的帮助下改变了这一点,我遇到了另一个问题,但最终重构了整个事情。
Whats the best way to get content from a generic and somehow dynamic go map?

关于go websockets with gorilla libs - 未知的 json 响应,所以无法映射它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62630620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com