|
1 | 1 | <template> |
| 2 | + <div> |
| 3 | + <h2>gRPC-Web ASP.NET Core example</h2> |
2 | 4 | <div> |
3 | | - <h2>gRPC-Web ASP.NET Core example</h2> |
4 | | - <div> |
5 | | - <span>CORS:</span> |
6 | | - <select v-model="cors"> |
7 | | - <option value="same">Same Origin</option> |
8 | | - <option value="different">Different Origin</option> |
9 | | - </select> |
10 | | - <span>Hello:</span> |
11 | | - <input type="text" v-model="name" /> |
12 | | - <input type="button" |
13 | | - value="Send unary" |
14 | | - v-bind:disabled="streamingCall" |
15 | | - @click="sendUnary()" /> |
16 | | - <input type="button" |
17 | | - v-model="streamingButtonText" |
18 | | - @click="sendServerStream()" /> |
19 | | - </div> |
20 | | - <div> |
21 | | - <p v-if="result">{{result}}</p> |
22 | | - <p v-for="item in streamResults" :key="item">{{item}}</p> |
23 | | - </div> |
| 5 | + <span>CORS:</span> |
| 6 | + <select v-model="cors"> |
| 7 | + <option value="same">Same Origin</option> |
| 8 | + <option value="different">Different Origin</option> |
| 9 | + </select> |
| 10 | + <span>Hello:</span> |
| 11 | + <input type="text" v-model="name" /> |
| 12 | + <input type="button" value="Send unary" v-bind:disabled="streamingCall" @click="sendUnary()" /> |
| 13 | + <input type="button" v-model="streamingButtonText" @click="sendServerStream()" /> |
24 | 14 | </div> |
| 15 | + <div> |
| 16 | + <p v-if="result">{{result}}</p> |
| 17 | + <p v-for="item in streamResults" :key="item">{{item}}</p> |
| 18 | + </div> |
| 19 | + </div> |
25 | 20 | </template> |
26 | 21 |
|
27 | 22 | <script lang="ts"> |
28 | | - import { Component, Vue } from "vue-property-decorator"; |
29 | | - import { GreeterClient, HelloRequest } from "./generated/greet_grpc_web_pb"; |
| 23 | +import { Component, Vue } from "vue-property-decorator"; |
| 24 | +import { GreeterClient, HelloRequest } from "./generated/greet_grpc_web_pb"; |
30 | 25 |
|
31 | | - function getDifferentOrigin() { |
32 | | - const schema = window.location.protocol as "http:" | "https:"; |
33 | | - const differentSchema = schema === "http:" ? "https" : "http"; |
34 | | - // when port is omitted |
35 | | - if (!window.location.port) { |
36 | | - return `${differentSchema}://${window.location.host}`; |
37 | | - } |
| 26 | +function getDifferentOrigin() { |
| 27 | + const schema = window.location.protocol as "http:" | "https:"; |
| 28 | + const differentSchema = schema === "http:" ? "https" : "http"; |
| 29 | + // when port is omitted |
| 30 | + if (!window.location.port) { |
| 31 | + return `${differentSchema}://${window.location.host}`; |
| 32 | + } |
38 | 33 |
|
39 | | - const port = parseInt(window.location.port); |
| 34 | + const port = parseInt(window.location.port); |
40 | 35 |
|
41 | | - let differentPort: number; |
42 | | - if (port === 80) { |
43 | | - differentPort = 443; |
44 | | - } else if (port === 443) { |
45 | | - differentPort = 80; |
46 | | - } else { |
47 | | - // handle aspnet dev server |
48 | | - differentPort = schema === "http:" ? port + 1 : port - 1; |
49 | | - } |
| 36 | + let differentPort: number; |
| 37 | + if (port === 80) { |
| 38 | + differentPort = 443; |
| 39 | + } else if (port === 443) { |
| 40 | + differentPort = 80; |
| 41 | + } else { |
| 42 | + // handle aspnet dev server |
| 43 | + differentPort = schema === "http:" ? port + 1 : port - 1; |
| 44 | + } |
50 | 45 |
|
51 | | - return `${differentSchema}://${window.location.hostname}:${differentPort}`; |
52 | | - } |
| 46 | + return `${differentSchema}://${window.location.hostname}:${differentPort}`; |
| 47 | +} |
53 | 48 |
|
54 | | - @Component({}) |
55 | | - export default class GrpcWebDemo extends Vue { |
56 | | - private defaultClient = new GreeterClient(window.location.origin, null, null); |
57 | | - private corsClient = new GreeterClient(getDifferentOrigin(), null, null); |
58 | | - get client() { |
59 | | - return this.cors === "same" ? this.defaultClient : this.corsClient; |
60 | | - } |
61 | | - cors: "same" | "different" = "same"; |
62 | | - name = ""; |
63 | | - result = ""; |
64 | | - streamingButtonText = "Start server stream"; |
65 | | - streamResults: string[] = []; |
66 | | - streamingCall: any = null; |
| 49 | +@Component({}) |
| 50 | +export default class GrpcWebDemo extends Vue { |
| 51 | + private defaultClient = new GreeterClient(window.location.origin, null, null); |
| 52 | + private corsClient = new GreeterClient(getDifferentOrigin(), null, null); |
| 53 | + get client() { |
| 54 | + return this.cors === "same" ? this.defaultClient : this.corsClient; |
| 55 | + } |
| 56 | + cors: "same" | "different" = "same"; |
| 57 | + name = ""; |
| 58 | + result = ""; |
| 59 | + streamingButtonText = "Start server stream"; |
| 60 | + streamResults: string[] = []; |
| 61 | + streamingCall: any = null; |
67 | 62 |
|
68 | | - sendUnary() { |
69 | | - this.result = ""; |
70 | | - this.streamResults = []; |
| 63 | + sendUnary() { |
| 64 | + this.result = ""; |
| 65 | + this.streamResults = []; |
71 | 66 |
|
72 | | - const request = new HelloRequest(); |
73 | | - request.setName(this.name); |
74 | | - this.client.sayHello(request, {}, (err, response) => { |
75 | | - this.result = response.getMessage(); |
76 | | - }); |
77 | | - } |
| 67 | + const request = new HelloRequest().setName(this.name); |
| 68 | + this.client.sayHello(request, {}, (err, response) => { |
| 69 | + this.result = response.getMessage(); |
| 70 | + }); |
| 71 | + } |
78 | 72 |
|
79 | | - sendServerStream() { |
80 | | - if (!this.streamingCall) { |
81 | | - this.streamingButtonText = "Stop server stream"; |
82 | | - this.result = ""; |
83 | | - this.streamResults = []; |
| 73 | + sendServerStream() { |
| 74 | + if (!this.streamingCall) { |
| 75 | + this.streamingButtonText = "Stop server stream"; |
| 76 | + this.result = ""; |
| 77 | + this.streamResults = []; |
84 | 78 |
|
85 | | - const request = new HelloRequest(); |
86 | | - request.setName(this.name); |
| 79 | + const request = new HelloRequest().setName(this.name); |
87 | 80 |
|
88 | | - this.streamingCall = this.client.sayHellos(request, {}); |
89 | | - this.streamingCall.on("data", response => { |
90 | | - const result = response.getMessage(); |
91 | | - this.streamResults.push(result); |
92 | | - }); |
93 | | - this.streamingCall.on("end", function () { }); |
94 | | - } else { |
95 | | - this.streamingButtonText = "Start server stream"; |
96 | | - this.streamingCall.cancel(); |
97 | | - this.streamingCall = null; |
98 | | - } |
99 | | - } |
100 | | -
|
101 | | - mounted() { } |
| 81 | + this.streamingCall = this.client.sayHellos(request, {}); |
| 82 | + this.streamingCall.on("data", response => { |
| 83 | + const result = response.getMessage(); |
| 84 | + this.streamResults.push(result); |
| 85 | + }); |
| 86 | + this.streamingCall.on("end", function() {}); |
| 87 | + } else { |
| 88 | + this.streamingButtonText = "Start server stream"; |
| 89 | + this.streamingCall.cancel(); |
| 90 | + this.streamingCall = null; |
102 | 91 | } |
| 92 | + } |
| 93 | +
|
| 94 | + mounted() {} |
| 95 | +} |
103 | 96 | </script> |
104 | 97 |
|
105 | 98 | <style lang="scss" scoped> |
|
0 commit comments