def broadcast_data (sock, message):
#Do not send the message to master socket and the client who has send us the message
for socket in CONNECTION_LIST:
if socket != server_socket and socket != sock :
try :
socket.send(message)
except :
# broken socket connection may be, chat client pressed ctrl+c for example
socket.close()
CONNECTION_LIST.remove(socket)
該功能可以作爲一個起點,一個發送功能可以有插座他們與用戶名關聯
usernameSockets={}
usernameSockets[username]=socket_for_user_name
,那麼你的字典
def send_data (sock, message):
#send message to only one client
#sock is the socket of the client to send to
try :
sock.send(message)
except :
# broken socket connection may be, chat client pressed ctrl+c for example
sock.close()
CONNECTION_LIST.remove(socket)
那麼你可以調用send_data函數,如
send_data(usernameSockets[username],message)
在你鏈接的代碼中沒有獲取用戶名的方法,所以你必須讓客戶端發送他們的用戶名到服務器,當他們連接到能夠填充像我上面提到的字典。