@@ -6,14 +6,6 @@ import RSocketNIOChannel
6
6
import RSocketReactiveSwift
7
7
import RSocketWSTransport
8
8
9
- func route( _ route: String ) -> Data {
10
- let encodedRoute = Data ( route. utf8)
11
- precondition ( encodedRoute. count <= Int ( UInt8 . max) , " route is to long to be encoded " )
12
- let encodedRouteLength = Data ( [ UInt8 ( encodedRoute. count) ] )
13
-
14
- return encodedRouteLength + encodedRoute
15
- }
16
-
17
9
extension URL : ExpressibleByArgument {
18
10
public init ? ( argument: String ) {
19
11
guard let url = URL ( string: argument) else { return nil }
@@ -40,23 +32,26 @@ struct TwitterClientExample: ParsableCommand {
40
32
func run( ) throws {
41
33
let bootstrap = ClientBootstrap (
42
34
transport: WSTransport ( ) ,
43
- config: ClientConfiguration . mobileToServer
35
+ config: . mobileToServer
44
36
. set ( \. encoding. metadata, to: . messageXRSocketRoutingV0)
45
37
. set ( \. encoding. data, to: . applicationJson)
46
38
)
47
39
48
40
let client = try bootstrap. connect ( to: . init( url: url) ) . first ( ) !. get ( )
49
-
50
- try client. requester. requestStream ( payload: Payload (
51
- metadata: route ( " searchTweets " ) ,
52
- data: Data ( searchString. utf8)
53
- ) )
54
- . attemptMap { payload -> String in
55
- // pretty print json
56
- let json = try JSONSerialization . jsonObject ( with: payload. data, options: [ ] )
57
- let data = try JSONSerialization . data ( withJSONObject: json, options: [ . prettyPrinted] )
58
- return String ( decoding: data, as: UTF8 . self)
59
- }
41
+ try client. requester. build ( RequestStream {
42
+ Encoder ( )
43
+ . encodeStaticMetadata ( " searchTweets " , using: RoutingEncoder ( ) )
44
+ . mapData { ( string: String ) in
45
+ Data ( string. utf8)
46
+ }
47
+ Decoder ( )
48
+ . mapData { data -> String in
49
+ // pretty print json
50
+ let json = try JSONSerialization . jsonObject ( with: data, options: [ ] )
51
+ let data = try JSONSerialization . data ( withJSONObject: json, options: [ . prettyPrinted] )
52
+ return String ( decoding: data, as: UTF8 . self)
53
+ }
54
+ } , request: searchString)
60
55
. logEvents ( identifier: " route.searchTweets " )
61
56
. take ( first: limit)
62
57
. wait ( )
0 commit comments