Skip to content

Commit

Permalink
Fix sample with Routing metadata changes
Browse files Browse the repository at this point in the history
Prior to this commit, changes in the routing metadata mechanism had
broken this sample application.
The routing metadata is now transmitted over the wire and the metadata
must be encoded/decoded according to the MIME type used.

See https://github.com/rsocket/rsocket/blob/master/Extensions/Routing.md

This commit implements a new Serializer that writes the metadata length
and then the metadata itself as an UTF8 string.

This only supports a single type of metadata.
We'll need to use Composite metadata if we want to send other metadata,
such as authentication.
  • Loading branch information
bclozel committed Sep 9, 2019
1 parent 9f9108c commit eb8ad0a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
29 changes: 29 additions & 0 deletions flight-client/src/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
createBuffer,
byteLength
} from 'rsocket-core';

/**
* Serializer for Routing metadata
* @see https://github.com/rsocket/rsocket/blob/master/Extensions/Routing.md
*/
export class RoutingMetadataSerializer {

deserialize(data) {
throw 'Not implemented!';
}

serialize(data) {
if (data == null) {
return null;
}
let dataLength = byteLength(data, 'utf8');
let outBuffer = createBuffer(1 + dataLength);
outBuffer.writeUInt8(dataLength, 0);
outBuffer.write(data, 1, data.length, 'utf8');
return outBuffer;
}
}

RoutingMetadataSerializer.MIME_TYPE = "message/x.rsocket.routing.v0";
RoutingMetadataSerializer.MIME_TYPE_ID = 0x7E;
20 changes: 16 additions & 4 deletions flight-client/src/radars.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import {
RSocketClient,
JsonSerializer,
IdentitySerializer
IdentitySerializer,
BufferEncoder,
UTF8Encoder
} from 'rsocket-core';
import RSocketWebSocketClient from 'rsocket-websocket-client';
import {RoutingMetadataSerializer} from './metadata'

const CustomEncoders = {
data: UTF8Encoder,
dataMimeType: UTF8Encoder,
message: UTF8Encoder,
metadata: BufferEncoder,
metadataMimeType: UTF8Encoder,
resumeToken: UTF8Encoder,
};

export class RadarClient {

constructor(url, responder) {
this.client = new RSocketClient({
serializers: {
data: JsonSerializer,
metadata: IdentitySerializer,
metadata: new RoutingMetadataSerializer(),
},
setup: {
// ms btw sending keepalive to server
keepAlive: 10000,
// ms timeout if no keepalive response
lifetime: 20000,
dataMimeType: 'application/json',
metadataMimeType: 'message/x.rsocket.routing.v0',
metadataMimeType: RoutingMetadataSerializer.MIME_TYPE,
},
transport: new RSocketWebSocketClient({url: url}),
transport: new RSocketWebSocketClient({url: url}, CustomEncoders),
responder: responder
});
}
Expand Down

0 comments on commit eb8ad0a

Please sign in to comment.