Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing connInfo and chanInfo in presence #221

Merged
merged 1 commit into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Install dependencies
run: yarn install
- name: Start Centrifugo
run: docker run -d -p 8000:8000 centrifugo/centrifugo:latest centrifugo --client_insecure --http_stream --sse
run: docker run -d -p 8000:8000 -e CENTRIFUGO_PRESENCE=true centrifugo/centrifugo:latest centrifugo --client_insecure --http_stream --sse
- name: Lint
run: yarn lint
- name: Test
Expand Down
41 changes: 41 additions & 0 deletions src/centrifuge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,44 @@ test.each(transportCases.slice(0, 1))("%s: not connecting on online in disconnec
networkEventTarget.dispatchEvent(onlineEvent);
expect(c.state).toBe(Centrifuge.State.Disconnected);
});

test.each(transportCases)("%s: subscribe and presence", async (transport, endpoint) => {
const c = new Centrifuge([{
transport: transport as TransportName,
endpoint: endpoint,
}], {
websocket: WebSocket,
fetch: fetch,
eventsource: EventSource,
readableStream: ReadableStream,
emulationEndpoint: 'http://localhost:8000/emulation'
});

c.connect();
await c.ready(5000);
const sub = c.newSubscription('test');

sub.subscribe()
await sub.ready(5000);
expect(sub.state).toBe(Centrifuge.SubscriptionState.Subscribed);
expect(c.state).toBe(Centrifuge.State.Connected);

const presence = await sub.presence();
expect(Object.keys(presence.clients).length).toBeGreaterThan(0);

const presenceStats = await sub.presenceStats();
expect(presenceStats.numClients).toBeGreaterThan(0)
expect(presenceStats.numUsers).toBeGreaterThan(0);

let disconnectCalled: any;
const disconnectedPromise = new Promise<DisconnectedContext>((resolve, _) => {
disconnectCalled = resolve;
})
c.on('disconnected', (ctx) => {
disconnectCalled(ctx);
})

c.disconnect();
await disconnectedPromise;
expect(c.state).toBe(Centrifuge.State.Disconnected);
});
15 changes: 14 additions & 1 deletion src/centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,21 @@ export class Centrifuge extends (EventEmitter as new () => TypedEventEmitter<Cli

return this._methodCall().then(function () {
return self._callPromise(cmd, function (reply: any) {
const clients = reply.presence.presence;
for (const clientId in clients) {
if (clients.hasOwnProperty(clientId)) {
const connInfo = clients[clientId]['conn_info'];
const chanInfo = clients[clientId]['chan_info'];
if (connInfo) {
clients[clientId].connInfo = connInfo;
}
if (chanInfo) {
clients[clientId].chanInfo = chanInfo;
}
}
}
return {
'clients': reply.presence.presence
'clients': clients
};
});
});
Expand Down
39 changes: 39 additions & 0 deletions src/protobuf/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,42 @@ test.each(transportCases)("%s (Protobuf): publish and receive message", async (t
c.disconnect();
expect(ctx.data).toStrictEqual(binary);
});

test.each(transportCases)("%s (Protobuf): subscribe and presence", async (transport, endpoint) => {
const c = new Centrifuge([{
transport: transport as TransportName,
endpoint: endpoint,
}], {
protocol: 'protobuf',
websocket: WebSocket,
fetch: fetch,
readableStream: ReadableStream,
emulationEndpoint: 'http://localhost:8000/emulation'
});

c.connect();
await c.ready(5000);

const sub = c.newSubscription('test');
sub.subscribe()
await sub.ready(5000);

const presence = await sub.presence();
expect(Object.keys(presence.clients).length).toBeGreaterThan(0);

const presenceStats = await sub.presenceStats();
expect(presenceStats.numClients).toBeGreaterThan(0)
expect(presenceStats.numUsers).toBeGreaterThan(0);

let disconnectCalled: any;
const disconnectedPromise = new Promise<DisconnectedContext>((resolve, _) => {
disconnectCalled = resolve;
})
c.on('disconnected', (ctx) => {
disconnectCalled(ctx);
})

c.disconnect();
await disconnectedPromise;
expect(c.state).toBe(Centrifuge.State.Disconnected);
});