-
Notifications
You must be signed in to change notification settings - Fork 98
/
RxjsRequesterResponderExample.ts
229 lines (206 loc) · 5.64 KB
/
RxjsRequesterResponderExample.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright 2021-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { RSocket, RSocketConnector, RSocketServer } from "rsocket-core";
import { Codec } from "rsocket-messaging";
import { RxRequestersFactory, RxRespondersFactory } from "rsocket-adapter-rxjs";
import { TcpClientTransport } from "rsocket-tcp-client";
import { TcpServerTransport } from "rsocket-tcp-server";
import { exit } from "process";
import {
firstValueFrom,
interval,
map,
Observable,
of,
take,
tap,
timer,
} from "rxjs";
import Logger from "../shared/logger";
let serverCloseable;
class EchoService {
handleEchoRequestResponse(data: string): Observable<string> {
return timer(1000).pipe(map(() => `Echo: ${data}`));
}
handleEchoRequestStream(data: string): Observable<string> {
return interval(1000).pipe(
map(() => `RxEchoService Echo: ${data}`),
tap((value) => {
Logger.info(`[server] sending: ${value}`);
})
);
}
handleEchoRequestChannel(datas: Observable<string>): Observable<string> {
datas
.pipe(
tap((value) => {
Logger.info(`[server] receiving: ${value}`);
})
)
.subscribe();
return interval(200).pipe(
map((data) => `RxEchoService Echo: ${data}`),
tap((value) => {
Logger.info(`[server] sending: ${value}`);
})
);
}
handleLogFireAndForget(data: string): Observable<void> {
Logger.info(`[server] received: ${data}`);
return of(null);
}
}
function makeServer() {
const stringCodec = new StringCodec();
return new RSocketServer({
transport: new TcpServerTransport({
listenOptions: {
port: 9090,
host: "127.0.0.1",
},
}),
acceptor: {
accept: async () => {
const echoService = new EchoService();
return {
fireAndForget: RxRespondersFactory.fireAndForget(
echoService.handleLogFireAndForget,
stringCodec
),
requestResponse: RxRespondersFactory.requestResponse(
echoService.handleEchoRequestResponse,
{ inputCodec: stringCodec, outputCodec: stringCodec }
),
requestStream: RxRespondersFactory.requestStream(
echoService.handleEchoRequestStream,
{ inputCodec: stringCodec, outputCodec: stringCodec }
),
requestChannel: RxRespondersFactory.requestChannel(
echoService.handleEchoRequestChannel,
{ inputCodec: stringCodec, outputCodec: stringCodec },
4
),
};
},
},
});
}
function makeConnector() {
return new RSocketConnector({
transport: new TcpClientTransport({
connectionOptions: {
host: "127.0.0.1",
port: 9090,
},
}),
});
}
async function fireAndForget(rsocket: RSocket, route: string = "") {
return new Promise((resolve, reject) => {
const request = RxRequestersFactory.fireAndForget(
"Hello World",
stringCodec
);
request(rsocket).subscribe({
complete() {
// give server a chance to handle before continuing
setTimeout(() => {
resolve(null);
}, 100);
},
error(err) {
reject(err);
},
});
});
}
async function requestResponse(rsocket: RSocket) {
const request = RxRequestersFactory.requestResponse(
"Hello World",
stringCodec,
stringCodec
);
return firstValueFrom(
request(rsocket).pipe(tap((data) => Logger.info(`payload[data: ${data};]`)))
);
}
async function requestStream(rsocket: RSocket) {
const request = RxRequestersFactory.requestStream(
"Hello World",
stringCodec,
stringCodec,
5
);
return request(rsocket)
.pipe(
tap((data) => {
Logger.info(`[client] received[data: ${data}]`);
}),
take(10)
)
.toPromise();
}
async function requestChannel(rsocket: RSocket) {
const request = RxRequestersFactory.requestChannel(
interval(1000).pipe(
map((i) => `Hello World ${i}`),
tap((data) => {
Logger.info(`[client] produced[data: ${data}]`);
})
),
stringCodec,
stringCodec,
5
);
return request(rsocket)
.pipe(
tap((data) => {
Logger.info(`[client] received[data: ${data}]`);
}),
take(25)
)
.toPromise();
}
class StringCodec implements Codec<string> {
readonly mimeType: string = "text/plain";
decode(buffer: Buffer): string {
return buffer.toString();
}
encode(entity: string): Buffer {
return Buffer.from(entity);
}
}
const stringCodec = new StringCodec();
async function main() {
const server = makeServer();
const connector = makeConnector();
serverCloseable = await server.bind();
const rsocket = await connector.connect();
Logger.info(`----- Fire And Forget -----`);
await fireAndForget(rsocket);
Logger.info(`----- Request Response -----`);
await requestResponse(rsocket);
Logger.info(`----- Request Stream -----`);
await requestStream(rsocket);
Logger.info(`----- Request Channel -----`);
await requestChannel(rsocket);
}
main()
.then(() => exit())
.catch((error: Error) => {
console.error(error);
exit(1);
});