Use the Python Tornado framework to implement a one-to-one chat program, pythontornado
Chat by train of thought:
Similarly, click a user to go to the one-to-one chat page. The chat box list contains all one-to-one chat boxes with chat records. Click to enter the chat page.
[Data structure]
Because both parties have chat records, each chat actually needs to be stored in two copies. The designed data structure is as follows:
A:
user_a = {“id”:1,”name”:”A”}
B:
user_b = {“id”:2,”name”:”B”}
Chat record of:
chat_a = { “id”:1, “user”:1, “who”:2, “name”:”B”, “new”:0, msg:[]}
B's chat record:
chat_b = { “id”:2, “user”:2, “who”:1, “name”:”A”, “new”:0, msg:[]}
Msg is actually a list with the following structure: msg = {"user": sender id, "name": sender name, "date": sending time, "content": Message content}
[Business logic]
Click B's name in the friend list at a to enter the chat box (locate chat_a, chat = coll Based on the id of both parties through the user and who fields. find_one ({"user": user_a ['id'], "who": user_ B ['id']}); If the chat does not exist, use the id of both parties to create chat_a)
Send messages (update chat_a and chat_ B. If chat_ B does not exist, create chat_ B. If chat_ B is not online, update chat_ B ['new'] = 1)
A. Delete the chat box (delete chat_a)
[Record client connection]
Because there are multiple one-to-one chats, you cannot directly use the set in the tutorial to record connections.
The final decision is to use a dict, use the string spliced by both user IDs as the key, and use the list client to connect.
... SocketHandler (...):
Chats = dict ()... def on_open (self ):... # generate a unique string min = user_a ['id'] max = user_ B ['id'] if min> max: max = user_a ['id'] min = user_ B ['id'] key = str (user_a ['id']) + "_" + str (user_ B ['id']) # determine whether the current session exists. if yes, add the current user if key in chats: SocketHandler. chats [key]. append (self) # creates a session if it does not exist and adds the current user to else SocketHandler. chats [key] = [self]
[Send message]
Call the send function from the client, accept the parameters in the on_message function on the server, and update the chat records of both parties. Call send_to_all (key, message) to update the chat window.
[Send a notification/update chat window]
After updating the chat records in the database, you must update the html in the chat window. Therefore, you must notify the connection person of the session.
According to the method in which we record the connector, the corresponding notification function is as follows:
def send_to_all(key,message): for user in SocketHandler.chats[key]: user.write_message(json.dumps(message))
Close connection]
Based on how we record the connector, the corresponding close function is as follows:
Def on_close (self ):... # construct the key if key in SocketHandler using the method in the on_open function. chats: SocketHandler. chats [key]. remove (self) # Delete the current connection if len (SocketHandler. chats [key]) = 0: del SocketHandler. chats [key] # delete a session if the session has no connection
After the above transformation, we can implement multiple one-to-one chat functions.