Web Basics
This example illustrates some of the new web features in MonkeyTerm v2+.
For reference you might want to take a peek at the MOM.
When the MT Web Server receives a request for a document like "/SessionName/SomeStrangeString?Param=Value" it will fire a web event to the session with the name "SessionName".
The session will then check if there's any web-events that have a pattern that match "SomeStrangeString".
If a matching event is found, then it is fired and the parameters like "Param=Value" can be accessed through the web.request object.
-
First we need an event that tells the web server to send a file when somebody requests it.
We call that event "web/ShowFile".
event name: web/ShowFile
event type: web
Command: <% web.response.filename=session.expandpath("%sessionpath%\" & $1) %>
Pattern: ^(.+\.\w{1,4})$
Enabled: yes
The regexp pattern ensures that the requested document ends with a "." and 1 to 4 word-characters.
This is a test that the filename extension is written. The command then looks for this file
with the given name located under the session path.
-
Next we need to make the on load event force the browserwindow to reload. This is done in the "OnLoad" Event.
<%
session.browser.width=6200
session.browser.visible=true
session.updatelayout
Session.browser.locationurl="http://localhost:" & web.server.localport & "/" & session.sessionname & "/webbasics.htm"
%>
-
And it works. It is used to show load this page and the corresponding stylesheet for this page. Press F10 to find these 2 events in the FMT.
When you click this link it will execute the following command: "2s3n10e".
This is done in the event "web/firecommand".
event name: web/firecommand
event type: web
Command: <% session.evaluatecommand web.request("cmd") & vbcrlf %>
Pattern: ^firecommand$
Enabled: yes
Press F10 to find the event in the FMT.
Note: The reason that the document in the browser window doesn't change is that that the web-event has no output.
This causes monkeyterm to send the header "204 No Content" to the browser.
The browser then knows to keep the current page.
You can access the document object of the browser through web.doc. This can be used for all kinds of fancy
stuff.
- Blink browser background when friend enters
- Show HP/Mana in a nice graphical way.
- write your score sheet to the browser window
- ...
Or like in this cool example change the text of a <span> element whenever a timer event fires in the session.
This clock is updated by the event "TestTimer" in monkey term: "A clock"
The html code for the element is
<span id="Tester">Monkey</span>
The updating event "TestTimer" is constructed like this:
event name: TestTimer
event type: Timer
Command:
<%
set tester=web.doc.getElementById("Tester")
if not tester is nothing then tester.innerText=now
%>
Interval: 1
FireOnce: no
Enabled: yes