Develop a DCOM-based local area online chat room (3)

zhaozj2021-02-16  47

(Connected to above)

Perfect and repair:

Based on the revision of the bugs found by the test, we have a certain change in the client, mainly in:

• A better abnormality process for the client to prevent the client from being requested by the server due to the server abnormal interrupt.

· Added speech objects and whispers (implementation on client)

· Increase the login form, you can log in to the specified room and configure the server (see the improvement of the server below)

In addition, we also have some improvements in the server side, mainly completed the last function, mainly reflected in:

· Complete the server-side functionality to configure and open multiple topic rooms (a Tchatroom instance corresponds to a topic room)

· Maintain a list of people who log in into the room for each room of the server for client calls

· Improve the server-side UI, and implement the server-side login and logout to the client system announcement, and limit the number of logged in to the server side and re-name judgment

Let's take a look at the code change of the main improvement part, first is the interface of the server:

IChatManager = Interface (Idispatch)

['{E7CD7F0D-447F-497A-8C7B-1D80E748B67F}']]

......

Function getroomlist: istrings; saFECALL; // Client obtains a list of rooms in server-side

Function RoomcanLogin (conter; widestring): Integer; SaFECALL;

/ / The client receives a return value to determine if the server is allowed to log in.

// Return value representation: 1: You can log in 2: Users reintegration 3: Excessive number

Function RoomuserList (Roomid: Integer): Istrings; SaFECALL;

// Provide a list of personnel in a room for the client, and maintained this list by tchatroom

/ / Each login and leave a USER update list

END;

Where the implementation of RoomcanLogin is more important, the remaining two interfaces are just two lists that returns server maintenance.

// RoomcanLogin method corresponds to the implementation within the TCHATROOM class

Function Tchanom.canlogin (username: string): Integer;

VAR

i: integer;

Begin

RESULT: = 1;

If Froomuserlist.count> 50 THEN / / Up to 50 people allowed in a room

Begin

RESULT: = 3;

EXIT;

END;

For i: = 0 to FroomuserList.count-1 DO

// Traverse the list of people maintained by Tchatroom to determine if there is a reimported user

Begin

If FroomuserList [I] = username the

RESULT: = 2;

Break;

END;

END;

Let's take a look, the last multi-top question room maintenance:

/ / Please compare the same name of the previous article

Constructor tchatroommanager.create;

VAR

i: integer;

Begin

Froomlist: = TSTRINGLIST.CREATE;

Try

Froomlist.loadfromfile (applfilepath (application.exename) 'chatroomlist.ini');

Except

ON E: Exception DO

Begin

Application.MessageBox (Pchar ('Profile Error, Error Code:' E.MESSAGE), 'DCOMCHATPRO', MB_ICONWARNING; Application.Terminate;

END;

END;

Froomlist.delete (0);

Froomcount: = Froomlist.count;

// Here, there will be several chat rooms from the configuration file.

SetLength (Chatroom, FroCount);

For i: = 1 to Froomcount DO

Chatroom [I]: = tchatroom.create (Froomlist [i-1], i);

// Create every instance of the room

END;

The important improvement of the client Timer.ontimer (the functionality of whispers and speech objects is implemented here):

/ / Please compare the same name of the previous article

Procedure TclientMainform.Timer1Timer (Sender: TOBJECT);

VAR

Tempstrings; TSTRINGS;

i: integer;

TOSTARTPOS, TOENDPOS: Integer

FromWho, Towho, TempName: String

Begin

Try

if chatserver.server.readready (roomid) = 1 THEN

Begin

Tempstrings: = TSTRINGLIST.CREATE;

Setolestrings (Tempstrings, chatserver.server.readfrom (roomid);

IF FreadStartPos> 19 THEN

IF (fclearbuffertag = 0-chatserver.server.testclearbuffertag (roomid)) THEN

Begin

FreadStartPos: = 0;

FCLEARBUFFERTAG: = chatserver.server.testclearbuffertag (roomid);

END;

For i: = freadstartpos to tempstrings.count-1 do

Begin

IF Rightstr (Tempstrings [I], 11) = 'SecretSpeak' Then

// You can see that you have a special indicator for the last addition of your confidence. SECRETSPEAK.

Begin

// This program parses the object from the string

TOSTARTPOS: = POS ('Sneed ", tempstrings [i]);

FromWho: = Copy (Tempstrings [i], 1, TOSTARTPOS-1); // Who said

TOSTARTPOS: = TOSTARTPOS 10;

TOENDPOS: = POS ('said:', tempstrings [i]);

Towho: = Copy (Tempstrings [i], TostartPos, toendpos-TostartPOS); // Say

IF (Towho = 'owner') or (Towho = Username) or (fromwho = username) THEN

// is what you said, or you should see, or have the right to see it for everyone's whisper.

Begin

Memo1.Lines.Add (Copy (Tempstrings [i], 1, Length (Tempstrings [i]) - 11))

Memo1.Lines.Add ('');

END;

end

Else // Don't see the content

Begin

Memo1.Lines.Add (Tempstrings [i]); Memo1.Lines.Add ('');

END;

END;

FreadStartPos: = Tempstrings.count;

END;

// Refresh the list of online personnel

Listbox1.clear;

Setolestrings (ListBox1.items, chatserver.server.roomuserlist (roomid));

// Refresh the list of speech objects

TempName: = SpeakTocbx.Text;

SpeakTocbx.clear;

SpeakTocbx.Items.Assign (ListBox1.Items);

SpeakTocbx.Items.Insert (0, 'owner');

For i: = 0 to speaktocbx.items.count-1 do

Begin

If SpeakTocbx.Items [i] = TempName Then Break;

END;

IF I> SpeakTocbx.Items.count-1 Then i: = 0;

SpeakTocbx.ItemIndex: = i;

//

Except // abnormal processing

ON E: Exception DO

Begin

Timer1.enabled: = FALSE;

Application.MessageBox

(PCHAR ('communication interruption or server fault, the program will be closed after the point is determined, please restart later. Detailed interrupt cause:' E.MESSAGE), 'DCOMCHATCLIENT', MB_ICONWARNING

Application.Terminate;

END;

END;

END;

Of course, the string of the above program (says who, who said, whether it is a whisper) is generated at Speak, which is quite simple:

// Client's Speak

Procedure TclientMainform.Button1Click (Sender: TOBJECT);

VAR

Content: String;

Begin

if Edit1.Text = '' THEN

Begin

Application.MessageBox ('can't make a message.', 'DCOMCHATCLIENT', MB_ICONINFORMATION);

EXIT;

END;

if Length (Edit1.Text)> 100 THEN

Begin

Application.MessageBox ('speaking is too long.', 'DComchatClient', MB_ICONInformation);

EXIT;

END;

if CheckBox1.checked Then

Content: = Username 'STET SPEAKTOCBX.TEXT ' said: ' Edit1.Text ' Secretspeak '

// You can see the function of whispering and speaking objects just simply handled on strings.

Else

Content: = Username 'Say:' Edit1.Text; Edit1.Text;

ChatServer.Server.Speakto (Content, Roomid);

Edit1.clear;

END;

So this program has been basically perfect, we can pack it to avoid the trouble of the end user to configure DCOM.

(

Full text

)

转载请注明原文地址:https://www.9cbs.com/read-27343.html

New Post(0)