Browse DevX
Sign up for e-mail newsletters from DevX


Create a Java TOC2 Class to Communicate with AIM : Page 3

Instant messaging is more than a great way to keep in touch: It's a full-fledged business tool and there are plenty of reasons that you might be interested in extending your favorite IM service into a custom chat application. But if you choose AIM, you'll need to be armed with some detailed information about its brand new protocol.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Receiving an Instant Message
To receive instant messages you must implement the Chatable interface:

public interface Chatable { public void unknown(String str); public void error(String str,String var); public void im(String from,String message); }

Whenever you receive a message, the im method will be called. The error method will be called when an error occurs. The unknown method will be called when a known TOC command is received. This allows you to handle additional TOC commands that my library does not handle.

To process the TOC events you should call the processTOCEvents method. This method enters a loop and waits for TOC events to come through the socket:

public void processTOCEvents() throws IOException { for ( ;; ) { String str = this.getFlap(); System.out.println(str); if ( str==null ) continue; if ( str.toUpperCase().startsWith("IM_IN2:") ) { handleIM(str); } else if ( str.toUpperCase().startsWith("ERROR:") ) { handleError(str); } else { owner.unknown(str); } } }

As you can see, this method enters a loop and waits for IM_IN2 commands. Because this method enters a loop, you should call it from a background thread. It will process incoming TOC messages until its thread is terminated.

For every incoming instant message, the handleIM method is called. This method parses the instant message and sends the results on to the interface's im method:

protected void handleIM(String str) { StringBuffer sb = new StringBuffer(str); nextElement(sb); // get who its from String from = nextElement(sb); String a = nextElement(sb); String b = nextElement(sb); // get message String message = sb.toString(); owner.im(from,message); }

As you can see, this method parses the parameters of the incoming message. The only parameters that are used are who the message is from, and what the message text is.

Sending an Instant Message
To send messages, the send method is provided. The send method accepts two parameters: who the message is to and what the message is. The send method is shown here.

public void send(String to,String msg) { try { this.sendFlap(DATA,"toc_send_im " + normalize(to) + " \"" + encode(msg) + "\""); } catch ( java.io.IOException e ) { } }

The send method is relatively simple. The message is packaged up as a toc_send_im command and sent over the socket. The normalize method is used to properly encode any special characters that might be in the user's name.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date