Page 1 of 1

How to obtain a webserver variable's value from webpage?

Posted: Wed Jul 06, 2016 6:27 pm
by caffeinatedcoder
Hi everyone! :) This is my first time posting, although I've been observing for awhile now. My issue/inquiry is in reference to the Webserver plugin. I have a webserver running locally and are port-forwarding it so I can access it online. On the (simple) webpage I have up, there is a button that when clicked sends an HTTP request to EG and runs a few scripts. What I've been looking to do is when I click the button I would like Eventghost to somehow be able to send me a response in some (any) way letting me know what percentage of the macro is complete. My original thought/idea was to have a progress bar appear maybe below the button and fill it as each process completes. However, after aimlessly scouring search engines and this forum I haven't come across a solution that matches what I'd like to do. The best I got was a vague idea of how I might be able to do it:

1. Clicking the button on the webpage > sends HTTP request > triggers macro that runs scripts
2. As each script completes, update a Webserver temporary variable (scriptProgress) by increments of 5 until it reaches 100 (when all the scripts have been run)
3. Each time a script complete, have EG return a JSON string containing the scriptProgress' updated value > Update the progress bar on the webpage accordingly.

Is this even possible though? I've also heard about doing Ajax calls to accomplish something like this, but am clueless about Ajax :shock: As is probably obvious, I'm completely new to EG and have only been messing with it for a few weeks in my free time. If what I've explained isn't possible is there any other way that you could suggest that would help me achieve the same result (or close to it)? Examples of how to make this happen would be greatly appreciated.

Here's what I have so far:
1. I'm able to trigger a macro by making the HTTP call. I know it works because it shows up in the EG Log and also the popup is produced on my main computer
2. I think I figured out how to create and adjust a temporary webserver variable's value
Image
3. As I stated above, I have a simple mock website with the following html

Code: Select all

<html>
  <head>
    <title>Action Button</title>
	<style>
		body{
			background-repeat:no-repeat;
			background-attachment:absolute;
			width: 100%;
			height: 100%;
			margin: 0px;
			padding: 0px;
			background-size: 100% 100%;
		}
		#top{
			width:100%;
			height:38%;
			margin: 0 auto;
		}
      	.image-blurred-edge{
        	opacity:.5;
			width: 40%;
			height:100%;
			background-image: url('http://www.finedesigndine.us/wp-content/uploads/2012/03/exterior.jpg');
            background-size: 100% 100%;
			background-repeat:no-repeat;
			display:block;
			margin-left: auto;
        	margin-right: auto;
        	box-shadow: 0 0 8px 8px rgba(255, 255, 255, 0.5) inset;
			-moz-box-shadow: 0 0 8px 8px rgba(255, 255, 255, 0.5) inset;
			-webkit-box-shadow: 0 0 8px 8px rgba(255, 255, 255, 0.5) inset;
      	}
		
	</style>	
  </head>
  <body background='http://i.imgur.com/Gj7b3Ms.jpg'>	
	<div id="content">
      <div id="top"><div class='image-blurred-edge'></div></div>
		Ready on your command<br>
		<a id='startscripts' href="eventghost.html?nuke">
			<img border="0" src="http://img-aws.ehowcdn.com/640/cme/photography.prod.demandstudios.com/dcfb57aa-671d-4a56-af65-005280736c15.jpg" />
		</a>
	</div>
  </body>
</html>
It's not supposed to be anything fancy; especially since my wife and I are the only ones who are going to be using it from our home to run scripts on our computers in the office. The idea is to help avoid our employees from having to touch (and inevitably screw up) the server when there are issues.

Re: How to obtain a webserver variable's value from webpage?

Posted: Thu Jul 07, 2016 3:29 am
by kgschlosser
anything is possible with eventghost. tho it would be easier if you had EG and the webserver plugin actually hosting the website. but what you would do is where ever you want to is to use the webserver action "Send event to another webserver"

the event piece of the action is just like doing the ?blahblahblah after the url. so you would be able to enter a username and password if necessary as well as your server url. and then use the event field to fill in the bits after the url including the variablename to set and what to set it to.

if you have websockets set up on your server you could use websockets to do it also.

Re: How to obtain a webserver variable's value from webpage?

Posted: Thu Jul 07, 2016 3:41 am
by kgschlosser
it is a little confusing by your description exactly what is doing what and when.

but this is what i get from it, tell me if i am right


you have a webserver running on a different computer. and it hosts a site that has a button that when clicked it will send an http message to another computer running eventghost on it. and in eventghost an event will show. i am assuming this computer that is running eventghost is the "server". and that event triggers a macro that runs a script. what i do not know is. can the script be broken up into sections???


because if it can be then great. then you would have to break the script into pieces at the percentage breakpoints you want. and have multiple actions one to each of the script pieces. and in between then put the above mentioned action in to send off the information you need to have changed.

i do not know the html code side of things to have it update a variable. but i am sure something like that should easily be found with google.

if the script that runs has output into say a command line window. you could use the stdout to read what is in the command windows but that is python coding and alot more involved. if you are not able to break your script apart then that is something that you would have to do at a python code level.

Re: How to obtain a webserver variable's value from webpage?

Posted: Thu Jul 07, 2016 1:05 pm
by caffeinatedcoder
Thanks for the replies kgschlosser. I'm sorry I wasn't as detailed as I should've been with my original post. I am actually hosting the site with EG and the Webserver plugin from the computer in the office. I then use a localtunnel service to make it accessible from the Internet by visiting a specific url, which makes it possible for me to access the website (with my credentials of course) via a typical URL. I do not have EG running on my home computer, but I believe that I don't really need to have it on the computers I'm using to access the localtunneled site. I've been digging even deeper into the forums and came across one post a few pages into the official Webserver thread when it was first put up. In it, I found the following code that allows me to get the variable's current value

Code: Select all

$.ajax({
           type: 'POST',
           url: "",
           dataType: 'JSON',
           contentType: "application/json;charset=utf-8",
           data: JSON.stringify({'args':["scriptProgress"],'method' :'GetValue'}),
           cache: false,
           success: function (jsonData) {
                alert("scriptProgress = "+jsonData);  
           },            
        });
Using this I am able to get the value of the variable (in this case scriptProgress) and then fooling around a bit more with ajax commands I figured out how to trigger an HTTP.Event without having the page change. Like so

Code: Select all

$.ajax({
				type: 'POST',
				url: "",
				dataType: 'JSON',
				contentType: "application/json;charset=utf-8",
				data: JSON.stringify({'args':["nuke"],'method' :'TriggerEvent'}),
				cache: false,
				success: function (jsonData) {
					alert("Event triggered"); 

				}            
			});
So, now that I figured out how to get the variable's value and trigger the HTTP.Event that starts the scripts I just need to figure out

1. How to produce (and update) a progress bar on the webpage.
2. How to make the two above ajax calls in one go instead of having to make two separate calls. If that's at all possible it'd be nice.

To answer your question, yes, the script can be broken up. So, that will allow for easy incrementing of the variable. However, I'm now stuck with how to produce and update the progress bar until it's value is 100%.

Re: How to obtain a webserver variable's value from webpage?

Posted: Thu Jul 07, 2016 5:46 pm
by kgschlosser
ok breaking apart the script is a HUGE piece of it. because at each step you can use the python command action to change a variable. to the desired percentage, and then trigger the webserver plugin to update the variable. you would have to have some sort of automatic refresh going on the website. this is where it gets out of my realm of knowledge.

http://eventghost.net/forum/viewtopic.p ... ist#p38230

that forum post contains an html file that was made by krambriw. called simple_list.html that i think will be very helpful to you and get you going in the right direction. it uses websockets and since you are hosting the site from eg it will be easier to go that route to achieve what you want,

K

Re: How to obtain a webserver variable's value from webpage?

Posted: Fri Jul 08, 2016 1:29 pm
by caffeinatedcoder
Thanks for the link! I have looked into using that approach, but it appears that might not be a viable solution for me from what I've gathered. I've been attempting to connect via websocket, but I typically get a '502 error' during the handshake. I expect this is because of the service I'm using to tunnel my local port. I'm using a utility called localtunnel since it allows me to consistently obtain the same url (unlike ngrok, which provides a randomly generated link each time unless you pay). Let me break it down for anyone who reads this, so they know how I'm going about my whole process.

1. EG starts Webserver plugin on Startup > at this point site is accessible via 'http://localhost' or 'http://ip address'
2. After Webserver has started, EG runs command line command to start forwarding site through the localtunnel utility on port 8090 > now site is available via url 'http://WhateverSubdomainIPassToLocalTun ... ltunnel.me' from any device connected to the internet in the world.
3. Upon clicking the button on the website ajax commands triggers EG to create a temporary webserver variable and sends the HTTP.Request that triggers the scripts to start running.
4. Now I'm looking at how to update and return the temporary webserver variable back to the client/website end and update a progress bar to show the progress of the scripts.

Since I can't seem to get Websockets working using my current structure (most likely due to the localtunnel utility), it looks like I'll have to look into doing continual chronological ajax calls while waiting until the percentage variable has reached 100. Any other ideas or insight from anyone would be much appreciated as always! Like I said, I'm a novice at this, so I could be not seeing a very easy avenue of which I have no knowledge of at this point.

Re: How to obtain a webserver variable's value from webpage?

Posted: Fri Jul 08, 2016 3:18 pm
by krambriw
To use websockets, you will have to allow the websocket client to connect to the websocket service. It might be that your tunnel is blocking the port needed (I think is 1234). You should be able to add some lines of javascript code in the browser html to debug (or start to open the console in Chrome)

WIth a proper VPN solution, you would not have this problem and websockets will for sure work (I have such a solution myself based on OpenVPN as clients and a VPN server with certificates running under Linux in a Raspberry Pi to access my network from internet). But I think Windows Server software has support for VPN included.

Re: How to obtain a webserver variable's value from webpage?

Posted: Fri Jul 08, 2016 7:42 pm
by kgschlosser
oh yes it sure does have support for vpn and if you like being bald then go for it.

My question is why are you creating a tunnel for just what would be a simple port forward (really a port redirection)?
You can host the page on any port you like and use your router to do a port rediection from 80 to the specific port you are using for the http server end of things. And you also have to unblock the websockets port on the firewall. i am pretty sure you know to do this but maybe didn't know the websockets use their own port and as krambriw stated is 1234. so make sure you change the windows firewall to allow this port in both directions. you can also do a redirection for the websockets port if you want the internal port to differs from the external. if your concern is security. i know you already stated it has a password.

To set up the vpn on windows server can get rather complicated. and if what you want is a stable dns name if you are not using windows server essentials. then use a dynamic dns provider. i personally use ChangeIP as my provider it's nice and easy to set up. and since you have eventghost webserver plugin installed it's quite simple to set a scheduled timer using schedulghost plugin so it will update the dynamic dns once a day via the send a event to another webserver. they have a nice and easy method of updating the information which is just a url with you username and password in it and it will autodetect the ip. and there is no need to install any software from the dynamic dns company.

some something like a business if i didn't want to try and get a static ip address (if you even can now) and then link my domain DNS for a specific A record to the dynamic one. that way everything moves together if you don't have a static IP. i am taking a stab at the reason for using the VPN tunnel for this. but in order to have less issues with setting up a vpn tunnel on windows server you really need to have the server exposed to the external network (wan) via a 2nd network card. otherwise it can become a real nightmare trying to get things to work properly with throwing a router in the mix. you can do yourself a huge favor and save a whole lot of hair on your head and get a Linksys LRT-224 or LRT-222

the Linksys piece was actually made before they got sold to Belkin. and is actually a Cisco piece rebranded and Cisco used to sell the exact same thing. it's a dedicated router with no WiFi but is built for business it internally handles all the encryption for every kind of VPN type available all the way up to 4096 bit encryption. and can handle 50 tunnels as well as site to site tunnels. with the dynamic DNS built in and dual WAN ports that you can set for failover or load balance.

or you can go and get a really expensive Cisco VPN router. but this is the only one i know of that has been designed for home/small business use that is not built for home use. and made properly. I know everyone has had or is having the issues of constantly rebooting their internet connection and internet gateway(modem). and what is not realized is there are 65,554 ports to a single ip address. that means there could be 65554 connections to a single ip address. and people think that the router they buy that does the WiFi and all the rest of the stuff built in can do that. NOPE it. has a heart attack and then binds up and you have to unplug it. most can't even handle 200 connections. (point of fact torrents). i have personally had over 1000 connections to multiple ip address running through mine. without even a slow down.

It's not very expensive retail is little over 200 but i know you can look about and find it for maybe 150. that is a really cheap price for sanity. it a very solid piece i think i have had to reboot this thing one time in the last year and that was due to a goof from my internet provider so I just restarted everything. it's one of those set it and forget it things.

but it takes all the headaches out of VPN Setup and is compatible with OpenVPN. I personally believe for any small company (not sure how large your company i am just assuming here) it's a must have. i don't like putting all my cookies into a single jar. and this also provides a means if needed to restart the server remotely without dumping your connection. and you do not have to rely on a 3rd party company to handle the VPN.

But if you want to go the route of a 3rd party company i would use hamechi over any of them for it. it has been a long while since i have had the need to use a 3rd party company for such a thing so i am just giving a suggestion. do the research first and set up on a test machine. but worth a shot for ya.

actually here is the link for that router from newegg business

https://www.neweggbusiness.com/Product/ ... AgeM8P8HAQ

just under 160.00

K

sorry for the long winded message. but education is knowledge is sanity. and simplicity is better 100% of the time. and not relying on some other software and API to have you setup work. especially when you do not have control of them changing things. look into the future and see if you want to redo this setup if that VPN company goes out of business or they change their servers in some way where the connection no longer works.

Re: How to obtain a webserver variable's value from webpage?

Posted: Sat Jul 09, 2016 7:58 am
by piert
Thanks kgschlosser, interesting post with some nice tips!

Re: How to obtain a webserver variable's value from webpage?

Posted: Sat Jul 09, 2016 12:45 pm
by krambriw
1. Clicking the button on the webpage > sends HTTP request > triggers macro that runs scripts
2. As each script completes, update a Webserver temporary variable (scriptProgress) by increments of 5 until it reaches 100 (when all the scripts have been run)
3. Each time a script complete, have EG return a JSON string containing the scriptProgress' updated value > Update the progress bar on the webpage accordingly.
To make a very simple but less elegant solution, consider adding a timer to the html javascript code that reloads the page (or a frame that holds your progress indicator). If you let the timer fire every 10 second, i guess this could do for your use case?

BR Walter

Re: How to obtain a webserver variable's value from webpage?

Posted: Sat Jul 09, 2016 2:26 pm
by kgschlosser
if anyone can help you on the html side of things is krambriw. one day i will get into the web server plugin and that's the person i am going to ask when i get stuck. which is not to for off.