Graphical explanation of the function call of proto file in Vue

Graphical explanation of the function call of proto file in Vue

1. Compile proto

Create a new proto folder under the src folder to store all .proto files. Open the terminal in the proto folder and enter the following command:

//Enter the proto folder and execute the following compilation, replacing helloworld.proto with the current .proto file name protoc -I=. helloworld.proto \
    --js_out=import_style=commonjs,binary:. \
    --grpc-web_out=import_style=commonjs,mode=grpcwebtext:.

A .proto file (helloworld.proto) is compiled to generate 2 js files:

  • helloworld_pb.js
  • helloworld_grpc_web_pb.js

2. Variables and functions in the compiled proto file

The structure of a function in .proto mainly consists of two parts: function and parameters:

Service Greeter
{
  rpc AddEmployee(Employee) returns (EmployeeID) {} // Submit employee information unary message}
//Send request data type structure message Employee
{
  string name = 1;
  int32 age = 2;
}
//Return the type structure of the function processing result message EmployeeID
{
  int32 id = 1;
}

Function part

After compilation, the service named “service Greeter” and the function AddEmployee are defined in the helloworld_grpc_web_pb.js file:

Parameters section

The parameters of Employee and EmployeeID are defined in helloworld_pb.js:

1. Send the request parameter Employee

The first parameter name of Employee has the following function form (this is the request parameter, using the set format):

The second parameter age of Employee has the following function form (this is a request parameter in set format):

2. Return result parameter EmployeeID

The EmployeeID return result has only one parameter, id. The function structure is as follows (here is the return parameter, using the get format):

Calling functions in proto

A simple calling example is as follows (click the button to generate a click event get_helloworld):

<el-button type="primary" @click="get_helloworld">
    hello_world
</el-button>
get_helloworld() {
    this.client = new GreeterClient("http://192.168.10.102:8181", null, null);

    // Create request parameters and assign values ​​var request = new Employee();
    request.setName("World");
    request.setAge(11);
    // Call the corresponding grpc method of the client, send a grpc request, and receive the return value sent back by the backend this.client.addEmployee(request, {"my-service-header": "test_service"}, (err, response) => {
        if (err) {
            console.log(
                `Unexpected error for addEmployee: code = ${err.code}` +
                `, message = "${err.message}"`
            );
        } else {  
            console.log(response.getId()); // Print the returned information}
    });
},

At this point you can see the returned ID value in the console.

Display the returned results in the interface

The return results of the function should be displayed in the interface controls in an appropriate form, here:

1. Table control

The table control is a frequently used data display control. Here is the sample proto code (returns the list data format and contains enumeration variables):

rpc SelectAllCameras(SelectAllCamerasRequest) returns(SelectAllCamerasResponse){}
// Query all camera devices message SelectAllCamerasRequest{
  int32 page_index = 1;
  int32 page_size = 2;
  string condition = 3;
}
//Return the query result, return an array of CameraInfo, which contains the enumeration type CameraBrand
message SelectAllCamerasResponse{
  CodeErr enumErrorNo = 1;
  repeated CameraInfo cameraArray = 2;
}
// Camera information message CameraInfo{
  string szCameraUID = 1; // uid
  string szName = 2; // Name Dongmenkou Camera CameraBrand enumCameraBrand = 3; // Brand}
// Camera brand enum CameraBrand {
  DEFAULT_CAMERA_BRAND = 0;
  HIKI_VISION = 1;
  DAHUA = 2;
  UNIVIEW = 3;
}

1. Import header file

import { device_register_serviceClient } from "../proto/device_manage_grpc_web_pb";
import { SelectAllCamerasRequest,} from "../proto/device_manage_pb";
 <el-table :data="caminfoTable" ref="caminfoTable" >
   <el-table-column type="index" :index="table_index" align="center" label="Serial number" width="50"></el-table-column>
   <el-table-column prop="UID" label="UID" width="220" align="center">
     <template slot-scope="scope">
       <span>{{scope.row.getSzcamerauid()}}</span>
     </template>
   </el-table-column>
   <el-table-column prop="szName" label="Camera name" width="150" align="center">
     <template slot-scope="scope">
       <span>{{scope.row.getSzname()}}</span>
     </template>
   </el-table-column>
   <el-table-column prop="enumCameraBrand" label="Camera brand" width="120" align="center">
     <template slot-scope="scope">
       <span>{{CameraBrand[scope.row.getEnumcamerabrand()].label}}</span>
     </template>
   </el-table-column>
</el-table>
//Assign the returned result to an array variable caminfoTable:[],
// Camera brand, the CameraBrand here is used to fill in the drop-down box options when adding camera information, and is also used here to display specific data CameraBrand: [
  {value:0, label:"Default"},
  { value: 1, label: "sea*" },
  { value: 2, label: "Big*" },
  { value: 3, label: "宇*" },
],
//Get the information of the camera device get_camerainfo_data(){
   this.client = new device_register_serviceClient("http://192.168.10.102:8181", null, null);
   var request_selectallCam = new SelectAllCamerasRequest();
   request_selectallCam.setPageIndex(this.Pagination_queryInfo.page_index);
   request_selectallCam.setPageSize(this.Pagination_queryInfo.per_page);
   this.client.selectAllCameras(request_selectallCam,{"my-service-header": "dev_manage_service"},(err,response)=>{
     if(err){
        console.log(
           `Unexpected error for selectAllCameras: code = ${err.code}` +
             `, message = "${err.message}"`
         );
     }else{
         var caminfoList = response.getCameraarrayList();
         this.Pagination_total_pages = caminfoList.length; //Get the total number of pages this.caminfoTable = caminfoList; //Assign the returned result to the table data table variable}
   });
   //Adjust the page number to show the first page this.Pagination_queryInfo.page_index=1;
 },

Summarize

This is the end of this article about proto file function calls in vue. For more relevant vue proto file function calls, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue parent component calls child component function implementation
  • Detailed explanation of the basic usage of the auxiliary function mapGetters in vuex
  • Detailed explanation of props and context parameters of SetUp function in Vue3
  • How to use Vuex's auxiliary functions
  • In-depth study of vue2.x--Explanation of the h function

<<:  Win10 64-bit MySQL8.0 download and installation tutorial diagram

>>:  Kali Linux installation VMware tools installation process and VM installation vmtools button gray

Recommend

Mysql Chinese sorting rules description

When using MySQL, we often sort and query a field...

Graphic tutorial on configuring nginx file server in windows 10 system

Download the Windows version of Nginx from the Ng...

How to inherit CSS line-height

How is Line-height inherited?Write a specific val...

How to install git on linux

1. Introduction Git is a free, open source distri...

js realizes the image cutting function

This article example shares the specific code of ...

10 bad habits to avoid in Docker container applications

There is no doubt that containers have become an ...

How to monitor Linux server status

We deal with Linux servers every day, especially ...

Tutorial on installing MySQL 5.7.28 on CentOS 6.2 (mysql notes)

1. Environmental Preparation 1.MySQL installation...

JavaScript data structure bidirectional linked list

A singly linked list can only be traversed from t...

Semantic web pages XHTML semantic markup

Another important aspect of separating structure ...