python-websocket-server icon indicating copy to clipboard operation
python-websocket-server copied to clipboard

Sending to a specific client

Open Jawmo opened this issue 5 years ago • 6 comments

I noticed that you have instructions to send to a specific client, but when I print our the value of "client", I just get back the current user's information. If I have to use "client" when I use send_message(), how do I actually specify the client ID?

I have to use: send_message(client, "message here.)

If I change "client" to "client['id']" or something, it won't work. How do you actually specify which use it should go to?

Jawmo avatar Oct 11 '18 14:10 Jawmo

I just do a for loop looping over all the clients and selecting only the one i want. Here's my function:

def sendMessageToClient(name, message):
	targetClient = None
	for oneClient in server.clients:
		if oneClient['id'] == name:
			targetClient = oneClient
			break
	if targetClient is not None:
		server.send_message(targetClient, message)

Hope it helps

DavidSteinmann avatar Aug 13 '20 10:08 DavidSteinmann

This is a bad design decision at the time - using a list instead of a dict.

The workaround is to loop through the clients and once you find your client, use the handler handler.send_text

Pithikos avatar Jul 15 '21 10:07 Pithikos

Maybe there should be a server.clients_dict or something (in addition to the existing, to keep compatibility)?

TheTechRobo avatar Oct 10 '22 02:10 TheTechRobo

Maybe there should be a server.clients_dict or something (in addition to the existing, to keep compatibility)?

Yes, probably a clients_by_id with client ID as key. But not something I have the time currently to work on unfortunately

Pithikos avatar Oct 10 '22 14:10 Pithikos

I might pick this up. Shouldn't be too difficult.

TheTechRobo avatar Oct 10 '22 15:10 TheTechRobo

I just do a for loop looping over all the clients and selecting only the one i want. Here's my function:

def sendMessageToClient(name, message):
	targetClient = None
	for oneClient in server.clients:
		if oneClient['id'] == name:
			targetClient = oneClient
			break
	if targetClient is not None:
		server.send_message(targetClient, message)

Hope it helps

Thanks for that. It helped me to solve this problem.

AsgJeold avatar Oct 16 '22 10:10 AsgJeold