Android Socket based Chat application example (ii) _android

Source: Internet
Author: User
Tags flush live chat

Objective

Long time no blog, previously in the Android chat room promised to write a customer (friend) between the chat demo,android socket based chat room has been implemented through the socket broadcast form of communication functions.

Here's a bubble chat app that I've written that resembles most chat software now. All the functions are their own ideas, for the current market success of the example is how to achieve, I do not understand. So the reader can only do reference learning, you can also share your case to me.

Function

    • One-on-one chat, non-chat room
    • Friends List
    • Friends online, Offline status (Live Update)
    • Bubble Live Chat Window
    • Send offline information

Basic principle

Before the chat room principle: Every time the client socket is connected to the ServerSocket, the program saves the corresponding socket in the clients collection and starts a thread for the socket that handles all the communication tasks for that socket. After the server thread reads the client data, the program traverses the clients collection and sends the data once to each socket in the Clients collection

One-on-one chat: Server through map to store clients socket, the client user ID as a map key, when a send information to B, the server search out B socket, build their communication channel.

Servers Server

This time I added 2 socket sets to the server, one to handle user online/offline, and the other to handle communication information between users.

 Static map<string, socket> Socketmap = new hashmap<string, socket> ();
 Static map<string, socket> Onlinemap = new hashmap<string, socket> ();

Clients Online, the offline action, the server will be filtered and then notify its online friends, Clients received the friend's on-line status and then modify Friends List.

Save client ' s name, online
//...
getnamestring = str.substring (config. Protocol_key.length () +config. Protocol_online.length ());
Server.onlineMap.put (getnamestring, s);
//...
Update online friends
dataoutputstream Onlinedos = new DataOutputStream (Server.onlineMap.get (Clientkey). Getoutputstream ());
Onlinedos.writeutf (config. Protocol_friends_start+onlinestring+config. Protocol_friends_end);
Onlinedos.flush ();

About Chat, I am through a custom encryption character for each Client to do a flag, such as: Client A sent information, the head of the message with a server and the client will recognize a special symbol, through the character processing, find out the information of the user information; and so on, client The communication object of A is also used in this way

Once we find the target object of Clienta, we find the socket channel and they can talk one-on-one.

Send msg to friend
dataoutputstream Ndos = new DataOutputStream (Server.socketMap.get (forname). Getoutputstream () );
Ndos.writeutf (fromname+date+ "\ n" +forchat);
Ndos.flush ();

With regard to off-line information, this is mainly the function that the server undertakes, I use MySQL to save the data. Client A sends a message to client B in the offline state, and the server determines whether client B is online and, if it is offline, stores the information in MySQL first; when client B is online, the server looks for its offline information, if there is unread information, will be sent in time. Client B will be able to receive offline information ( ̄ˇ ̄)

Client Clients

About chat, in order to be able to achieve at the same time with a number of friends Chat (different window threads), where the use of contentprovider monitoring chat data changes, so that not in the current Chat window activity can also receive a friend's information spell print.

 Monitor changes in chat data
 getcontentresolver (). Registercontentobserver (datachangeprovider.content_uri,true, COB);

That backstage is how to receive the information that the friend sends? The above server said, there is a collection of Socketmap, and this collection is to record the user's communications socket, when there is information, the client backend waitmsg () will receive the information sent and processed.

 Private Runnable Waitthread = new Runnable () {public
 void run () {
 System.out.println (' Wait running! ');
 Waitmsg ();
 }
 ;

With regard to the Online/offline status, the buddy list Activity receivemsg () monitors the buddy status information sent by the server and updates the buddy list listactivity in a timely manner.

 Update Buddy Database
 Fands.updatedata (remsg,name);
 Get buddy list
 Fansarray = Fands.getfans ();
 
 Friends = new Friends (fansarray,remsg,name);
 Friendlist = Friends.getfriends ();

Summarize

Relative to the chat room, one-on-one chat is mainly for each client socket is recorded, so that each communication action has the target object; server as a messenger docking the two sockets, so that both can communicate chat.

This is the TCP/IP protocol under the C/S mode of communication, as well as UDP protocol, Peer-to-peer mode of communication is worth learning.

Now put the code directly into the blog park, Download:demo

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.