I'm trying to make use a websocket client in python 2.7. The client works fine when running with IDLE, as in the received messages are shown. However trying to insert the websocket messages in a tkinter text widget is not working. Messages are still being displayed in IDLE, but the tkinter window is not being displayed at all.
The package I'm using is called websocket-client 0.32.0 and I dowloaded it from here. I followed the instructions on how to make a javascript like api and somewhat adapted the code.
Here is my code:
from Tkinter import *
from websocket import *
master = Tk()
master.wm_title("Websocket Test")
minwidth = master.winfo_screenwidth()/4*3
minheight = master.winfo_screenheight()/4*3
master.minsize(width=minwidth, height=minheight)
master.resizable(0,0)
text = Text(master)
text.pack(expand=True,fill=BOTH)
def on_message(ws, message):
text.insert(END, message+"\n")
print "Received: "+message
return
def on_error(ws, error):
text.insert(END, error+"\n")
print error
return
def on_close(ws):
text.insert(END, "### closed ###\n")
print "### closed ###"
return
def on_open(ws):
ws.send("hi")
ws.send("test")
return
enableTrace(True)
ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
ws.on_open = on_open
ws.run_forever()
master.mainloop()
Here is what is shown in IDLE if that is also helpful:
--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: echo.websocket.org
Origin: http://echo.websocket.org
Sec-WebSocket-Key: tXWAVVlaRoq2S4+p/z12gg==
Sec-WebSocket-Version: 13
-----------------------
--- response header ---
HTTP/1.1 101 Web Socket Protocol Handshake
Connection: Upgrade
Date: Fri, 21 Aug 2015 05:16:54 GMT
Sec-WebSocket-Accept: LH12LFLFaek6HgCnGIugF0sg9lA=
Server: Kaazing Gateway
Upgrade: websocket
-----------------------
send: '\x81\x82{b\x97\xfc\x13\x0b'
send: '\x81\x84\xa5J\xecf\xd1/\x9f\x12'
Received: hi
Received: test
I've been stuck on this for a while and I cannot find any solutions to this sort of problem. Any help is appreciated as I'm a newbie.