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

zhaozj2021-02-16  43

TCHATROOM implementation:

{Tchatroom}

Constructor tchatroom.create (roomname: string; roomid: integer);

Begin

FBufferLength: = 0;

FconnectCount: = 0;

FCLEARBUFFERTAG: = 1;

FLOCKED: = false;

Froomname: = roomname;

Froomid: = roomid;

END;

Procedure tchatroom.clearbuffer;

VAR

i: integer;

Begin

/// You can detect a sign, it is determined whether the server is required to record each chat content.

For i: = 1 to 20 do

FBuffer [I]: = '';

FBufferLength: = 0;

FCLEARBUFFERTAG: = 0-fclearbuffertag;

END;

Procedure tchatroom.onespeak (Content: string);

Begin

FLOCKED: = TRUE;

Inc (FBufferLength);

IF FBufferlength> 20 THEN

Begin

Clearbuffer;

Inc (FBufferLength);

END;

FBuffer [fbufferLength]: = Content;

FLOCKED: = false;

END;

Function tchatroom.onRead: tstrings;

VAR

FSTRINGS: TSTRINGS;

i: integer;

Begin

FLOCKED: = TRUE;

FStrings: = TSTRINGLIST.CREATE;

For i: = 1 to fbufferLength Do

FStrings.Add (fbuffer [i]);

RESULT: = fstrings;

FLOCKED: = false;

END;

Function TchanOM.getcanRead: Boolean;

Begin

Result: = FALSE;

IF FBUFFERLENGTH> 0 THEN RESULT: = true;

END;

Procedure tchatroom.loginroom (username: string);

// Users log in to chat room events, there is no complete implementation here

Begin

INC; fconnectCount;

END;

Procedure tchatroom.leaveroom (username: string);

// Users leave the chat room event, there is no complete implementation here

Begin

Dec (fconnectcount);

END;

The last compare part of the server side TchatroomManager:

Type

TchatroomManager = Class

Private

Chatroom: array of tchatroom

public

Constructor crete;

Function FindroomByid (ID: Integer): tchatroom;

END;

Implementation part:

{Tchatroommanager}

Constructor tchatroommanager.create;

VAR

I, Roomcount: Integer;

Roomnames: tstrings; // roomname is the name of the chat room in the configuration file

Begin

Roomcount: = 1;

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

Roomnames: = TSTRINGLIST.CREATE;

Roomnames.Add ('Testroom'); // This sentence will be replaced by the final reading from the profile reading of SetLength (chatroom, roomcount);

For i: = 1 to roomcount do

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

END;

Function TchanManager.findroombyid (ID: Integer): tchatroom;

// This function is called by the iChatManager interface. Since the final version of the interface will be provided to the customer

// End get the function of the room list, so the client knows the ID of your room

Begin

Result: = chatroom [ID];

END;

INITIALIZATION

Chatroommanager: = tchatroommanager.create;

End.

After the server side is complete, we configure the server-side DCOM configuration, you can develop a simple client to test: (Although the client is as simple as possible, we don't have to configure DCOM, but we still need to copy the type of server. Library files .TLB to the client and register to develop and use the client, of course, these can be done by the installer)

On the client, we only list two relatively important functions, and the rest is omitted, please let me know all the programs:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

// Click on Button1 to "say"

Begin

Server.speakto (Edit1.Text, 1);

END;

Procedure TFORM1.TIMER1TIMER (Sender: TOBJECT);

/ / Request to the server every other period of time, I set up for 1.5 seconds

VAR

Tempstrings; TSTRINGS;

i: integer;

Begin

IF server.readready (1) = 1 THEN

Begin

Tempstrings: = TSTRINGLIST.CREATE;

Setolestrings (Tempstrings, Server.ReadFrom (1));

IF FreadStartPos> 19 THEN

IF (fclearbuffertag = 0-server.testclearbuffertag (1)) THEN

Begin

FreadStartPos: = 0;

FCLEARBUFFERTAG: = Server.TestClearBuffertag (1);

END;

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

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

FreadStartPos: = Tempstrings.count;

END;

END;

A basis

DCOM

The core part of the local area online chat room is basically completed, and all the tests are relatively smooth. Here, you need to add a difficult point to the chat room server: you need to have a very cautious handling of the developer. Although I also have a certain synchronization. Processing, but there may still be deadlocks or other locks in the case of a number of clients, which require further testing, and even certain reconstruction. Please pay attention to the text.

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

New Post(0)