-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
48 lines (46 loc) · 1.36 KB
/
server.js
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
const {RSocketServer} = require('rsocket-core');
const RSocketWebsocketServer = require('rsocket-websocket-server').default;
const {Flowable, Single} = require('rsocket-flowable');
const getRequestHandler = () => {
return {
requestChannel: clientFlowable => {
let subscription;
let responses = ['g', 'h', 'i'];
responses = responses.map(res => {
return {
data: res
};
});
return new Flowable(subscriber => {
subscriber.onSubscribe();
clientFlowable.subscribe({
onSubscribe: sub => {
subscription = sub;
console.log('Server subscribed to client channel');
subscription.request(1);
},
onNext: clientRequest => {
console.log(clientRequest);
subscription.request(1);
if (responses.length) {
subscriber.onNext(responses.shift());
}
},
onComplete: () => {
console.log('Server received end of client stream');
while(responses.length) {
subscriber.onNext(responses.shift());
}
subscriber.onComplete();
}
});
});
}
}
};
const transport = new RSocketWebsocketServer({
host: 'localhost',
port: 7777
});
const server = new RSocketServer({transport, getRequestHandler});
server.start();