Water 5-Getting Started and Examples-Level 2 Examples
Chat Example
This Water program creates a simple web-based chat application.

How To Use It

You type in your user name [make up whatever you like] in the "From" field, enter another user's name in the "To" field and type in a message. Click on the 'Send & Refresh' button. You'll see in the log area at the bottom of the app the message you just entered plus any other messages that have been sent to you by other users.

Other Users

Another user can log in from a different computer using the url that's generated by the code in the comment at the bottom of this file. For testing purposes, you can pretend to be the other user simply by swapping the values of the From and To fields on the page and clicking Send & Refresh.

How It Works

This application stores the log string for each user in a field on the chat class object itself with the field's name being the user's name. The interesting code is:
Example: storing the log
chat.<set <do to/>=
            <join "From: " from "
" message 
                  "
____________________________
" 
                  chat.<get to default=""/>/>/>
In this call to "set", the field name to set is computed by the code <do to/> which just uses the value of the "to" argument as the name of the field to set. When computing the value for the new "log" string, we call "join" to put together who the message is from [the value of the 'from' argument] the message itself [the value of the 'message' argument] and the previous value of the log string. That last value is computed via: chat.<get to default=""/> The call to 'get' allows the name of the field to be created programmatically. In this case, it is just the value of the 'to' argument. If that field is not present in the 'chat' object, it defaults to "", the empty string.

Inspect The State

You can see the internal data stored by inspecting 'chat'. After running the application and sending a few messages, go back into the Steam IDE, select the string "chat" at the top of the file and click on the execute button. You'll see "chat" in the supper right pane. Click the "Inspect" button. When the inspector window comes up you'll see in the main (lower) pane all the fields of the chat object. There should be a field for each of the user names that you entered whose value is the log string for that user.
Example: The Application
<class chat>  <!-- define the app -->
  <method htm_class> .<chat_page/> </method>

  <method chat_page from=""=string to=""=string message=""=string>
    <if> message.<equal ""/>.<not/>
         <do
          chat.<set <do to/>=
            <join "From: " from "
" message 
                  "
____________________________
" 
                  chat.<get to default=""/>/>/>
         chat.<set <do from/>=
             <join "To: " to "
" message 
                   "
____________________________
" 
                  chat.<get from default=""/>/>/>
        />
    </if>
   <body bgcolor=<color red=204 green=255 blue=204/>>

    <form action="/chat_page">
      <H1> Cheap Chat </H1>
      <i>Access via url:</i> http://<do os.<my_ip_address/>/>:9090
      <table>
       <tr><td>From:</td><td><input name="from" value=from/></td></tr>
       <tr><td>To:</td><td><input name="to" value=to/></td></tr>
       <tr><td>Message:</td><td> <textarea name="message" value="" rows=5 cols=60/></td></tr>
       <tr><td colspan=2><input type="submit" value="Send & Refresh"/></td></tr>
       <tr><td>Log: </td><td><textarea rows=15 cols=60 value=chat.<get from default=""/>/></td></tr>
      </table>    
    </form>
   </body>
  </method>
</class>
Example: launch the server
<server chat port=9090/>
Example: use the app from a browser
<open_browser_window "http://localhost:9090"/>
Example: call from Water
<resource "http://localhost:9090"/>.<execute/>
Example: To generate the URL to access this app from another machine, execute:
<join "http://" os.<my_ip_address/> ":9090"/>