|
-
Sep 28th, 2000, 07:44 AM
#1
Thread Starter
Fanatic Member
I'm attempting to write a chat program that allows multiple users over telnet. The program works relatively well, it's object oriented (and still in the progress of converting
the code). There are player objects which contain their own sockets, and command parsers (to help eliminate confusion and conflicts). For now, the objects are stored in
an Array (IE Player() as new oPlayer).
What I'm doing is using a large case statement to deal with commands, parse out the data that's to be sent to the other users in the chat program, and grab other
parameters from the incoming data.
Currently, when someone sends something that's seen by everyone, the server will cycle through the players who are online with a for next loop. (previously it was set to go
to the maximum number of users each time, which slowed it down a bit). For instance:
Code:
For x = 1 to playercollection.count
y = playercollection.iIndex(x)
player(y).dataout = Message
next x
As you can see, this would get rather boggy if more people logged on and there was alot of chatting going on. Going to maximum users all the time is a rather long
process in itself.
What I'd like to know, is if there is a way to utilize an event handler (this project has just modules and class modules, no forms) to take care of this problem. My thinking? If
I can use events for commands, then i do not have to worry about problems dealing with invalid player objects stuck in the player collection. If this will work, all I have to
do is raise the event Player_Chat or Player_Action and have the objects handle it themselves instead of cycling through a for next loop every time something happens.
As I understand it, an object cannot handle an event that is declared within itself, but I can add an Event Handler object that contains the events to be triggered. I've tried
to set up the error handler in a class module called PlayerEvents. Here is an example of my implementation, which didn't do what I needed it to.
In class module PlayerEvents.cls:
Code:
Public Event Command(Command As String, Player As Index)
Public Sub parseCommand(pCommand As String, pPlayer as Integer)
RaiseEvent Command(pCommand,pPlayer)
End Sub
In class module Player.cls:
Code:
Private WithEvents Socket as WinSock
Private WithEvents ehPlayer as PlayerEvents
Private Sub Class_Initialize()
Set ehPlayer = New PlayerEvents
'The code breaks without the above line
Set Socket = New WinSock
End Sub
Private Sub Socket_DataArrival(...)
.....
Select Case LCase(left(sCommand,instr(1,sCommand," ")-1))
Case "chat"
ehPlayer.parseCommand(sCommand,SocketIndex)
End Select
Private Sub ehPlayer_Command(Command As String, Index as Integer)
Socket.SendData parseit(Command)
End Sub
The above code is a representation, I've obviously left out variables and such, and just showed how I was working with the events. With the above code, the event works,
but only for the person who raised the event. I think it has something to do with Set ... = New ... statement. I don't remember where I saw it, but I thought I read something
that stated that there is a problem with mulitple objects that contain an instance of the event handler object not being able to see the events generated by the other
copies or some such... As you can see from this last statement, events have somewhat confused me heh
The question now is, even though i can get a user to deal with his own events, is there a way to trigger an event in the other user objects so that when one person sends a
command, the appropriate event will fire for all the others so they can recieve the message? A quick example of what I'm asking follows:
There are 4 players: A, B, C, & D
1 -- Player A sends the command: chat how is everyone.
2 -- The player object recives this, and sends this command to the event handler object which raises the event PlayerChat.
3 -- Having raised the event, the following happens.
4 -- The Player objects for players B, C, & D see that the PlayerChat event has been triggered, in each object, the Handler_PlayerChat sub fires, sending: PlayerA: how is
everyone. to Socket.SendData
5 -- The player object for Player A sees the event has been triggered and the Handler_PlayerChat sub fires, sending: You Chat: how is everyone. to Socket.SendData
I'm hoping someone can help me out as it's rather unstable to 1-rely on for ... next loops, especially if more players are logged on, and 2-there has been quite a bit of
instability on behalf of the collection and for ... next loops which causes the server to crash.
If I can get the event handler working, without causing conflicts with input and output, I will worry less about having to loop through everything as much as I am now.
Update: I tried to use a Globally declared EventHandler to see if that would work and it didn't.
Does _anyone_ have an idea they might suggest?
Thanks for reading my book,
Excalibur
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|