github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/example/routeguide/nodejs/client.js (about) 1 const util = require('util'); 2 const grpc = require('@grpc/grpc-js'); 3 4 const features = require('../../../example/routeguide/features'); 5 const messages = require('../../../example/routeguide/routeguide_pb') 6 const services = require('../../../example/routeguide/routeguide_grpc_pb') 7 8 console.log(`Loaded ${features.length} from feature database`); 9 const COORD_FACTOR = 1e7; 10 11 12 function newPoint(latitude, longitude) { 13 const point = new messages.Point() 14 point.setLatitude(latitude); 15 point.setLongitude(longitude); 16 return point; 17 } 18 19 function newRectangle(lo, hi) { 20 const rect = new messages.Rectangle() 21 rect.setLo(lo); 22 rect.setHi(hi); 23 return rect; 24 } 25 26 function newNote(point, message) { 27 const note = new messages.RouteNote() 28 note.setLocation(point); 29 note.setMessage(message); 30 return note; 31 } 32 33 34 async function runGetFeature(client) { 35 const method = util.promisify(client.getFeature).bind(client); 36 for (const point of [ 37 newPoint(409146138, -746188906), 38 newPoint(1, 1) 39 ]) { 40 const feature = await method(point); 41 if (feature.getName() && feature.getName() != "undefined") { 42 console.log( 43 'Found feature called "' + feature.getName() + '" at ' + 44 feature.getLocation().getLatitude() / COORD_FACTOR + ', ' + 45 feature.getLocation().getLongitude() / COORD_FACTOR 46 ); 47 } else { 48 console.log('Found no feature'); 49 } 50 } 51 } 52 53 54 function runListFeatures(client) { 55 return new Promise((resolve, reject) => { 56 const rectangle = newRectangle( 57 newPoint(400000000, -750000000), 58 newPoint(420000000, -73000000) 59 ); 60 console.log('Looking for features between 40, -75 and 42, -73'); 61 62 const call = client.listFeatures(rectangle); 63 call.on('data', (feature) => { 64 console.log( 65 'Found feature called "' + feature.getName() + '" at ' + 66 feature.getLocation().getLatitude() / COORD_FACTOR + ', ' + 67 feature.getLocation().getLongitude() / COORD_FACTOR 68 ); 69 }); 70 call.on('end', resolve); 71 }); 72 } 73 74 75 function runRecordRoute(client) { 76 return new Promise((resolve, reject) => { 77 const call = client.recordRoute((error, stats) => { 78 if (error) return reject(error); 79 console.log('Finished trip with', stats.getPointCount(), 'points'); 80 console.log('Passed', stats.getFeatureCount(), 'features'); 81 console.log('Traveled', stats.getDistance(), 'meters'); 82 console.log('It took', stats.getElapsedTime(), 'seconds'); 83 resolve(); 84 }); 85 86 for (let i = 0; i < 10; i++) { 87 const randIndex = ~~(Math.random() * (features.length - 1)) 88 console.log("randomIndex", randIndex); 89 const randomPointJson = features[randIndex]; 90 const randomPoint = newPoint( 91 randomPointJson.location.latitude, randomPointJson.location.longitude 92 ) 93 console.log("randomPoint", randomPointJson, randomPoint.toObject()); 94 call.write(randomPoint); 95 } 96 97 call.end(); 98 }); 99 } 100 101 102 function runRouteChat(client) { 103 return new Promise((resolve, reject) => { 104 const call = client.routeChat(); 105 call.on('data', (note) => { 106 console.log( 107 'Got message "' + note.getMessage() + '" at ' + 108 note.getLocation().getLatitude() + ', ' + note.getLocation().getLongitude() 109 ); 110 }); 111 call.on('end', resolve); 112 113 const notes = [ 114 newNote(newPoint(0, 0), 'First message'), 115 newNote(newPoint(0, 1), 'Second message'), 116 newNote(newPoint(1, 0), 'Third message'), 117 newNote(newPoint(0, 0), 'Fourth message'), 118 ] 119 for (const note of notes) { 120 console.log( 121 'Sending message "' + note.getMessage() + '" at ' + 122 note.getLocation().getLatitude() + ', ' + note.getLocation().getLongitude() 123 ); 124 call.write(note); 125 } 126 127 call.end(); 128 }); 129 } 130 131 132 async function main() { 133 let port = '50055'; 134 if (process.env.SERVER_PORT) port = process.env.SERVER_PORT; 135 const addr = 'localhost:' + port; 136 const client = new services.RouteGuideClient(addr, grpc.credentials.createInsecure()); 137 138 client.waitForReady(4000, async () => { 139 await runGetFeature(client); 140 await runListFeatures(client); 141 await runRecordRoute(client); 142 await runRouteChat(client); 143 }); 144 } 145 146 if (require.main === module) { 147 main().catch((e) => { 148 console.log(e) 149 process.exit(1); 150 }); 151 }