Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
kjg
Visitor

number of viewers query

Hello!
I am new to brightscript and I have run into a snag. I have a working channel (based on Roku- New Video Player) that streams a single hls live stream. this stream is only available a couple of times a week.
I would like to add a screen that would ask viewers to input the number of viewers watching the stream. There is a live stream version that works in a web browser but i cannot get one to send just a count to a text file on our server. The browser version goes to a page that simply states "please enter number of viewers" and then it will display the live stream.
The request would go to a page such as - http://mywebsite:8080/rokuform.html. the web page info is listed below.
Is there a way to implement this for my Roku channel? I have tried a few examples but none worked.
Thanks!!
<html>
<head>
<title>Input Number Of Viewers</title>

<script type="text/javascript">
function processForm()
{
//Get the number of viewers from the entry in the form
var numViewers = document.getElementById("numviewers").value;

if(numViewers.length > 0)
{
if(numViewers.match(/^[0-9]+$/g))
{
//Send the report to the php processor
var xmlhttp = new XMLHttpRequest();
var url = "formprocessor.php";

var data = new FormData();
data.append('customMethod', 'updateCounter');
data.append('numviewers', numViewers);

   xmlhttp.onreadystatechange = function()
   {
       if (xmlhttp.readyState == XMLHttpRequest.DONE )
       {
          if (xmlhttp.status == 200)
          {
              window.location.href = "http://mywebpage.com:8080/live/livestream/index.m3u8";
          }
         
          else if (xmlhttp.status == 400)
          {
             alert('There was an error 400');
          }
         
          else
          {
              alert('something else other than 200 was returned');
          }
       }
   };

   xmlhttp.open("POST", url, false);
   xmlhttp.send(data);
}
}

return false;
}
</script>

<style>
.content
{
position: relative;
}

.formdiv
{
background: #CCC;
border: 1px solid #000;
left: 30%;
height: 150px;
margin: 0 auto;
padding: 0 0;
position: absolute;
text-align: center;
top: 50%;
transform: translateY(100%);
width: 40%;
}
</style>
</head>

<body>
<div class="content">
<div class="formdiv">
<form class="viewerform" method="POST" onsubmit="return processForm();">
<p>Please Enter the Number of People that will be Viewing this Video</p>
<label for="numviewers">Number Of Viewers: <input type="text" name="numviewers" id="numviewers"></label><br><br>
<span class="viewerformbuttonspan"><input type="submit" value="Submit"></span>
</form>
</div>
</div>
</body>
</html>
0 Kudos
27 REPLIES 27
kjg
Visitor

Re: number of viewers query

Just to clarify, this is a private channel. We just want to get an accurate count of people watching so they can be counted in attendance.
If i could just hit the field on the web page for input, then I could proceed.

Any input would be greatly appreciated.!!
0 Kudos
brybott
Visitor

Re: number of viewers query

I don't mean to be rude, but I don't know what your question is.
0 Kudos
NND
Visitor

Re: number of viewers query

It is not clear what problem exactly you're trying to solve. Do you have problem with screen to collect number of viewers? Or is it submitting form to your website? Or something else? If you state your problem more narrowly it will be easier to help you. But in general I don't see any reason why you couldn't implement functionality you've described in your app. 
0 Kudos
kjg
Visitor

Re: number of viewers query

My apologies for not being more specific.
I basically am a novice.
What I would like to do is add a working keyboard or pinpad that asks for a number of viewers to just the HLS stream before it opens. Then that count will be added to a text file on the server handling the hls stream. The numbering file is already in place and working. I am using the NewVideoPlayer by belltown as a source for the hls feed. All other channel functions are not used.
I can get a working keyboard installed in the program by adding it in the Ui script and using the  xml component. I am basically using the Keyboard dialog example from Roku.  But I cannot get it to exit to the stream. Also I am unsure how to get it to send the count to the file. I have provided a copy of the actual script for the Web page version of the counter in my original question, but what is needed to get the channel to do the same? A photo of the web page is here: https://www.dropbox.com/s/jiv627zoy06u2 ... n.jpg?dl=0

So how do I mimic the web page and send the count to it from the script I am using for Roku since Roku devices don't do html?
I might not even have the keyboard in the right place in the files.
Forgive my ignorance. If I can provide any more specifics, please let me know.
This is thus far been a learning adventure for me and I would like to continue with it.
Thank you for your replies.
I do appreciate it.
0 Kudos
kjg
Visitor

Re: number of viewers query

Well I finally got back to this. I have made some progress but I am stuck at my count screen. The pin pad loads,and goes no further. If I remove the "Wait for message" , the pin pad will load momentarily and then move on to the live hls stream. I must not be calling for the return correctly.
there are no errors showing in the debug console.
Any input would be greatly appreciated.
I do not as of yet have it set up to place count in the text file that we have. But, one step at a time.

Thank You!!!

code removed since i am now using new code
0 Kudos
kjg
Visitor

Re: number of viewers query

Had shelved this for a bit but got back to it.
I now have a working channel after completely redoing it. However, I am getting a 405 error when sending my count to the server and I am hoping someone would take a look at the code I am using and see what I might be doing wrong. Yes, I'm a novice. But I have done quite a bit of reading and now have a working channel. I just need to finish this up  on the viewer counter and I can put it out.
I am only showing the transfer task. If other code is needed, I will show it too.
Thanks!!
<?xml version="1.0" encoding="utf-8" ?>
<component name="NumTransfer" extends="Task">
    
  <script type="text/brightscript">
    <![CDATA[
sub init()     
        m.top.functionName = "go3"
      end sub

    sub go3()
timeout% = 0
    roRequest = CreateObject("roURLTransfer")    
    roRequest.setUrl("http://mypage.com:8080/rokuform.html")
    print "In NumTransfer"  
    
    roPort = CreateObject("roMessagePort")  
    roRequest.setPort(roPort)                 
    if (roRequest.AsyncPostFromString(m.global.NumViewers))
            roEvent = wait(timeout%, roRequest.GetPort())         
            if type(roEvent) = "roUrlEvent"
                str = roEvent.GetString()              
                print roEvent.GetResponseCode()              
            else if roEvent = invalid
                roRequest.AsyncCancel()               
            else
                print "roUrlTransfer::AsyncPostFromString(): unknown event"
            endif
        end if
    
'_____Return from Post_________
     DeviceInfo  = CreateObject("roDeviceInfo")
        IPAddr      = DeviceInfo.getIPAddrs()
        transfer    = createObject("roUrlTransfer")
        for each item in IPAddr
        
          url       = "http://" + IPAddr[item]  + ":8060/keypress/Back"
          transfer.setUrl(url)
          transfer.PostFromString("")
          
          url       = "http://" + IPAddr[item]  + ":8060/keypress/Back"
          transfer.setUrl(url)
          transfer.PostFromString("")
        end for
    end sub
]]>
    
  </script>
   
  </component>



0 Kudos
EnTerr
Roku Guru

Re: number of viewers query

You are getting HTTP 405 - Method Not Allowed from the server you are trying to POST the form to. Looking that the address is in the form of "http://mypage.com:8080/rokuform.html", that's be expected - typically for .html the server does not expect to receive POST requests but GET ones. You need some kind of server-side script to accept the posted information - and if you did, the URL would've looked something like .../rokuform.php or /rokuform or such
0 Kudos
kjg
Visitor

Re: number of viewers query

Thank you for the quick response. I will look into it and have it added.
I will post back as soon as I have something.
0 Kudos
kjg
Visitor

Re: number of viewers query

Well, that was not too bad. I added "roRequest.setUrl("http://mypage.com:8080/formprocessor.php") "
and now get a 200 which is ok. so now it is getting the data. however, it also lists the string as  "Incorrect Call"
What have I missed here?

here is info from roku device:

M.TOP.DIALOG.PIN 3
m.global.NumViewers = 3
m.top.dialog.buttonSelected =  0
In NumTransfer
after setting the port
roEvent <h2>Incorrect Call</h2>
NumViewers =3
I am Transferring NumViewers
 200
str = <h2>Incorrect Call</h2>
In OnKey Event
In OnKey Event
In OnKey Event
key pressed =back
in Show Dialog
you are here! in SG Function Main not in While
button Selected:  0
In Exiter
In AppExiter
in SimpleVideoScene.xml onKeyEvent down false
in SimpleVideoScene.xml onKeyEvent OK false


Thanks again!!!!
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.