Reputation: 1
I'm trying to send a message to a specific individual on a public Socket-based Server using python.
Say Client 1, Client 2, Client 3 and Client 4 are all on the same chatroom server and Client 1 wants to send a message directly to Client 3 without Client 2 and Client 4 seeing it. How can I relay the message from Client 1 to Client 3 using the socket library? The only way I know how to transfer a message is by using socket.sendall(), and that clearly won't hide the message from Client 2 and Client 4.
I'm also trying to make multiple chatrooms within the server but that is a problem for a later time (ex.: clients 1,2,3 want to chat solely amongst themselves without having to look at the globalchat. How can I generate a chatroom with password access?).
Here is my code for the private messaging/regular messaging relay on the server side. (it's based off of a public template, I intend to alter it further later). Under the elif is the private "sending" of the message.
clientList = {
1:Client1
2:Client2
3:Client3
4:Client4
#later on I will make the code that separates the client's servername from a /w (name) (message). My thought process is that by comparing the typed name to the dictionary it should send a message to the user who matches - if there is no comparison, an error would pop up and say "this user is not active on the server. Please try again.". For now, I have code that detects "/w" at the start of a string, which you can find below.
}
def client_handler(connection):
connection.send(str.encode('You are now connected to the replay server... Type BYE to stop'))
while True:
data = connection.recv(2048)
message = data.decode('utf-8')
if message == 'BYE':
break
elif message == Regex.IsMatch(str,"^/w "):
reply = f'Server: {message}' # <<<PRIVATE MESSAGE SYSTEM HERE.
connection.sendall(str.encode(reply)) #<<<Instead of sendall I want to just send it to a specific user in the dictionary above.
reply = f'Server: {message}'
connection.sendall(str.encode(reply))
connection.close()
My inspiration is the /w and /party system in Valorant, which is what I'm trying to recreate (amongst other additions). I'm stuck and I need some help. Any advice?
Upvotes: 0
Views: 74