Skip to content

Commit

Permalink
Merge pull request #767 from lynniemagoo/cometd
Browse files Browse the repository at this point in the history
Logitech#765 - Provide support for npm cometd client
  • Loading branch information
mherger committed Feb 20, 2022
2 parents 726f3dd + a4f7c60 commit 5acd438
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Slim/Web/Cometd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ sub handler {
my $delayedResponse; # false if we want to call sendResponse at the end of this method

for my $obj ( @{$objs} ) {

# Issue #765 - For comet meta messages handshake, connect, reconnect, disconnect, subscribe, unsubscribe populate 'id' field on response to ensure compatibility with the readily available npm comet library.
my $msgid;
if ( !$msgid ) {
# obtain 'id' from outer object if present, otherwise default to - Here we use different variable name '$msgid' as '$id' is defined for use within slim/subscribe channel.
if ( $obj->{id} ) {
$msgid = $obj->{id};
} else {
#default to empty string if not available.
$msgid = '';
}
}

if ( ref $obj ne 'HASH' ) {
sendResponse(
@{$conn},
Expand Down Expand Up @@ -218,6 +231,7 @@ sub handler {
if ( !$manager->is_valid_clid( $clid ) ) {
# Invalid clientId, send advice to re-handshake
push @{$events}, {
id => $msgid,
channel => $obj->{channel},
clientId => undef,
successful => JSON::XS::false,
Expand All @@ -241,6 +255,7 @@ sub handler {
};

push @{$events}, {
id => $msgid,
channel => '/meta/handshake',
version => PROTOCOL_VERSION,
supportedConnectionTypes => [ 'long-polling', 'streaming' ],
Expand All @@ -257,6 +272,7 @@ sub handler {
# We want the /meta/(re)connect response to always be the first event
# sent in the response, so it's stored in the special first_event slot
$conn->[HTTP_CLIENT]->first_event( {
id => $msgid,
channel => $obj->{channel},
clientId => $clid,
successful => JSON::XS::true,
Expand Down Expand Up @@ -318,6 +334,7 @@ sub handler {

# disconnect them
push @{$events}, {
id => $msgid,
channel => '/meta/disconnect',
clientId => $clid,
successful => JSON::XS::true,
Expand All @@ -344,6 +361,7 @@ sub handler {

for my $sub ( @{$subscriptions} ) {
push @{$events}, {
id => $msgid,
channel => '/meta/subscribe',
clientId => $clid,
successful => JSON::XS::true,
Expand All @@ -364,6 +382,7 @@ sub handler {

for my $sub ( @{$subscriptions} ) {
push @{$events}, {
id => $msgid,
channel => '/meta/unsubscribe',
clientId => $clid,
subscription => $sub,
Expand Down

0 comments on commit 5acd438

Please sign in to comment.