Updating our ChatWindow class to send requests to the server

With all of the heavy lifting passed over to our other classes, the changes required to our ChatWindow class should be very simple.

Firstly, let's update it so that the messages we send are sent to the web service and stored in a conversation database:

def send_message(self, event=None):
message = self.text_area.get(1.0, tk.END)

if message.strip() or len(self.text_area.smilies):
self.master.requester.send_message(
self.master.username,
self.friend_username,
message,
)

message = "Me: " + message
self.messages_area.configure(state='normal')
self.messages_area.insert(tk.END, message)
...

Before adding the Me : and incorporating the smileys into our messages_area, we fire off a request to the web service containing our username, the friend's username, and the unaltered message. We can access our username by referring to the username attribute of the FriendsList class, which will be our master widget.

Likewise, instead of creating a requester per ChatWindow, we can just use the one from our master widget.

The rest of this method remains unchanged.

Yes, that's really all that we need to do here to get our ChatWindow class connected!

The last thing to add is the conversation history when the user opens up the chat window. For this, we will need two new methods.

We will add a method which updates the messages_area with a message without sending it back to the requester:

def receive_message(self, author, message):
self.messages_area.configure(state='normal')

if author == self.master.username:
author = "Me"

message_with_author = author + ": " + message

self.messages_area.insert(tk.END, message_with_author)
self.messages_area.configure(state='disabled')

This method looks like a very abridged version of send_message, since it will just receive a message and its author, swap the author's name to Me if the author matches the stored username, and then add it to our messages_area.

With that in place, we can grab the conversation history from our web service and add it to the messages_area with this method:

def __init__(self, master, friend_name, friend_username, friend_avatar, **kwargs):
...
self.load_history()

def load_history(self):
history = self.master.requester.prepare_conversation(self.master.username, self.friend_username)

if len(history['history']):
for message in history['history']:
self.receive_message(message['author'], message['message'])

When the ChatWindow is first loaded, we want to prepare it with the chat history or by initializing the conversation database if it does not already exist. We use the prepare_conversation method of the requester to achieve this.

Once we have the history, we loop over each message and pass the information to our receive_message method to add it to our messages_area.

That's it, our ChatWindow is now connected! Run your friendslist.py file, log in and open up a chat window with someone. Send them a few messages, then close the window. When you open it again, you should see your old messages are still there!

With that, this chapter comes to an end. Our FriendsList and ChatWindow are now both connected to our web service via the Requester class.

The current implementation works fine if two people want to close and re-open their chat window between each method, but this is obviously not very user-friendly. In the next chapter, we will look at getting messages to send in the background.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset