Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Webserver

Questions and comments specific to a particular plugin should go here.
eirik226
Experienced User
Posts: 142
Joined: Wed Nov 07, 2012 5:22 pm

Re: Webserver

Post by eirik226 »

Thank you! This helps, it's actually because of you I'm asking, haha.

You recall that simple_list.html you sent me? That updates a list, i think it was based on weather data on your end I think.

I just can't figure out how you sort it, how you know whats the newest added data and put it on the top again. I've heavily modified it to fit my needs but the sorting part really got me, so i thought I could add time stamps from python and sort it in JS that way. Here is the JS i'm using anyway, this is based on yours actually:

Code: Select all

					<script type="text/javascript">
ws = new WebSocket("ws://" + location.host);
recent_downloaded_shows = [];

ws.onopen = function(event) {
    console.log("Web Socket opened.");
}

ws.onmessage = function(event) {
    recent_downloaded_shows.push(event.data);

    if (recent_downloaded_shows.length > 3) {
        recent_downloaded_shows[0] = recent_downloaded_shows[1];
        recent_downloaded_shows[1] = recent_downloaded_shows[2];
        recent_downloaded_shows[2] = event.data;
        recent_downloaded_shows.splice(2, 1);
    }

    document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
}

ws.onclose = function() {
    console.log("Web Socket closed.");
}

ws.onerror = function(error) {
    console.log("Web Socket error: " + error);
}
</script>
Could you explain a bit how you sort it? I just don't get how you do it based on your JS file.

And thank you for the code you provided!
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Webserver

Post by krambriw »

Hi, I am not sure what is your problem. I think the code you wrote is somehow functioning. I made a very simple test-page, maybe you can try it and then explain what you would like to change?

Code: Select all

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

<title>Simple websocket test page</title>

<script language="javascript">
wSocket = null;
prot = "";
ip = "";
url = "";
recent_downloaded_shows = [];


	  function init() {
		    // event handlers for WebSocket:
        wSocket = null;
        prot = "";
        ip = location.host;
        if (location.protocol == "https:") {
            prot = "wss://";
        }
        else {
            prot = "ws://";
        }
        url = prot + ip; //enough for Webserver
		    //url = prot + ip + "/ws"; //mandatory for Tornado
		    wSocket = new WebSocket(url);
		    wSocket.onopen = function() {
            document.getElementById("log").style.backgroundColor ="green"; //Green
		    }
		    wSocket.onmessage = wsMessage; 
		    wSocket.onclose = function() {
            document.getElementById("log").style.backgroundColor ="#C0C0C0"; //Grey
		    }
	  }


    function wsMessage() { 
        recent_downloaded_shows.push(event.data);
        if (recent_downloaded_shows.length > 3) {
            recent_downloaded_shows[0] = recent_downloaded_shows[1];
            recent_downloaded_shows[1] = recent_downloaded_shows[2];
            recent_downloaded_shows[2] = event.data;
            recent_downloaded_shows.splice(2, 1);
        }
        document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
    }
    
    
</script> 

<body id="log" onLoad="init()" bgcolor=black text=white link=white vlink=white alink=white </body>
eirik226
Experienced User
Posts: 142
Joined: Wed Nov 07, 2012 5:22 pm

Re: Webserver

Post by eirik226 »

krambriw wrote:Hi, I am not sure what is your problem. I think the code you wrote is somehow functioning. I made a very simple test-page, maybe you can try it and then explain what you would like to change?

Code: Select all

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

<title>Simple websocket test page</title>

<script language="javascript">
wSocket = null;
prot = "";
ip = "";
url = "";
recent_downloaded_shows = [];


	  function init() {
		    // event handlers for WebSocket:
        wSocket = null;
        prot = "";
        ip = location.host;
        if (location.protocol == "https:") {
            prot = "wss://";
        }
        else {
            prot = "ws://";
        }
        url = prot + ip; //enough for Webserver
		    //url = prot + ip + "/ws"; //mandatory for Tornado
		    wSocket = new WebSocket(url);
		    wSocket.onopen = function() {
            document.getElementById("log").style.backgroundColor ="green"; //Green
		    }
		    wSocket.onmessage = wsMessage; 
		    wSocket.onclose = function() {
            document.getElementById("log").style.backgroundColor ="#C0C0C0"; //Grey
		    }
	  }


    function wsMessage() { 
        recent_downloaded_shows.push(event.data);
        if (recent_downloaded_shows.length > 3) {
            recent_downloaded_shows[0] = recent_downloaded_shows[1];
            recent_downloaded_shows[1] = recent_downloaded_shows[2];
            recent_downloaded_shows[2] = event.data;
            recent_downloaded_shows.splice(2, 1);
        }
        document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
    }
    
    
</script> 

<body id="log" onLoad="init()" bgcolor=black text=white link=white vlink=white alink=white </body>
Hello Krambriw,

Sorry for the late reply, I have been really busy with studies lately, thankfully done now.

First of all, I don't get your code to work, think I found out the problem tho'.

Well, we have talked about it before, but what I am trying to do is that I get events like this for example:

Code: Select all

PushBullet.Note.DownloadFinished [u'White Collar - 5x11 - Shot Through the Heart', None, u'ujAdLjXAeDksjAdxAd0gbA', '', '2015-10-22 01:17:17']
Then I use

Code: Select all

Webserver.BroadcastMessage(u'{eg.event.payload[0]}', False)
to get the payload to my webpages and I have the code I past last time, this one:

Code: Select all

<script type="text/javascript">
ws = new WebSocket("ws://" + location.host);
recent_downloaded_shows = [];

ws.onopen = function(event) {
    console.log("Web Socket opened.");
}

ws.onmessage = function(event) {
    recent_downloaded_shows.push(event.data);

    if (recent_downloaded_shows.length > 3) {
        recent_downloaded_shows[0] = recent_downloaded_shows[1];
        recent_downloaded_shows[1] = recent_downloaded_shows[2];
        recent_downloaded_shows[2] = event.data;
        recent_downloaded_shows.splice(2, 1);
    }

    document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
}

ws.onclose = function() {
    console.log("Web Socket closed.");
}

ws.onerror = function(error) {
    console.log("Web Socket error: " + error);
}
Now as you said, this code it works, somewhat okay. But I am trying to add better sorting and adding.

I wish to populate a list or something like that, and make the latest downloaded show to move one down when the next downloaded show is in.

Like this:

- New show (3) (this is the newest show)
- New show (2) (second newest)
- New show (1) (third newest)

-->

- New show (4) (now this is the newest show and get added to the top of the list)
- New show (3) (this gets pushed down one)
- New show (2) (same with this)

new show (1) is now out of the picture.

So, what I am having problems with is proper sorting and adding. And I've taken a look at a simple_page.html you sent me ages ago with that sort of functionality, but I just can't manage to adopt it to my needs. The code I have now basically works fine and it's no bother of me, but I'd just love to be able to add some sort of logic to this, if you understand what I mean.

Edit: I am not locked down to HTML, can do this in python as well I guess, I just don't know how yet. Because as I have figured this will also include every other update broadcast I do, so if I for example do a "Broadcast_message: TV IS ON" - TV IS ON will also be added to the list.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Webserver

Post by krambriw »

I have been really busy with studies lately, thankfully done now.
The same for my son, he came home from Lund yesterday, totally exhausted after a lot of work this period since summer vacation ended. So I can Imagine ;)
but I'd just love to be able to add some sort of logic to this, if you understand what I mean
OK, but you say it is sorting & working kind of correctly. I think my sample is sorting but in the opposite order (newest comes from bottom and moves up to top before it disappears)

So if you wanna reverse the order, newest on top and pushing down the older ones a step, this code should work (I hope)

Code: Select all


    function wsMessage() { 
        recent_downloaded_shows.unshift(event.data); //adding one element in the beginning, pushing older back
        if (recent_downloaded_shows.length > 3) {
            recent_downloaded_shows.pop(); //removes oldest/last element
        }
        document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
    }


BR
eirik226
Experienced User
Posts: 142
Joined: Wed Nov 07, 2012 5:22 pm

Re: Webserver

Post by eirik226 »

but I'd just love to be able to add some sort of logic to this, if you understand what I mean
OK, but you say it is sorting & working kind of correctly. I think my sample is sorting but in the opposite order (newest comes from bottom and moves up to top before it disappears)

So if you wanna reverse the order, newest on top and pushing down the older ones a step, this code should work (I hope)

Code: Select all


    function wsMessage() { 
        recent_downloaded_shows.unshift(event.data); //adding one element in the beginning, pushing older back
        if (recent_downloaded_shows.length > 3) {
            recent_downloaded_shows.pop(); //removes oldest/last element
        }
        document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
    }


BR
I just tried this, and to quote you this did the exact same as "I think my sample is sorting but in the opposite order (newest comes from bottom and moves up to top before it disappears)".

I'm really bad with JS, but I figured out how to add it to a file tho:

Code: Select all

outfile = open('D:\\Eventghost Webserver\\nedlastet.txt', 'a')
outfile.write(eg.event.payload[0] + "\n")
outfile.close()
Maybe it's easier to use a JS to populate a list from that .txt file? Make it read from the bottom or whatever since all the shows added are on a new line. So the newest will always be on the bottom. I got no idea tho, just a thought.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Webserver

Post by krambriw »

I don't get it, what is not working???

When I run it, the newest is on top and gets pushed down if a new arrives. Was that not what you asked for?
eirik226
Experienced User
Posts: 142
Joined: Wed Nov 07, 2012 5:22 pm

Re: Webserver

Post by eirik226 »

krambriw wrote:I don't get it, what is not working???

When I run it, the newest is on top and gets pushed down if a new arrives. Was that not what you asked for?
oh, thats odd. Thats not at all whats happening here, on my side it's newest on the bottom and then they move up, so the opposite of what you said, I even tried it with your new script. Might be something wrong with my code then.

So the first 3 added works, but when the 4th and 5th etc starts coming in it's in reverse. I dont know how I can explain it in any other way, i can make a video or a gif maybe :P

Edit: Forgot to add the code.

Code: Select all

<script language="javascript">
wSocket = null;
prot = "";
ip = "";
url = "";
recent_downloaded_shows = [];


     function init() {
          // event handlers for WebSocket:
        wSocket = null;
        prot = "";
        ip = location.host;
        if (location.protocol == "https:") {
            prot = "wss://";
        }
        else {
            prot = "ws://";
        }
        url = prot + ip; //enough for Webserver
          //url = prot + ip + "/ws"; //mandatory for Tornado
          wSocket = new WebSocket(url);
          wSocket.onopen = function() {
            document.getElementById("log").style.backgroundColor ="green"; //Green
          }
          wSocket.onmessage = wsMessage; 
          wSocket.onclose = function() {
            document.getElementById("log").style.backgroundColor ="#C0C0C0"; //Grey
          }
     }


		function wsMessage() { 
			recent_downloaded_shows.unshift(event.data); //adding one element in the beginning, pushing older back
			if (recent_downloaded_shows.length > 3) {
				recent_downloaded_shows.pop(); //removes oldest/last element
			}
			document.getElementById("log").innerHTML = recent_downloaded_shows.join("<br />");
		}
    
    
</script> 
<body id="log" onLoad="init()" bgcolor=black text=white link=white vlink=white alink=white </body>
This is what I am using atm, I dont get whats wrong.

EDIT: I might have found out whats wrong, and it's really stupid if I am correct. Will test and see.

EDIT 2: Yes, I found the error, and it was as I said, really stupid. Turns out i've been editing and old index.html and not the right one, so there's that. Sorry for all your troubles.
Anyway, it works correctly now, thank you!
coricidin
Posts: 6
Joined: Sat Dec 06, 2014 8:41 pm

Re: Webserver

Post by coricidin »

Does anyone one know how the webserver plugin manages to replace anything in double brackets in an html file (for example, {{variable}}) with EG variables? I'm trying to do the same thing with python but outside of eventghost and I can't seem to find the part of the webserver plugin code that does that.

Basically I have an html file that I wrote for the EG webserver plugin that has lots of {{eg.globals.variables}} in it, and now I'm trying to write a python script that replicates the variable replacement functionality so that I can run the html file on a non-EG webserver. Any help is appreciated. Here is an illustration of the question:

Example "file.html":

<body>
<p>My variable is {{AnyVariable}}</p>
</body>

Example "python.py":

AnyVariable = "value"
(some code here that will replace every instance of {{xxx}} in "file.html" with python variable "xxx")
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Webserver

Post by Pako »

coricidin wrote:Does anyone one know how the webserver plugin manages to replace anything in double brackets in an html file (for example, {{variable}}) with EG variables? I'm trying to do the same thing with python but outside of eventghost and I can't seem to find the part of the webserver plugin code that does that.
It is very simple.
You do not need a file python.py !
Maybe that's why you have a problem with that.
Webserver_variable_demo.png
Webserver_variable_demo.png (3.98 KiB) Viewed 6070 times
index.html content:

Code: Select all

<!DOCTYPE HTML>
<html>
  <head>
    <title>EventGhost - variable demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="refresh" content="5"> 
  </head>
  <body>
    <p>
       Global variable "eg.globals.xxx":&nbsp;&nbsp;&nbsp;&nbsp; <b><font color="green"><span style="font-size: 150%">{{xxx}}</span></b></font>
    </p>
    <p>
       Webserver temporary variable "yyy":&nbsp;&nbsp;&nbsp;&nbsp; <b><font color="red"><span style="font-size: 150%">{{yyy}}</span></b></font>
    </p>
    <p>
       Webserver persistent variable "zzz":&nbsp;&nbsp;&nbsp;&nbsp; <b><font color="blue"><span style="font-size: 150%">{{zzz}}</span></b></font>
    </p>
  </body>
</html>
Result in a web browser:
Webserver_variable_demo_result.png
Webserver_variable_demo_result.png (6.64 KiB) Viewed 6070 times
Edit:
I'm sorry. Only now I read the whole post properly.
You ask for something else.
I can advise you a little bit. I do not know what server you're using.
In the code is necessary to look for keywords such as environment, template, FileLoader.


Pako
You know flattr ? You can Image
wysocki
Experienced User
Posts: 65
Joined: Mon Nov 23, 2015 9:23 pm
Location: Los Angeles area

Re: Webserver

Post by wysocki »

krambriw wrote:...read the thread about how to add web-socket support and how to use a persistent variable....BR
Been over a year so I'm sorry to quote you on this, but I'm a newb in need of some serious help! Just WHERE is this fabled thread about using websocket and variables? I've dug through all the docs, searched for hours and read a ton of posts, but I still can't find any "guide" to using websockets or variables. I've graduated from doing href= GETs in the webserver to actually posting events with javascript (yay!) thanks to one sample I found on the downloads page. But now I'd like to get some status info on my pages. I know that Pako is really loaded with the technical end of the product, but someone really needs to do some real documentation to get people to use it. I've spent hours poring over posts that ended up being obsoleted. I really love EG and I'd love to see it succeed beyond the relatively small group of users that are on these forums. But I can understand why some people just give up.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Webserver

Post by krambriw »

But I can understand why some people just give up.
Yeah, it sometimes is hard. I do not know what was the old goals for EventGhost but I think it has developed substantially during the last five years (except for documentation). Personally, I do think the "original and initially targeted" functionality for EventGhost is reached and rather well documented. Like controlling media and other applications using a remote. But then you have the more advanced part that the framework allows. Here you must posses some python and javascript coding skills, or at least, some understanding of the code so you can do "selected copy & paste".

This part of the scene is, except what is written in the related threads, not well documented, partly because limited time. The advanced stuff takes a considerable time to develop and the hope was that other users would spend some of their time to help document "things" in a more user friendly way. The idea of sharing. But this rarely happens even though it would be most efficient to let the few developers focus on the functionality instead of guidelines.

Well, anyway, back to your specific request (I did not find that quote either). Earlier, when that quote was written, the websocket support was provided by a separate plugin named Websocket Suite. This is now obsolete since Pako added this functionality to the Webserver plugin itself. The only plugin you therefore need to get websocket support in you configuration is the latest Webserver plugin.

What I further understand is that you need a description on how to use websockets to update status info in a page? I think I do have some sample (just need to check around) that you can start of with but to make it fit your needs and make it look nice, you will for sure have to learn javascript & html basics.

Kind regards,
Walter
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Webserver

Post by krambriw »

The attached zip contains a web page sample (simple-list.html). It is just a simple logger that will show incoming messages.

1) Put the html file into the Webserver plugins 'HTML documents root'

2) Then open the page in a browser: your ip:port/simple_list.html

3) Use some actions to broadcast messages via websockets and check what is happening in the browser. You can do this manually using broadcast actions. Here is a sample showing how I also use events from a RFXtrx device to be sent.
Skärmklipp.JPG
simple_list.zip
(1.43 KiB) Downloaded 170 times
Sem;colon
Plugin Developer
Posts: 625
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: Webserver

Post by Sem;colon »

Hey together,

I just improved the webserver plugin a bit again; added support for "prefix" as keyword argument on POST requests for the method TriggerEnduringEvent.
Attachments
__init__.py
v3.9
(192.33 KiB) Downloaded 151 times
If you like my work, Image me a drink :wink:
bongolf
Experienced User
Posts: 58
Joined: Tue Jan 19, 2010 12:37 pm

Re: Webserver

Post by bongolf »

krambriw wrote:The attached zip contains a web page sample (simple-list.html). It is just a simple logger that will show incoming messages.
I have tried this example and it works great, but you dont happend to have an simple example of using websockets to change variables also?
Right now I am just using eg.globals in my html-files but it would be nice to get instant updates.

I have tried Pako┬┤s example using "Webserver: Set temporary value" but I cant see any difference in thr result from using eg.globals (The webpage still has to be reloaded to see the updated values).

Any advice would be much appreciated.
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Webserver

Post by krambriw »

This simple sample sends and receives data using websockets. Copy & Paste into an html-document and put it into the webserver root

Code: Select all

<html lang="sv">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<title>TV - styrning (exempel)</title>
<!-- comment -->

<style type="text/css">
    .on { 
        border:4px inset;
        color:#369;
        background:#f9d543; 
    }
        
    .off {
        border:4px outset;
        color:#369;
        background:#f9d543; 
    }
</style>

<script type="text/javascript">
wSocket = null;
prot = "";
ip = "";
url = "";


    function init() {
    //  event handlers for WebSocket:
        wSocket = null;
        prot = "";
        ip = location.host;
    
        if (location.protocol == "https:") {
            prot = "wss://";
        }
        else {
            prot = "ws://";
        }
    
        url = prot + ip + "/ws"; //mandatory for Tornado
        //url = prot + ip; //enough for Webserver
        wSocket = new WebSocket(url);
        wSocket.onopen = function() {
        	document.getElementById("theBody").style.backgroundColor ="green"; //Green
        }
        wSocket.onmessage = wsMessage;
        wSocket.onclose = function() {
    	    document.getElementById("theBody").style.backgroundColor ="#C0C0C0"; //Grey
        }
    }

    
    function doSend(strng) {
        if (wSocket.readyState == 1) {
        	wSocket.send(JSON.stringify(strng));
        }
    }

    
    function TriggerWebSocketEvent(eventname, el){
        if(el.className == "on") {
            el.className="off";
            switch (eventname) {
              case 'TV_TOGGLE':  
                  document.getElementById("tv.is.what").style.backgroundColor="#black";
                  document.getElementById("tv.is.what").style.color ="black";
                  document.getElementById("tv.is.what").className = "off";
                  document.getElementById("tv.is.what").style.fontSize ="25";
                  document.getElementById('tv.is.what').src = "/images/tv_off.PNG";
                  doSend('TV_OFF');
                  break; 
              default: doSend(eventname+'_OFF');
            } 
        } else {
            el.className="on";
            switch (eventname) {
              case 'TV_TOGGLE': 
                  document.getElementById("tv.is.what").style.backgroundColor="#black";
                  document.getElementById("tv.is.what").style.color ="white";
                  document.getElementById("tv.is.what").className = "on";
                  document.getElementById("tv.is.what").style.fontSize ="25";
                  document.getElementById('tv.is.what').src = "/images/tv_on.PNG";
                  doSend('TV_ON');
                  break; 
              default: doSend(eventname+'_ON');
            } 
        }
        return false;
    }


    function wsMessage(event) {
        document.getElementById("theBody").style.backgroundColor ="black"; //Black
    }

</script>

<body id="theBody" onLoad="init()" bgcolor=black text=white link=white vlink=white alink=white />
<center>
<table border="2" width="100%" height="85%">
  <tr><!-- A row of buttons -->
        <td><img src="/images/tv_off.PNG" alt="Slå på TV" width="80" height="60" id="tv.is.what" class="off" onMouseDown="TriggerWebSocketEvent('TV_TOGGLE', this)" ></td>
        <td></td>
        <td></td>
        <td></td>
  </tr>
</table>
</center>
</body>

Post Reply