Last active
February 2, 2023 08:21
-
-
Save lhlyu/f39aef08b8f261921bc54acdb2066a87 to your computer and use it in GitHub Desktop.
微信小程序云开发 grpc simple & stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你好,你遇到这个问题吗?