Skip to content

Instantly share code, notes, and snippets.

@lhlyu
Last active February 2, 2023 08:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhlyu/f39aef08b8f261921bc54acdb2066a87 to your computer and use it in GitHub Desktop.
Save lhlyu/f39aef08b8f261921bc54acdb2066a87 to your computer and use it in GitHub Desktop.
微信小程序云开发 grpc simple & stream
// protoc ./*.proto --js_out=import_style=commonjs:./ --grpc-web_out=import_style=typescript,mode=grpcweb:./
var pb = require('./grpc_pb');
var svc = require('./grpc_grpc_pb');
var grpc = require('@grpc/grpc-js');
// 证书
const tls = `-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----`
const buf = Buffer.from(tls)
function call() {
var client = new svc.gwClient('localhost:1024',
grpc.credentials.createSsl(buf),
);
var request = new pb.SimpleRequest();
request.setParams(`{"id":"100"}`)
var metadata = new grpc.Metadata()
metadata.add('os', 'ios')
metadata.add('version', '1.0.0')
metadata.add('uid', '12323')
metadata.add('token', 'asdk2j3h3hkh')
client.simpleCall(request, metadata, function (err,resp) {
console.log(resp.getCode(), resp.getMsg(), resp.getData());
})
var req = new pb.StreamRequest()
const stream = client.streamCall(metadata)
stream.on('data', resp => {
console.log(resp.getMessagetype())
})
stream.on('end', () => {
console.log('end==>')
})
stream.end()
}
exports.main = async (event, conext) => {
call()
return true
}
@shuchengshun
Copy link

image
你好,你遇到这个问题吗?

@lhlyu
Copy link
Author

lhlyu commented Feb 2, 2023

@shuchengshun 不好意思,这个有点久远了,我不太记得了,感觉小程序弄grpc有点吃力,建议还是用websocket,真心建议

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment