Skip to Content
It is currently December 7th, 2021, 12:05 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: March 11th, 2020, 3:35 pm 
Offline
Senior User
Senior User

Joined: July 23rd, 2007, 2:22 am
Posts: 157
Location: Vancouver, BC, Canada
One of the things that bugs me about the phpWeb templates is that SAM shows how many people are listening up until the song changes again. If someone drops connection half way through the song, the phpWeb template still shows that person is still listening.

I'm envisioning that there has to be some bit of java code that will tell me when SAM detects connects and disconnects at all times, not just when the current song ends and a new one starts.

If I'm sitting there looking at my page I'd like to see a live account of how many listeners are connecting and disconnecting even while the song is playing.

Is this possible or am I barking up the wrong tree with the phpWeb templates?

_________________
Park Magic Radio
http://parkmagicradio.ca


Top
 Profile  
Reply with quote  
PostPosted: March 12th, 2020, 7:40 am 
Offline
SVS Member
SVS Member

Joined: May 8th, 2004, 9:00 am
Posts: 10551
Location: Denver, CO
Two ways to accomplish this. Query the actual streaming server for listener count on a frequent basis like every 30 seconds to a minute. Or query SAM's stats relay to get current listener count.

In my case I don't have direct access to the streaming server so I have SAM write to an sql table every minute via PAL and I query that table. I have my web page updated every minute by reading that table I created in the database.

When i had access to the streaming server, I ran a server side php script that would query the streaming server and write to a table, it was triggered every minute via PAL.

_________________
- Rob Oyler, SVS
Image


Top
 Profile  
Reply with quote  
PostPosted: March 12th, 2020, 8:01 am 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8299
Location: Cologne (Germany)
You're mistaking Java for JavaScript. The two are not related at all, despite a similar name.
The PHP templates pick up the current listener count from the SAM database, which only stores that number once for every track played in the historylist. So as soon as the track starts playing the then current listener count is recorded.

If you want a live listener count, you can ask your streamserver for it. Shoutcast v2 can give proper JSON responses for statistics which are easy to parse in JS.
There's just one limitation: If your website is https (lock symbol, certificate, etc.), you can only fetch data from other HTTPS sites using JavaScript. Stream servers usually are NOT providing an HTTPS connection. (Though, it can be configured in Shoutcast 2 or can be wrapped by a reverse proxy)

For example shoutcast 2 reports all the statistics of the server/stream on this address:
<your-server-hostname-and-port>/stats?sid=<your-SID, if you're alone on that server it's usually 1>&json=1
i.e.: http://144.76.106.52:7000/stats?sid=1&json=1 (Stats page from a fairly popular [~500 listeners across all formats] German ProgHouse Channel called Hirschmilch Radio)

This code will look through your website for an HTML element with the ID stats and put the current number of listeners in there.
Code:
fetch('http://144.76.106.52:7000/stats?sid=1&json=1').then((res) => {return res.json()}).then((data) => {document.getElementById('stats').innerHTML = data.currentlisteners;})

What it does is 1: fetch the json document from the Shoutcast v2 server., 2: then translate JSON to javascript object, 3: then find an HTML element (i.e. <div>, <span>, etc.) with the id 'stats' (i.e.: <div id="stats">) and set the innerHTML of that element to the number from the Shoutcast stats page. (innerHTML overwrites everything enclosed by the element, i.e.: <div id="stats">Our Listener count is: 1</div> would be overwritten by <div id="stats">99</div> if there were 99 currentListeners)


To make it update regularly every 30 seconds:
Code:
setInterval(() => {fetch('http://144.76.106.52:7000/stats?sid=1&json=1').then((res) => {return res.json()}).then((data) => {document.getElementById('stats').innerHTML = data.currentlisteners;})}, 30000);

Note: Times in JavaScript are always given in milliseconds hence the number 30000 being used here.

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: November 23rd, 2021, 1:22 am 
Offline
Senior User
Senior User

Joined: July 23rd, 2007, 2:22 am
Posts: 157
Location: Vancouver, BC, Canada
I'm just getting back into this. Where do I put this code?

I put it in display.header.php as the following to see if I could get it to update, and when I listened to it for 15 seconds with two listening sessions going, it simply stayed at '1'.

<script>
setInterval(() => {fetch('http://parkmagicradio.ca:8000/stats?sid=1&json=1').then((res) => {return res.json()}).then((data) => {document.getElementById('stats').innerHTML = data.currentlisteners;})}, 30000);
</script>
<div id="stats">Listener count: 1</div>

From your code above, I understand it should over-write the "1" the actual number of listeners, and 15000 ms is 15 seconds. I'm not sure having the code in a <script> tag is enough... do I need a Function() as well?

Also, what happens if there are 0 listeners? Should it change to "0", or not display at all?

_________________
Park Magic Radio
http://parkmagicradio.ca


Top
 Profile  
Reply with quote  
PostPosted: November 23rd, 2021, 9:13 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8299
Location: Cologne (Germany)
The code snippet only works if Shoutcast runs on the same hostname, protocol and port number as the website. (i.e.: http://parkmagicradio.ca is your website, then it will only be able to access your streamserver if it's in some kind of "virtual" subdirectory of http://parkmagicradio.ca -i.e.: http://parkmagicradio.ca/live)
It does not work cross site. (Cross-Site includes different port numbers and different protocols - i.e.: https://parkmagicradio.ca vs http://parkmagicradio.ca:8000 are different sites as far as your browser is concerned)

No idea why that ever worked in the first place...


The workaround is to use JSON-P (this is a fancy word for: Create a script tag using javascript and make the browser load the script from the streaming server. Shoutcast will then return a script which basically calls a function somewhere else in your HTML page with a predefined name)

I don't have a code-snippet for that handy, but can look into that later this week if you'd like to. Just please check it in a timely manner and not 1 year later. My memory is not THAT good.

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: November 24th, 2021, 6:59 pm 
Offline
Senior User
Senior User

Joined: July 23rd, 2007, 2:22 am
Posts: 157
Location: Vancouver, BC, Canada
Hi Benedikt, Shoutcast DNAS is running on the same IP server as SAM. If I go to http://parkmagicradio.ca:8000/stats?sid=1&json=1 I can see that "currentlisteners" is "1" but then I connect myself hoping to bring the number up to "2" but after 30 seconds it always stays at "1" even if no one is listening. I have 3 VMs running - shoutcast and SAM on one, MySQL server on one, and my web server. Are you saying that those numbers will never change from "1" if I don't run everything from the same server? Or is my code wrong, above?


Attachments:
Capture.PNG
Capture.PNG [ 6.51 KiB | Viewed 137 times ]

_________________
Park Magic Radio
http://parkmagicradio.ca
Top
 Profile  
Reply with quote  
PostPosted: November 25th, 2021, 6:32 am 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8299
Location: Cologne (Germany)
Same IP doesn't matter. In web context it has to be the same exact site. (site being defined as schema + hostname + port - i.e.: http://parkmagicradio.ca is different from http://parkmagicradio.ca:8000)


Yes, I'm saying your number will never change from 1.
If you press F12 and then select console it should show you the error message.
Something along the lines of Reason: CORS request did not succeed.

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: November 25th, 2021, 10:50 am 
Offline
Senior User
Senior User

Joined: July 23rd, 2007, 2:22 am
Posts: 157
Location: Vancouver, BC, Canada
You're exactly correct! Thanks for the headsup! This explains why this doesn't work for me. Seems I would need to move all three of my VMs to one machine in order to get it to work properly. I'm not prepared to do that, so I can take this off my list of improvements. Heaps of thanks for your time with me on this!

Cheers, mate!


Attachments:
Capture.PNG
Capture.PNG [ 9.15 KiB | Viewed 121 times ]

_________________
Park Magic Radio
http://parkmagicradio.ca
Top
 Profile  
Reply with quote  
PostPosted: November 25th, 2021, 12:28 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8299
Location: Cologne (Germany)
There's no need to consolidate your VMs. You could also just add a ReverseProxy entry to your Apache/Nginx config and have the website VM proxy all requests to the stream server.

And again, there's also a way to get around the CORS limitations and keep your URLs "as is".

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: December 2nd, 2021, 12:40 am 
Offline
Senior User
Senior User

Joined: July 23rd, 2007, 2:22 am
Posts: 157
Location: Vancouver, BC, Canada
Mastacheata wrote:
There's no need to consolidate your VMs. You could also just add a ReverseProxy entry to your Apache/Nginx config and have the website VM proxy all requests to the stream server.


I use Windows 2019 Server and IIS.

Mastacheata wrote:
And again, there's also a way to get around the CORS limitations and keep your URLs "as is".


I'm intrigued... and listening. :)

_________________
Park Magic Radio
http://parkmagicradio.ca


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group