-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] 模拟器上可以,真机上失败了 #1
Comments
看了下日志,好像发送不成功,微信也没有报错,但是服务器没有收到。 |
微信那边有bug,具体hack的方法,我贴到这里了 https://developers.weixin.qq.com/blogdetail?action=get_post_info&docid=0c1e8e0253176ecf0b2212d8fb57ff21 const message = Hello2Server.create(msg)
const encodedMessage = Hello2Server.encode(message).finish()
console.log(`hello:`, encodedMessage)
let newMsg = encodedMessage
if (sysInfo.platform !== 'devtools') {
newMsg = new Uint8Array([...encodedMessage]).buffer
}
console.log('newMsg:', newMsg)
wx.sendSocketMessage({
data: newMsg
}) |
很感谢你的Issue,你应该用到了微信websocket部分。这个工具序列化出来的是类型Uint8Array的数据,但是微信websocket的api 要求发送数据的是String/ArrayBuffer类型:具体可见:https://developers.weixin.qq.com/miniprogram/dev/api/network-socket.html#wxsendsocketmessageobject;所以针对你的问题其实你已经给出答案了,只要取Uint8Array中的buffer就可以了,所以可以将你上部分代码改成如下;如果真机的websocket成功建立连接的话,应该可以发送成功。😁 const message = Hello2Server.create(msg)
const encodedMessage = Hello2Server.encode(message).finish()
console.log(`hello:`, encodedMessage)
let newMsg = encodedMessage.buffer
console.log('newMsg:', newMsg)
wx.sendSocketMessage({
data: newMsg
}) |
@Zhang19910325 你觉得你把这个publish到npm怎么样,我觉得这样会方便很多 |
这样写,在IOS真机上,确实可以发送,但是在server端接收我遇到问题 从日志看,newData.length和data.length都是8192,导致后续的Msg decode有时正常,有时异常 但是如果用模拟器的话,不会出现这个问题,length都是正常的,不是8192,我看8192的十六进制是:0x2000 所以个人感觉,还是在client生成的buffer的问题,但是从ios手机上输出buffer,只能看到是个Object,看不到length |
我在模拟器上做了这样的尝试,把encode.finish得到的数据msg取出msg.buffer进行base64,发现数据长度确实有问题,问题症结点应该是 wx.sendSocketMessage msg: Uint8Array(92) [8, 1, 18, 88, 10, 14, 49, 56, 48, 46, 49, 53, 48, 46, 49, 56, 54, 46, 52, 50, 16, 168, 44, 26, 6, 104, 113, 121, 120, 109, 113, 34, 8, 49, 50, 51, 121, 120, 51, 50, 49, 42, 3, 47, 121, 120, 50, 20, 51, 95, 49, 53, 52, 49, 49, 52, 57, 49, 55, 54, 57, 51, 49, 50, 57, 55, 50, 56, 58, 20, 51, 95, 49, 53, 52, 49, 49, 52, 57, 49, 55, 54, 57, 51, 49, 50, 57, 55, 50, 56, 66, 0] |
@littleferry 用msg.slice().buffer就能得到正确长度的buffer 参考 protobufjs/protobuf.js#852 |
看了下日志,模拟器上时发送的时候是Uint8Array,真机上Object,不知道是不是这个原因。
The text was updated successfully, but these errors were encountered: