I'm writing a listener service. Data is streaming to me from a specific port. But I cannot read the incoming data and write to consola.
I have a listener service written in GoLang. I tried to extract the listener logic from these codes and adapt it for node.js. But I couldn't.
My service can listen to the port, there is no problem in this regard. Because the place where I get the data produces output because your service is running on its side.
Message returning to me from the place that sent data
These are my proto codes:
syntax = "proto3";
import "rpc_base_msg.proto";
//----------Remote Process Control Models BEGIN----------//
message RPCKeyboardActivityRequest {
RequestModel request_model = 1;
}
message RPCKeyboardActivityResponse {
ResponseModel response_model =1;
}
//----------Remote Process Control Models END-----------//
//---------------Data Tranfer Models BEGIN-------------//
message DTKeyboardActivityModel {
uint64 dt = 1; // Date-Time Stamp
enum Activity {
APPLICATION = 0;
WEB_BROWSER = 1;
}
Activity activity = 2; // Activity: Can be application or a web browser.
string activity_name = 3; //Activity is the name of the application or web page name
string key = 4; // This is the user's keystroke sequence. Changes for every different application or same application in every 60 seconds!!!
bool islive_data=5;
uint64 delete_id=6;
}
message DTKeyboardActivityRequest {
DTKeyboardActivityModel dt_keyboard_activity_model = 1;
}
message DTKeyboardActivityResponse {
bool ret_complete = 1; // Return True/False
}
//---------------Data Tranfer Models END-------------//
//----------------Service BEGIN------------------//
service ActivityService {
rpc SSPCKeyboardActivity (RPCKeyboardActivityRequest) returns (stream RPCKeyboardActivityResponse);
rpc UDTKeyboardActivity (DTKeyboardActivityRequest) returns (DTKeyboardActivityResponse);
rpc BDSDTKeyboardActivity (stream DTKeyboardActivityRequest) returns (stream DTKeyboardActivityResponse);
}
//----------------Service END------------------//
These are the proto import codes:
syntax = "proto3";
message RequestModel {
bool is_process_completed = 1;
}
message ResponseModel {
bool is_monitoring = 1;
}
These are my listen service codes:
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const path = "./protos/test.proto";
const path2= "./protos/activity.proto";
const packageDefination = protoLoader.loadSync(path,{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const packageDef = protoLoader.loadSync(path2,{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const test_proto = grpc.loadPackageDefinition(packageDefination);
const activity_proto = grpc.loadPackageDefinition(packageDef);
function sayHello(call,callback) {
callback(null,{message:'Hello '+ call.request.name});
}
function sayHelloAgain(call,callback) {
callback(null,{message:'Hello Again ' + call.request.name});
}
function SSPCKeyboardActivity(call){
let rpcKeyboardActivityRequest = call.request.RPCKeyboardActivityRequest;
console.log("rpcKeyboardActivityRequest:" + rpcKeyboardActivityRequest);
let monitorStatusChange = true;
let whileControl = true;
console.log("SSPCKeyboardActivity client recv :" + GetRequestModel());
while (whileControl === true){
call.write({IsMonitoring: monitorStatusChange})
monitorStatusChange = !monitorStatusChange;
if (!monitorStatusChange){
setTimeout(function () {},30000)
}else {
setTimeout(function () {},10000)
}
}
}
function UDTKeyboardActivity(call,callback) {
let context = call.metadata.get('context.Context');
console.log("Context" + context);
if (context){
let udtKeyboardActivity = call.request.DTKeyboardActivityRequest;
console.log("udtKeyboardActivity" + udtKeyboardActivity);
let dtKeyboardActivityResponse = call.request.DTKeyboardActivityResponse;
console.log("dtKeyboardActivityResponse" + dtKeyboardActivityResponse);
callback(null , dtKeyboardActivityResponse);
console.log("CallBack" + callback);
}else {
console.log("Contex Error");
}
}
function BDSDTKeyboardActivity(call) {
call.on('data', function () {
let dtKeyboardActivityRequest = call.request.DTKeyboardActivityRequest;
console.log("dtKeyboardActivityRequest" + dtKeyboardActivityRequest);
})
call.on('end',function () {
call.write(DTKeyboardActivityResponse);
console.log("Write DTKeyboardActivityResponse"+DTKeyboardActivityResponse);
})
}
function main(){
const server = new grpc.Server();
server.bindAsync('0.0.0.0:5801',grpc.ServerCredentials.createInsecure(),()=>{
server.start();
console.log("Server listening at :5801");
});
server.addService(test_proto.Greeter.service,{
sayHello: sayHello,
sayAgainHello: sayHelloAgain
});
server.addService(activity_proto.ActivityService.service, {
SSPCKeyboardActivity : SSPCKeyboardActivity,
UDTKeyboardActivity: UDTKeyboardActivity,
BDSDTKeyboardActivity: BDSDTKeyboardActivity,
})
}
main();
"path" and testproto are not a problem for me. I ran it. I am having a problem with activity_proto.
I came across after the service was running:
➜ service node server/server.js
Server listening at :5801
As an example, I also share the functions in the service code block written in go.
package service
import (
"context"
"github.com/******/pb"
"log"
"time"
)
type ActivityServer struct {
}
type ActivityRpcUpdateModel struct {
IsMonitor bool
}
func NewActivityServer() *ActivityServer {
return &ActivityServer{}
}
func (a *ActivityServer) SSPCKeyboardActivity(request *pb.RPCKeyboardActivityRequest, server pb.ActivityService_SSPCKeyboardActivityServer)
error {
monitorStatusChange := true
log.Printf("SSPCKeyboardActivity client recv : %v", request.GetRequestModel())
for {
err := server.Send(&pb.RPCKeyboardActivityResponse{ResponseModel: &pb.ResponseModel{IsMonitoring: monitorStatusChange}})
if err != nil {
log.Printf("SSPCForegroundApp err : %v", err)
}
monitorStatusChange = !monitorStatusChange
if !monitorStatusChange {
time.Sleep(time.Second * 30)
} else {
time.Sleep(time.Second * 10)
}
}
}
func (a *ActivityServer) UDTKeyboardActivity(
ctx context.Context,
request *pb.DTKeyboardActivityRequest,
) (*pb.DTKeyboardActivityResponse, error) {
log.Println(request.GetDtKeyboardActivityModel())
return &pb.DTKeyboardActivityResponse{
RetComplete: true,
}, nil
}
func (a *ActivityServer) BDSDTKeyboardActivity(
server pb.ActivityService_BDSDTKeyboardActivityServer,
) error {
panic("implement me")
}
I cannot catch the incoming data. And I can't see in the consol. What kind of changes should I make to my functions on the node.js side of me?