An important note: These lessons are now updated and built into Hackety Hack. Don't bother reading them here on the wiki. Just run HH and click on the lightbulb icon in the toolbar. Much obliged! - _why

Lesson Six / Hacky Chats

Approx Time Needed: 20 mins Due Date: Tues., April 3rd

Part A: Walkie-Talkies

Hackety Hack has built-in walkie-talkies! You know how walkie-talkies let you talk on certain channels? Or how radios need to be tuned to a specific frequency? Well, you can name the frequency you want to chat on. Here, I'll show you...

First, open up Hackety Hack and click on "Your Setup". It's right on that first page under Hacky Mouse. On that page, type in your Hackety Hack name and password. (It's the same one you're using to download these test versions.)

Save your setup. Now, start a new program called "Test Channel".

 #!ruby                                                                                                        
 # Test out my chat channel                                                                            
 chan = Hacker('yourname').channel('test')                                                             
 puts chan.start                                                                                       

Where it says "yourname", put in your Hackety Hack username. You're going to start your own channel named "test".

You should see the message:

  (Channel yourname's test)                                                                             

A channel is a place where you can send chat messages. You're going to love this. It's so easy! Okay, let's send a few chat messages:

 #!ruby
 # Test out my chat channel                                                                            
 chan = Hacker('yourname').channel('test')                                                             
 chan.say("Anyone home???")                                                                            
 chan.say("HALLOOOOOO???!")                                                                            

The say method sends chat messages to the channel. And, even though it's your channel, anyone can come and chat on that channel, if you give them the name of the channel.

Let's read the messages:

 #!ruby
  # Test out my chat channel                                                                            
 chan = Hacker('yourname').channel('test')                                                             
 chan.say("Hmm, that truck just exploded.")                                                            
 puts chan.hear()                                                                                      

The hear method gets back a list of messages from the channel. You should see all of the messages you've just sent. Messages will disappear from the channel after ten minutes, so keep chatting or the channel will go dead!

Now, go get a buddy to start Hackety Hack on their machine. (They can be across the Internet even.) And try playing with the last program, sending messages and reading messages.

Remember, though, that your buddy will need to use *your* channel. So, if your name is 'klapaucius', your buddy will do this:

 #!ruby
 Hacker('klapaucius').channel('test')                                                                  

Both of you need to be talking on the channel you created. Got it?

Extra credit: Try sending a Picture object from the Internet. Pictures are created like this:

 #!ruby
 Picture("http://hacketyhack.net/hackety.png")                                                         

For more extra credit, try sending each other some Arrays and Symbols.

Part B: Making It Slick

So, say and hear are pretty cool. But it's kind of annoying to have to keep running the program over and over just to keep chatting, isn't it? Let's write an actual chat popup, you know?

Let's make a popup that reads the channel:

 #!ruby
 # Test out my chat channel                                                                            
 chan = Hacker('yourname').channel('test')                                                          
 Web.popup() do                                                                                        
   title('Chat Box')                                                                                   
   box = scrollbox()                                                                                   
   editline('Say')                                                                                     
 end                                                                                                   

Okay, save it. Run it. How's that look? It doesn't do any actual chatting yet, so let's start reading the chat messages:

Right after the editline('Say'), but before the end, add this:

 #!ruby
 every(2) do                                                                                           
   chan.hear().each() do |message|                                                                     
     add_to_scrollbox(message)                                                                         
   end                                                                                                 
 end                                                                                                   

This is a neat little bit of code! The every method will run over-and-over in the background. This means that your popup can show up and the every method can do its business without stopping your program. The 2 means "every 2 seconds".

So what are we doing every 2 seconds? We're listening to the channel, grabbing any new messages and adding them to the scrollbox. (Testing note: the add_to_scrollbox part is kind of dumb, I'm sorry, it'll go away soon! It'll probably turn into update('scrollbox', message) -- _why)

Run this program. And have your buddy send you some messages while it's running. Do you see how it checks every two seconds?

Now let's get the 'Say' box working so you can chat back. Here's the full program:

 #!ruby
 # Test out my chat channel                                                                            
 chan = Hacker('yourname').channel('test')                                                          
 Web.popup() do                                                                                        
   title('Chat Box')                                                                                   
   box = scrollbox()                                                                                   
   editline('Say')                                                                                     
   buttons(:send) do |popup|                                                                           
     chan.say(popup['Say'])                                                                            
   end                                                                                                 
   every(1) do                                                                                         
     chan.hear().each() do |message|                                                                   
       add_to_scrollbox(message)                                                                       
     end                                                                                               
   end                                                                                                 
 end                                                                                                   

The last part hooks up the 'Send' button. If you attach a block to a button, that block will be called when you press the button.

Okay, you're chatting! We're going to do some fun things with your chat channel, but for now just play around with it

Extra credit #1: Strings have a reverse method, remember? So "HacketyHack?".reverse() gives you "kcaHytekcaH". Change the chat program so that everything you say gets turned backward!

Extra credit #2: Try sending a blog entry to the channel. Do you remember how to read the MetaFilter? blog? Send: blog.items[0] to your hacker buddy and see what happens.

Oh, and if you don't have a buddy, you can see if I'm around in my channel:

 #!ruby
 chan = Hacker('why').channel('lobby')