Socket Thread and PyGTK

I'm trying to write an instant messenger, the base ui is almost done, and I am looking at the receiving part of the messages. I have a UI class and a Receive_Socket stream class. Each time a Received_Socket class socket receives a message, it calls gobject.idle_add () to invoke the user interface method to display the message in the chat window. After the line gobject.idle.add (), I have a while loop whose loop is actually shown in the chat window (I want the message to be displayed before receiving another message, because I read that gobject.idle_add () does not guarantee execution order, and, of course, I want the messages to be displayed in order :))

I tried to summarize my code:

User interface class:

Class UI:
##### somewhere in the init #####
    self.message_status = 0
def chat_window(self, contact, message=0):
    Check if a chat window is opened for the contact, if not it opens one.
    => In reality this check if a gtk.Window containing a gtk.Notebook is opened, 
    => if not it opens one
    => Then it creates a page for the contact in the notebook, containing a 
    => gtk.Textview which displays messages
    if message:
        self.message_display(contact, message)
def message_display(self,a,b):
    => Display the message in the message in the contact textview
    self.message_status = 1

Threaded Receive_Socket class:

Class Receive_Socket(threading.thread):
    message = sock.recv(1024)
    => the sender name is written in the message 
    if message:
        gobject.idle_add(myui.chat_window,sender, message)
        while not myui.message_status:
            time.sleep(0.1)
        myui.message_status = 0

:

if __name__ == "__main__":
    myui = UI()
    reception = Receive_Socket()
    reception.start()
    gtk.main()

:

1) ? ( ), ?

2) 2 , , = sock.recv(1024), . , , 1024 1 , , message_buffer, , sock.recv(1024), , message_buffer , , message_buffer sock.recv(1024). / ?

,

Nolhian

+3
1
+3

Source: https://habr.com/ru/post/1780503/


All Articles