REBOL [Title: "Peer-to-Peer Instant Messenger"] connected: false ; This is a "flag" variable, used to mark whether or not the ; two machines have already connected. It helps to more ; gracefully handle connection and shutdown actions throughout ; the script. ; The code below traps the close button (just a variation of ; the routine used in the previous database example). It ; assures that all open ports are closed, and sends a message ; to the remote machine that the connection has been terminated. ; Notice that the lines in the disconnect message are sent ; in reverse order. When they're received by the other machine, ; they're printed out one at a time, each line on top of the ; previous - so it appears correctly when viewed on the other ; side. insert-event-func closedown: func [face event] [ either event/type = 'close [ if connected [ insert port trim { ************************************************* AND RECONNECT. YOU MUST RESTART THE APPLICATION TO CONTINUE WITH ANOTHER CHAT, THE REMOTE PARTY HAS DISCONNECTED. ************************************************* } close port if mode/text = "Server Mode" [close listen] ] quit ] [event] ] view/new center-face gui: layout [ across at 5x2 ; this code positions the following items in the GUI ; The text below appears as a menu option in the upper ; left hand corner of the GUI. When it's clicked, the ; text contained in the "display" area is saved to a ; user selected file. text bold "Save Chat" [ filename: to-file request-file/title/file/save trim { Save file as:} "Save" %/c/chat.txt write filename display/text ] ; The text below is another menu option. It displays ; the user's IP address when clicked. It relies on a ; public web server to find the external address ; (whatismyip.com). The "parse" command is used to ; extract the IP address from the page. Parsing is ; covered in a separate dedicated section later in ; the tutorial. text bold "Lookup IP" [ parse read http://whatismyip.com [ thru copy my-ip to ( alert to-string rejoin ["external: " my-ip " internal: " read dns://] ) ] ] ; The text below is a third menu option. It displays ; the help text when clicked. text bold "Help" [ alert { Enter the IP address and port number in the fields provided. If you will listen for others to call you, use the rotary button to select "Server Mode" (you must have an exposed IP address and/or an open port to accept an incoming chat). Select "Client Mode" if you will connect to another's chat server (you can do that even if you're behind an unconfigured firewall, router, etc.). Click "Connect" to begin the chat. To test the application on one machine, open two instances of the chat application, leave the IP set to "localhost" on both. Set one instance to run as server, and the other as client, then click connect. You can edit the chat text directly in the display area, and you can save the text to a local file. } ] return ; Below are the widgets used to enter connection info. ; Notice the labels assigned to each item. Later, the ; text contained in these widgets is referred to as ;