Using Winsock, Page 6
Now that you've established two-way communication, the world's your oyster. You could do absolutely diddly anything.
One thing you might want to have a look at is chat sessions. Now if you put a little imagination into it, you can turn the instant messaging functionality you have right now into a chat session, no problemo.
Think about it: every time data arrives via the DataArrival event, you could add it to a colourful Text Box. And each time your user taps something in to send, this could also be added to the box. And there you have it your chat session!
If you fancy playing around with this, try grabbing my download from the previous page and making a few teensy-weensy alterations:
- Extend Form1 a little
- Add a Text Box to the newly expanded area
- Changed the Text Box properties as so:
Name - txtChatLog
Font - Arial, Bold, 9
ForeColor - Dark Red
Locked - True
This Text Box will be your chat log. Next, we're going to add code that inserts text into the chat box every time a message is either sent or received.
- Insert the following code behind your Form:
Public Sub AddToChatBox(Speaker As String, Sentence As String) txtChatLog.Text = txtChatLog.Text & _ (Speaker & ": " & vbTab & "" & Sentence) & _ vbCrLfEnd Sub
- Add the following code to the end of the Winsock DataArrival event:
AddToChatBox "Remote", strIncoming
- Add the following code after the SendData methods under the Command Button:
AddToChatBox "You", strOutgoingMessage.Text
And that's it! Every time a message is sent back and forth, it gets added to the log creating a virtual chat session. Corr blimey sneaky or what?
Top Tip: If you're wanting to knock-up a multi-user chat room, check out this site: http://www.freevbcode.com/ShowCode.Asp?ID=137, which demonstrates using multiple Winsock connections. You can even create your own mini ICQ with this good-looking download http://www.planetsourcecode.com/xq/ASP/txtCodeId.6520/lngWId.1/qx/vb/scripts/ShowCode.htm from Evan Sims.