Simple. Net-based websocket instance-online chat room

Source: Internet
Author: User
I believe that with the introduction in the previous two articles, developers who are a little familiar with socket programming should be able to design a websocket-based online interaction system on their own, while I am still somewhat enthusiastic about it, simply write a complete chat room, just a little contribution to the industry's new socket development. You are welcome to give me some ideas and Code. It is my wish to promote each other. Let's first make a general plan on what functions will be available in our chat room (listed first, which may be extended in the future ): 1. log on to the chat room 2. Broadcast messages to all online chat friends 3. Send messages to an online chat user, that is, private chat. 4. Exit the chat roomThese four points are also the simplest chat room function. For this reason, I have designed a simple application protocol. First, I will explain it to help you better understand it, I didn't use saving characters like the protocol in the second lecture. I believe that smart users will not be as clever as they are now in real design. The Protocol I am talking about now is for the text content in dataframe in the previous section. It is formulated to meet the effective communication between the client and the server during the chat, all commands start with a three-character command code. Each Command consists of four parts: Command word, message source, Message destination, and message body. The command is defined as follows:

 

1. log on to the chat roomThe specific process is as follows: after the client establishes a connection with the server, the client immediately sends the following format: Lin, Master Lin of Xiaoyao Valley is the logon command code, and Master Xiaoyao Valley is the nickname of the user, that is, the name you display in the chat room. After successful login, the server will generate an integer ID for you and send a notification to all online clients. Someone has logged on, and all online customer information will also be sent, return to you, so that as soon as you log on, everyone in the chat room will know you are here, and you can also know who are online now in the chat room. 2. Send messagesMSG, where the first 0 is the sender's ID, and the second ID is the receiver's ID (greater than 0). If you want to broadcast it to everyone, set this ID to 0. 3. Online listOln, online id and name combination string the first 0, fixed, representing the server sent, the second 0, in reality is your own ID 4. Exit the chat roomThe first value of out, 0, 0 indicates the ID of the client to exit. In this way, when a client exits, the corresponding online list will be refreshed on the Interface of other customers. This command was originally followed by two items that can be left blank, but for the sake of uniform format, keep it first and use it as a backup item. To achieve the interface effect, I will first borrow an interface in our project, and then fill in the content. Let's preview it first, which is our goal.

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.