如何在 Nodejs 中使用 gRPC 流式传输字节?
How to stream bytes using gRPC in Nodejs?
gRPC 的新手,我正在尝试使用 grpc 流式传输视频文件,客户端和服务器都在节点中,当 运行 服务时,我可以在服务器端控制台记录缓冲区,但是我无法从客户端获得任何信息,谁能帮帮我,谢谢!
video.proto:
syntax = "proto3";
package videoservice;
service VideoService {
rpc callVideo (EmptyRequest) returns (stream VideoResponse);
}
message EmptyRequest {}
message VideoResponse {
bytes videoStream = 1;
}
在server.js中的实施:
var PROTO_PATH = __dirname + '/video.proto';
var fs = require('fs');
var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var videoservice = grpc.loadPackageDefinition(packageDefinition).videoservice;
function callVideo(call) {
let videoDataStream = fs.createReadStream('./sample.mp4');
videoDataStream.on('data',function(chunk){
console.log(chunk);
call.write(chunk);
}).on('end',function(){
call.end();
})
}
function getServer() {
var server = new grpc.Server();
server.addService(videoservice.VideoService.service, {
callVideo: callVideo
});
return server;
}
if (require.main === module) {
// If this is run as a script, start a server on an unused port
var videoServer = getServer();
videoServer.bind('0.0.0.0:9090', grpc.ServerCredentials.createInsecure());
videoServer.start();
}
exports.getServer = getServer;
和client.js:
var PROTO_PATH = __dirname + '/video.proto';
var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var vs = grpc.loadPackageDefinition(packageDefinition).videoservice;
const REMOTE_URL = "localhost:9090";
let client = new vs.VideoService(REMOTE_URL, grpc.credentials.createInsecure());
client.callVideo({}).on('data',function(chunk){
console.log(chunk.videoStream);
});
下面是输出,左边部分是进入gRPC通道之前服务器端的缓冲区内容,右边部分是客户端的结果,每个块中什么都没有:
这将有助于解决您的问题:
function callVideo(call) {
const videoDataStream = fs.createReadStream('./sample.mp4');
videoDataStream.on('data', (chunk) => {
console.log(chunk);
call.write({ videoStream: chunk });
});
videoDataStream.on('end', () => {
call.end();
});
}
不要注意改变代码的风格。这里最主要的是call.write({videoStream: chunk});
.
gRPC 的新手,我正在尝试使用 grpc 流式传输视频文件,客户端和服务器都在节点中,当 运行 服务时,我可以在服务器端控制台记录缓冲区,但是我无法从客户端获得任何信息,谁能帮帮我,谢谢!
video.proto:
syntax = "proto3";
package videoservice;
service VideoService {
rpc callVideo (EmptyRequest) returns (stream VideoResponse);
}
message EmptyRequest {}
message VideoResponse {
bytes videoStream = 1;
}
在server.js中的实施:
var PROTO_PATH = __dirname + '/video.proto';
var fs = require('fs');
var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var videoservice = grpc.loadPackageDefinition(packageDefinition).videoservice;
function callVideo(call) {
let videoDataStream = fs.createReadStream('./sample.mp4');
videoDataStream.on('data',function(chunk){
console.log(chunk);
call.write(chunk);
}).on('end',function(){
call.end();
})
}
function getServer() {
var server = new grpc.Server();
server.addService(videoservice.VideoService.service, {
callVideo: callVideo
});
return server;
}
if (require.main === module) {
// If this is run as a script, start a server on an unused port
var videoServer = getServer();
videoServer.bind('0.0.0.0:9090', grpc.ServerCredentials.createInsecure());
videoServer.start();
}
exports.getServer = getServer;
和client.js:
var PROTO_PATH = __dirname + '/video.proto';
var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var vs = grpc.loadPackageDefinition(packageDefinition).videoservice;
const REMOTE_URL = "localhost:9090";
let client = new vs.VideoService(REMOTE_URL, grpc.credentials.createInsecure());
client.callVideo({}).on('data',function(chunk){
console.log(chunk.videoStream);
});
下面是输出,左边部分是进入gRPC通道之前服务器端的缓冲区内容,右边部分是客户端的结果,每个块中什么都没有:
这将有助于解决您的问题:
function callVideo(call) {
const videoDataStream = fs.createReadStream('./sample.mp4');
videoDataStream.on('data', (chunk) => {
console.log(chunk);
call.write({ videoStream: chunk });
});
videoDataStream.on('end', () => {
call.end();
});
}
不要注意改变代码的风格。这里最主要的是call.write({videoStream: chunk});
.