Now, double click on the AutoDiscovery socket. In the code editor, select the MemberJoined event and type in...
ChatLog.AppendText(ip + " has entered the chatroom." + EndOfLine)
and in the MemberLeft event...
ChatLog.AppendText(ip + "has left the chatroom." + EndOfLine)
Great! So whenever a user joins or leaves, it alerts everyone else. That's cool, right?
We ain't got to the hard part yet, buddy. Better get your pens; jot down "Command 10 is a message (broadcast)". This will be your reminder, because we're going to expand on this program as we develop it.
In the ReceivedMessage event, put this code in;
if command = 10 then
ChatLog.AppendText(fromIP + ": " + data + EndOfLine)
else
end if
So, what we've done here is written half a protocol. The fromIP property is a string which contains the origin IP address, data contains the message they want to send (which is also a string). We're telling our program to take the IP and the data and append it to any other text in our chat log.
Now, let's write the other half. Take a peek at the Action method of our push button. Write in this code;
ChatroomConnection.SendMessageToGroup(10, ChatString.Text)
Yay! So, what did we just do here? We've told our socket to send a message to everyone, the command is 10 and that the data we want to send is the text in the ChatString edit field.
Now I know what you're thinking; you're just itching to try it out. No sir, we need to make sure the socket will connect and register as a member of our little network. Under the Window1 Open event, put the following code in:
ChatroomConnection.Bind(6311)
ChatroomConnection.Register("PublicChat")
This registers you as a member. We want this to be a public chatroom, so we're just going to leave this code as-is for now.
Now then, let's take it for a spin. Run our new application, type in some crap and hit send.
Yay, it works! Now, how about we clean it up? Make the send button the default button and add the following line of code to it's action method.
That code clears off what you typed in last. To make it sound a bit better, we want some sounds to put into it; so get yourself a little blip or bloop sound that we can hear each time we get a message.
Under the RecievedMessage event, change the code to:
if command = 10 then
ChatLog.AppendText(fromIP + ": " + data + EndOfLine)
<INSERT SOUND NAME HERE>.Play
else
end if
Now, test out the app. If all goes well, your text should clear out automatically and a sound should play each time you receive a message.
(http://img41.imageshack.us/img41/6549/picture4wdw.png)
Well, was that easy enough for you? Beware, underestimating network programming is easy to do. It seems fun, but once you start having commands set booleans and get those booleans to trigger sends whose IP address is stored under a property which is set by a command, you start using an awful lot of paper and wishing death to all those who might interrupt you.
In my next post, I'll tell you how we can take this to the next level by having things like usernames, private messages and crap like that. Maybe. Stay tuned.
EDIT: Sweet, I managed to make a network program in under 15 lines of code!
[size=16]Networking for Beginners II: With A Vengeance[/size]
So, we've got our little IM application; now what?
We can get it to play sounds when we get a new message, we have buttons and stuff.. but these chatlogs aren't very good, are they? No, and that is why we are going to make them much better by no longer having those horrible IP addresses show up every time.
I must note, if you are following this tutorial, by the end of it, we will have a fully featured app with all sorts of good shit, such as the program remembering your username, ability to save chatlogs, etc.. But for now, we're going to replace the IP address with a handle.
Go ahead and make two new things; a new window, and a new global property under App (this property will be UserName as String; you want this to be a global property accessible anywhere).
Call your window Preferences, you don't really need a title, but put Preferences as both name and title anyway. It won't take much effort; add a single line edit field and a button with an Okay caption. Size your window appropriately.
Now, in the Preferences window's open event...
EditField1.Text=(app.UserName)
..and in the button's action code...
app.UserName=EditField1.Text
Now, this will require a bit of reworking of that old code we wrote about an hour ago. In the SendMessage button, you'll want to replace all you wrote with...
ChatroomConnection.SendMessageToGroup(10, (app.username) + ": " + ChatString.Text)
ChatString.Text=""
This basically just appends the user name to the string we want to send to everyone else; it won't actually attach your user name to an IP address and send it out to everyone. There's a reason for this: life is too fucking short to bother with two-dimensional arrays and indexes, especially when it comes to network stuff. So, here's the REAL lesson here: ALWAYS CUT CORNERS AT EVERY OPPORTUNITY!!
So, now that's over, we can re-write this bit of code under our socket's RecievedMessage event. While we're here, we'll add another command! Make this one command 20; it tells the other user that we've entered the chat room and we really DO have a handle.
if command = 10 then
ChatLog.AppendText(data + EndOfLine)
Udo.Play
elseif command = 20 then
ChatLog.AppendText(fromIP + " is now known as " + data + endofline)
else
end if
and then add this under the Open event of the main window...
ChatroomConnection.SendMessageToGroup(20, (app.UserName))
This sends out our username to the rest of the group. We'll be borrowing this line of code to put into the preferences window under the OK button.
Window1.ChatroomConnection.SendMessageToGroup(20, (app.UserName))
Finally, add a preferences button in the main window, and paste in the following;
Preferences.ShowModalWithin(Window1)
Go ahead, run your app and give it a go. You should see usernames pop up now whenever you enter something into the chatroom.