C # Build your own internal communication platform (I)

Source: Internet
Author: User

Project interval, a little boring...

Overview

In our current work, the most important part is communication. The so-called "communication" refers to the communication between large and medium-sized enterprises at work. There are various communication methods, which can be emailed, telephone or online communication tools, each of which has its own characteristics. For example, an email can be archived and can easily trace previous communication records, but the email communication speed is a little slow; telephone communication is the quickest way to express your thoughts and reach consensus. However, the telephone communication results are hard to be tracked, if there is no formal meeting minutes, they will be forgotten again in a short time. The third common method is to use some online communication tools, such as MSN and QQ, I also made a rough comparison of these communication tools, which only represent my personal point of view. I have no intention to advertise im tools or slander any manufacturers:

1. Yahoo

In actual projects, the most important communication is the so-called "group chat" approach, that is, a project team cannot sit together physically, therefore, we need to communicate with each other through "chat. Yahoo provides a good group chat function and supports voice chat. However, it is inconvenient to start receiving messages only after the receiver accepts them. Otherwise, messages in the group cannot be received, which leads to loss of message communication in group work, affects work.

2. MSN

At present, MSN is widely used in the market, but in China, its login has always been a headache and frequent logon failures, especially within our company, 70% of users cannot use MSN normally. In addition to logon, another problem is that MSN may occasionally lose information. In extreme cases, we can see that the chat party is desperately answering the question, the other side keeps asking, "Have you received this?" The adverse consequences of this problem are terrible.

3. qq

QQ is a very good and representative im tool in China. Its group chat function is quite good, but there is a restriction on Group Creation, that is, you must have a "sun" to create a group, or apply for a QQ member to create more groups. This is the biggest difference with other tools, it may be because it has been commercially successful. If the company has multiple project groups, it should be difficult to create multiple groups.

4. Skype

This is a software that provides excellent voice effects. When I am chatting, This is the preferred tool. Its voice is in terms of definition or latency, it has great advantages over other tools. The group function of the new version of Skype is also quite strong, but one of the biggest features of Skype is that it does not support offline message sending. Only when the sender and receiver are online at the same time will the message be sent, this can easily lead to message communication errors. For example, when we communicate with overseas branches, the time difference is very large. When I send a message, the other party is not online, but when the other party is online, I am not online. I receive messages only when both ends are online at the same time. Therefore, I often receive offline messages long ago, which has a great negative impact on my work.

5. officeim

I tried to find a more suitable tool after trying the main tools mentioned above.

All the above four tools are generic software that requires Internet support and are only available within the lan. For some companies, Internet access permissions must be activated, some companies do not even allow access to the Internet. For our company, access to the Internet must be strictly examined, and QQ should be prohibited. So I want to find a small communication platform in my own company.

This tool is a paid software. For the time being, I have discovered a problem that an employee can only belong to one node in the organizational structure. For our project management method, basically, it has been rejected because we need to belong to multiple project teams.

6. Flying Pigeon biography um

This is a software originated from Japan. The client is free to use and the server will incur charges. This software not only supports the organizational structure, but also allows a person to be affiliated to multiple project teams at the same time, I tried it and it worked well, but then I found a serious problem (to be honest, I can't remember the specific problem now). I called their customer service staff directly, confirm that the problem cannot be solved.

The above are just some im tools I have found. I believe there are countless similar tools in the world, and they should be exactly suitable for me, but I have not found them, if you cannot find one, why don't you try writing one by yourself? Well, it's a bit difficult to say, but I know myself. I know what features I need. I won't do a lot of fancy and beautiful effects. Let's start preparing for it. The first thing I need to do is to find a senior person in the circle to give some advice on the general working method of IM, it is learned that the most common method is to establish a TCP connection between chat clients to achieve mutual communication between clients. I am writing C # for a long time #ProgramThere are not many concepts about these underlying communication methods. Do you give up? No. After continuing to ask for advice, I learned that some people directly use WebService to establish communication between the client and the server. The client does not communicate with each other, and all requests are forwarded through the server, in fact, this is exactly what I imagined. I didn't expect someone to do this. I also know that this method has many shortcomings, but I think about my actual scenarios, there should be no major problems. Let's get started!

Architecture

In the overview section, we mentioned a point about the architecture. to simplify the application, we must implement the simplest chat function in the fastest way in the shortest time, because this tool serves the project itself, it does not exist as a specialized project.

The database uses Oracle10g to ensure the storage of a large amount of concurrency and historical information as much as possible, because the system has been clearly used within the enterprise and is all work-related, to prevent the loss of client chat records, this architecture requires that all chat records be retained on the server for later auditing.

The second part is the WebService server, which is the channel for interaction between the client and the database server and transmits information between the client and the database server. In this architecture mode, clients do not communicate with each other, and all of them need to be transferred through the server.

The third part is the client, that is, internal enterprise users. In this system, the public registration function is not provided. All users must register in the background and assign an initial password. After logging on, users can modify their own passwords, this method ensures the security and privacy of the system.

The system has been continuously improved by multiple members of the project team during use. Now it is quite easy to use and new functions are constantly added. The following describes the database, WebService, and client programs in sequence.

Database description

Databases mainly store the following content:

User Information

Ø organization structure information

Message history

Other auxiliary information

The following describes each table in detail:

1. User table: zr_user

Field name

Field Type

Description

User_id

Integer

The user's internal ID. This field is used when associated with other tables.

User_alias

Varchar2 (255)

The user's login name, stored in the database with uppercase letters

Name

Varchar2 (100)

The real name of the user. The real name is displayed in the chat tool.

Password

Varchar2 (255)

The user login password, which is initialized by the system and can be modified after logon.

Status

Integer

Status

0 indicates that logon is not allowed.

1 indicates that you can log on

Creation_user

Integer

User Created for this record

Creation_time

Date

Creation Time

Last_mod_time

Date

Last modification time of this record

Deleted

Integer

Delete flag

0: normal

1: Deleted

Last_login_time

Date

The last time the user logs on to the system. In this system, the value of this field is used to determine whether the user is online.

2. Organization table: zr_organization

Field name

Field Type

Description

Org_id

Number

Internal ID of the organization for Association

Parent_id

Number

ID of the upper-level organization

Org_type

Number

Organization type

Org_code

Varchar2 (100)

OrganizationCode

Org_name

Varchar2 (200)

Organization Name

Status

Integer

Status

0 indicates temporary Group

1 indicates a group that takes effect permanently.

Creation_user

Integer

Creator

Creation_time

Date

Creation Time

Last_mod_time

Date

Last modification time

Deleted

Integer

Delete flag

Ordering

Integer

Sort Value

3. User-organization table: zr_user_organization

Field name

Field Type

Description

Org_id

Number

Internal ID of the organization for Association

User_id

Number

User ID. In this table, you can describe the many-to-many relationship between users and organizations.

4. Message table: zr_message. Currently, all messages are saved on the server until they need to be deleted.

Field name

Field Type

Description

Oid

Number

Primary Key of the table

To_id

Number

The recipient's ID. It can be an individual or a group.

To_type

Number

Recipient type

0 indicates individual

1 indicates a group

Creation_time

Date

Sent

From_id

Number

Sender ID

Content

Varchar2 (2000)

The content sent. The field width cannot exceed 1000 Chinese characters.

5. message query History Table: zr_message_his. In the system, you need to record which messages have been viewed by whom to avoid repeated sending. Because the messages sent to a group must be read by all members of the group, therefore, you cannot simply add a "read or not" field to a message.

Field name

Field Type

Description

His_id

Number

Primary Key of the table

User_id

Number

Recipient id

Message_id

Number

Message ID

Creation_time

Date

Sent

Batch_id

Number

Batch ID. A member may receive a batch of messages at the same time, instead of sending messages one by one. The batch information is recorded here to reduce the interactions with the server.

Using Oracle as a storage database, in addition to its large capacity support is a good feature, it is also a great advantage to be able to write a very good storage process. The so-called stored procedure is to write some programs in the database to ensure quick and convenient operation of the database, rather than writing very complex C # code.

In this system, there is a package named zr_tim_pkg, which includes the following main stored procedures. A detailed description is given below:

1. Check the user's logon status

Function getuserstatus (lastlogintime in date) return number is
Value number: = 0;
Begin
If (lastlogintime is not null) then
If (sysdate-lastlogintime <2/24/60) then
Value: = 1;
End if;
End if;
Return value;
End;
This function is mainly used to retrieve the user's logon status. In this system, it is considered that the last logon date is more than two minutes offline.

2. Send messages. This function is used to receive messages sent from the client and recorded in the database. The function body is as follows:

Function sendmessage (toid number,
Totype number,
Fromid number,
Content varchar2) return number is
Pragma autonomous_transaction;
Value number: = 0;
Begin
Select seq_message.nextval into value from dual;
Insert into zr_message
(OID, to_id, to_type, creation_time, from_id, content)
Values
(Value, toid, totype, sysdate, fromid, content );
Commit;
Return value;
End;

3. this function is mainly used to determine which messages need to be sent to a specified user, that is, the messages cannot be sent repeatedly or leave any useful information. If the information is sent to a group, all the members in the group need to get the message. The program is as follows:

Function getmessage (userid in number) return number is
Pragma autonomous_transaction;
Value number;
Mbatchid number;
Begin
Select seq_message_batch.nextval into mbatchid from dual;
Insert into zr_message_his
(His_id, user_id, message_id, creation_time, batch_id)
Select 1, userid, OID, sysdate, mbatchid
From zr_message m
Where (to_id in
(Select org_id
From zr_organization
Start with org_id in (select org_id
From zr_user_organization
Where user_id = userid)
Connect by prior parent_id = org_id) or to_id = userid)
And from_id! = Userid
And not exists (select 'x'
From zr_message_his H
Where user_id = puserid
And H. message_id = M. OID );
If (SQL % rowcount = 0) then
Value: = 0;
Else
Value: = mbatchid;
End if;
Update zr_user set last_login_time = sysdate where user_id = userid;
Commit;
Return value;
End;

Code segments are the longest part of the entire stored procedure. There is a trick to judge whether you have read them. The code snippets are as follows:

Not exists (select 'x' from zr_message_his H where user_id = puserid and H. message_id = M. OID)

When writing a program, you generally like to use the not in syntax. This syntax is simple and easy to understand, but there is a big problem, that is, the efficiency is not guaranteed, after a table must be designed to meet a series of conditions, sometimes the results cannot be achieved. Not exists makes it easier to satisfy the efficiency.

The concept of "batch" is also used, that is, multiple messages can be sent to the user at a time, and the batch_id field is used to ensure the integrity of the batch.

4. Create a temporary group chat

The operating method of the system is to help establish the relevant project team or organizational structure in the background and create relevant users. Users cannot register themselves or select their own groups, when chatting, you can select one-to-one chat or initiate a group chat for the entire project group. These two methods basically meet the chat requirements, but for some special situations, for example, a project team has more than 10 people, and I only want to chat with two or three of them, at this time, if the entire project team is the recipient, many people will receive useless information and even work will be disturbed. Therefore, we accept suggestions from our project team members, the concept of "temporary group chat" is that a group can be created temporarily to chat in this small group. This function is used to create this group, and the group is temporary. Now let's take a look at the Code:

Function inviteuser (puserid in number, pguestids in varchar2)

Return varchar2 is

Pragma autonomous_transaction;

Value varchar2 (1000 );

Mgroupid number;

Mgroupname varchar2 (1000): = '';

Mname varchar2 (100); -- name of each person, Temporary Variable

Cursor cursor _ is select name from zr_user where user_id

In (select user_id from zr_user_organization where org_id = mgroupid );

Begin

Select seq_group_temp.nextval into mgroupid from dual;

Insert into zr_organization (org_id, parent_id, status, deleted,

Creation_time, creation_user, ordering)

Values (mgroupid, 0, 0, 0, sysdate, puserid, 0 );

Insert into zr_user_organization (user_id, org_id)

Select to_number (A. column_value), mgroupid

From table (split (pguestids, ',')

Union all

Select puserid, mgroupid from dual;

Open cursor _;

Fetch cursor _ into mname;

While (not cursor _ % notfound) loop

Mgroupname: = mgroupname | mname | ',';

Fetch cursor _ into mname;

End loop;

Close cursor _;

Mgroupname: = rtrim (mgroupname ,',');

Update zr_organization set org_name = mgroupname where org_id = mgroupid;

Commit;

Value: = mgroupid | '~ '| Mgroupname;

Return value;

End;

So far, the database has been roughly introduced. Although the code is lengthy, most of the Code has been pasted to prevent readers from misunderstanding. The following link is the preparation of WebService. Please wait for the next period.

Unfinished

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.