Re: Help: Implementing websockets in a plugin
expectingtofly <expectingtofly.avhg1b-NUepA2SMhDQqspMVqqL2D+4xXEVPTSb/[email protected]> Thu, 15 Dec 2022 16:25:12 +0000
| Newsgroups | gmane.music.equipment.slimdevices.devel |
|---|---|
| Organization | Logitech Squeezebox Forums |
| Message-ID | <[email protected]> |
Paul Webster wrote:
> Bringing this up again as I have come across another place that could
> use WebSockets as a solution.
> I was looking at a radio station to grab Now Playing info and they use
> WebSockets for it - and they do not seem to have a simple HTTP
> alternative.
>
> Perhaps updating AnyEvent from 5.26 (2010) to 7.x could be part of LMS
> 8.1
> I recognise that it is a big jump though and backwards compatibility may
> well make it impossible to do without another good reason.
>
> https://metacpan.org/changes/distribution/AnyEvent
Bringing up this WebSockets discussion again. I've needed to look at
this as I'm taking the GlobalPlayer plugin to the next stage,
implementing using hls streams for live radio which should improve the
bitrates and enable introducing similar pause/rewind/restart
functionality similar to the BBC Sounds functionality. Unfortunately
most of the information needed comes down websockets so I had no choice
but to look at it.
I've managed to create a non-blocking websockets class that works fine
for my needs, it uses
Code:
--------------------
use IO::Socket::SSL;
use IO::Select;
use Protocol::WebSocket::Client;
--------------------
So I only needed to include the low level Protocol::WebSocket::Client
lib. No need for AnyEvent
It works for my use case when I only want to wait for a message after
sending, then close the connection. It will need another method to
continually listen on a socket, if you need that, but that should not
be too tricky. I thought I would share with you @Paul Webster, as I
know that's something you have expressed would be useful for your
plugins. The branch where I am working on it is here :
https://github.com/expectingtofly/LMS_GlobalPlayer_Plugin/blob/hls_access/GlobalPlayerUK/WebSocketHandler.pm
I use it by doing something like:
Code:
--------------------
my $ws = Plugins::GlobalPlayerUK::WebSocketHandler->new();
$ws->wsconnect(
'wss://some.url.com/v2/now-playing',
sub {#success
$ws->wssend('{"actions":[{"type":"subscribe","service":"some service"}]}');
$ws->wsreceive(
0.2,
sub {
$log->info("Read succeeded");
},
sub {
$log->error("Read failed");
}
);
},
sub {#fail
$log->error("Failed to connect to web socket");
},
sub {#Read
my $readin = shift;
$log->info("We have read ". $readin); #Do something here with what came in
$ws->wssend('{"actions":[{"type":"unsubscribe","stream_id":"Some service"}]}');
$ws->wsclose();
}
);
--------------------
credit to 'this guy'
(https://greg-kennedy.com/wordpress/2019/03/11/writing-a-websocket-client-in-perl-5/),
for clues and code snippets
Stuart McLean
expectingtofly plugins :
'bbc sounds' (https://github.com/expectingtofly/lms_bbc_sounds_plugin),'
global player (uk)'
(https://github.com/expectingtofly/lms_globalplayer_plugin), 'times
radio' (https://github.com/expectingtofly/lms_times_radio_plugin), 'uk
radio player'
(https://github.com/expectingtofly/lms_ukradioplayer_plugin), 'virgin
radio (uk)' (https://github.com/expectingtofly/lms_virgin_radio_plugin)
and the 'radio favourites plugin'
(https://github.com/expectingtofly/lms_radio_favourites_plugin)
for bbc sounds help see the 'bbc sounds wiki'
(https://github.com/expectingtofly/lms_bbc_sounds_plugin/wiki).
------------------------------------------------------------------------
expectingtofly's Profile: http://forums.slimdevices.com/member.php?userid=63263
View this thread: http://forums.slimdevices.com/showthread.php?t=107186