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

Detailed explanation of nginx forward proxy and reverse proxy

Table of contents Forward Proxy nginx reverse pro...

About Vue's 4 auxiliary functions of Vuex

Table of contents 1. Auxiliary functions 2. Examp...

Setting up a proxy server using nginx

Nginx can use its reverse proxy function to imple...

Web front-end development CSS related team collaboration

The front-end development department is growing, ...

CentOS 7 installation and configuration method graphic tutorial

This article records the detailed installation tu...

Detailed explanation of daily_routine example code in Linux

First look at the example code: #/bin/bash cal da...

Mysql join query syntax and examples

Connection query: It is the result of connecting ...

Detailed explanation of Tomcat configuration and optimization solutions

Service.xml The Server.xml configuration file is ...

MySQL 8.0.16 installation and configuration tutorial under CentOS7

Uninstall the old version of MySQL (skip this ste...

Analysis of the ideas of implementing vertical tables in two ways in Vue project

Problem Description In our projects, horizontal t...

Steps to export the fields and related attributes of MySQL tables

Need to export the fields and properties of the t...

MySQL gets the current date and time function

Get the current date + time (date + time) functio...

Summary of the differences between Html, sHtml and XHtml

For example: <u> This has no ending characte...