This solution seems to work, it stores the handlers and the usernames. However I'm stuck at sending messages from one client to another. All clients can communicate with the server and server can communicate with the clients, but the problem appears when the server receives a message and tries to send it to another clients.
What I did is:
1. SERVER: Server starts
2. SERVER: Client connects - server stores the handler in ConnectionList and I added my ConnectedUsersList(record of TWebsocketCommunicator and string), it add the handler to my array in
TSocketHandler.DoHandleCommunication3. CLIENT: right after
chat.FCommunicator.StartReceiveMessageThread, if
chat.FCommunicator.Open is true, it sends a message to the server providing the client's username (each client has its unique username).
4. SERVER: Server receives this message (TSocketHandler.MessageReceived) and updates my array adding the username to the handler.
5. Steps 2-4 for every client (I've tested with just 3)
At this point all clients are able to talk to the server and receive messages from the server.
But if a client sends a message that's supposed to be send to another client, I come across a problem. Some client (apparently the last one) can send messages and these are delivered to another client with no problem but others can't.
How I did it:
1. Client A sends a message that's supposed to get to Client B.
2. Server receives the message (TSocketHandler.MessageReceived). The message contains the Client B's username .
3. Server looks up my array to find the B's username:
for i:=0 to length(ConnectedUsersList)-1 do if ConnectedUsersList[i].UserID=ClientB then ConnectedUsersList[i].Handler.WriteStringMessage.....
4. Client B receives the message.
I use locks whenever I use the connection list or send messages.
I performed some tests:
1#
-Client A connects
-Client B connects
-Client A sends a message to the Server (the message doesn't reach the server)
-Client B sends a message to the Server (the message goes through)
-Server sends a message to Client A (the message goes through)
-Server sends a message to Client B (the message goes through)
at this point Server can send messages to both clients but only Client B can send messages to the Server
-Client B sends a message to Client A (it goes through)
-Client A sends a message to Client B (the message doesn't even reach the Server)
at this point Server can still send message to both Clients but only Client B can send to the Server.
What's even more disturbing, Client B's GUI closes properly, Client A's GUI doesn't want to close, I need to kill the process.
2#
-3 clients connect
-Server can send messages to all 3 clients
-Only Client C can send message to the server
I've tried to do the same using my ConnectedUserList and the original ConnectionList. Another solution was adding
UserID: string variable to
TWebsocketCommunicator class (wsstream), since it's threaded, each thread keeps it's UserID. No matter which solution I use, the result is the same.
any idea what's wrong?