@@ -28,7 +28,7 @@ and other tutorials):
28
28
29
29
``` sh
30
30
$ # Clone the repository at the latest release to get the example code:
31
- $ git clone -b 1.0 .0 https://github.com/grpc/grpc-swift
31
+ $ git clone -b 1.13 .0 https://github.com/grpc/grpc-swift
32
32
$ # Navigate to the repository
33
33
$ cd grpc-swift/
34
34
```
@@ -131,64 +131,82 @@ In the same directory, open
131
131
method like this:
132
132
133
133
``` swift
134
- class GreeterProvider : Helloworld_GreeterProvider {
134
+ final class GreeterProvider : Helloworld_GreeterAsyncProvider {
135
+ let interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol? = nil
136
+
135
137
func sayHello (
136
138
request : Helloworld_HelloRequest,
137
- context : StatusOnlyCallContext
138
- ) -> EventLoopFuture< Helloworld_HelloReply> {
139
+ context : GRPCAsyncServerCallContext
140
+ ) async throws -> Helloworld_HelloReply {
139
141
let recipient = request.name .isEmpty ? " stranger" : request.name
140
- let response = Helloworld_HelloReply.with {
142
+ return Helloworld_HelloReply.with {
141
143
$0 .message = " Hello \( recipient ) !"
142
144
}
143
- return context.eventLoop .makeSucceededFuture (response)
144
145
}
145
146
146
147
func sayHelloAgain (
147
148
request : Helloworld_HelloRequest,
148
- context : StatusOnlyCallContext
149
- ) -> EventLoopFuture< Helloworld_HelloReply> {
149
+ context : GRPCAsyncServerCallContext
150
+ ) async throws -> Helloworld_HelloReply {
150
151
let recipient = request.name .isEmpty ? " stranger" : request.name
151
- let response = Helloworld_HelloReply.with {
152
+ return Helloworld_HelloReply.with {
152
153
$0 .message = " Hello again \( recipient ) !"
153
154
}
154
- return context.eventLoop .makeSucceededFuture (response)
155
155
}
156
156
}
157
157
```
158
158
159
159
#### Update the client
160
160
161
161
In the same directory, open
162
- ` Sources/Examples/HelloWorld/Client/main .swift ` . Call the new method like this:
162
+ ` Sources/Examples/HelloWorld/Client/HelloWorldClient .swift ` . Call the new method like this:
163
163
164
164
``` swift
165
- func greet (name : String ? , client greeter : Helloworld_GreeterClient) {
166
- // Form the request with the name, if one was provided.
167
- let request = Helloworld_HelloRequest.with {
168
- $0 .name = name ?? " "
169
- }
165
+ func run () async throws {
166
+ // Setup an `EventLoopGroup` for the connection to run on.
167
+ //
168
+ // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
169
+ let group = MultiThreadedEventLoopGroup (numberOfThreads : 1 )
170
+
171
+ // Make sure the group is shutdown when we're done with it.
172
+ defer {
173
+ try ! group.syncShutdownGracefully ()
174
+ }
170
175
171
- // Make the RPC call to the server.
172
- let sayHello = greeter.sayHello (request)
176
+ // Configure the channel, we're not using TLS so the connection is `insecure`.
177
+ let channel = try GRPCChannelPool.with (
178
+ target : .host (" localhost" , port : self .port ),
179
+ transportSecurity : .plaintext ,
180
+ eventLoopGroup : group
181
+ )
173
182
174
- // wait() on the response to stop the program from exiting before the response is received.
175
- do {
176
- let response = try sayHello.response .wait ()
177
- print (" Greeter received: \( response.message ) " )
178
- } catch {
179
- print (" Greeter failed: \( error ) " )
180
- return
181
- }
183
+ // Close the connection when we're done with it.
184
+ defer {
185
+ try ! channel.close ().wait ()
186
+ }
187
+
188
+ // Provide the connection to the generated client.
189
+ let greeter = Helloworld_GreeterAsyncClient (channel : channel)
182
190
183
- let sayHelloAgain = greeter.sayHelloAgain (request)
184
- do {
185
- let response = try sayHelloAgain.response .wait ()
186
- print (" Greeter received: \( response.message ) " )
187
- } catch {
188
- print (" Greeter failed: \( error ) " )
189
- return
191
+ // Form the request with the name, if one was provided.
192
+ let request = Helloworld_HelloRequest.with {
193
+ $0 .name = self .name ?? " "
194
+ }
195
+
196
+ do {
197
+ let greeting = try await greeter.sayHello (request)
198
+ print (" Greeter received: \( greeting.message ) " )
199
+ } catch {
200
+ print (" Greeter failed: \( error ) " )
201
+ }
202
+
203
+ do {
204
+ let greetingAgain = try await greeter.sayHelloAgain (request)
205
+ print (" Greeter received: \( greetingAgain.message ) " )
206
+ } catch {
207
+ print (" Greeter failed: \( error ) " )
208
+ }
190
209
}
191
- }
192
210
```
193
211
194
212
#### Run!
0 commit comments