Client can safely close the socket & program
This commit is contained in:
parent
dc5510a9b1
commit
3f8aeadb71
17
draw.py
17
draw.py
@ -57,9 +57,12 @@ def sendDraw(sock):
|
|||||||
|
|
||||||
clock.tick(30)
|
clock.tick(30)
|
||||||
|
|
||||||
|
startFlag = True
|
||||||
|
|
||||||
def guessInput(screen,sock):
|
def guessInput(screen,sock):
|
||||||
|
global startFlag
|
||||||
guessStr = ""
|
guessStr = ""
|
||||||
while True:
|
while startFlag:
|
||||||
for event in pygame.event.get() :
|
for event in pygame.event.get() :
|
||||||
if event.type== pygame.QUIT: # close GUI
|
if event.type== pygame.QUIT: # close GUI
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
@ -69,8 +72,6 @@ def guessInput(screen,sock):
|
|||||||
guessStr = guessStr + chr(event.key)
|
guessStr = guessStr + chr(event.key)
|
||||||
elif event.key == 13: #enter,send to server,clean
|
elif event.key == 13: #enter,send to server,clean
|
||||||
sock.send(guessStr.encode('utf-8'))
|
sock.send(guessStr.encode('utf-8'))
|
||||||
ans = sock.recv(1024).decode('utf-8')
|
|
||||||
print(ans)
|
|
||||||
guessStr = ""
|
guessStr = ""
|
||||||
elif event.key == 8 : #backspace
|
elif event.key == 8 : #backspace
|
||||||
guessStr = guessStr[0:-1]
|
guessStr = guessStr[0:-1]
|
||||||
@ -82,6 +83,7 @@ def guessInput(screen,sock):
|
|||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
def receiveDraw(sock):
|
def receiveDraw(sock):
|
||||||
|
global startFlag
|
||||||
white= (255, 255, 255)
|
white= (255, 255, 255)
|
||||||
black= (0, 0, 0)
|
black= (0, 0, 0)
|
||||||
|
|
||||||
@ -101,8 +103,13 @@ def receiveDraw(sock):
|
|||||||
guessThreading.setDaemon(False)
|
guessThreading.setDaemon(False)
|
||||||
guessThreading.start()
|
guessThreading.start()
|
||||||
|
|
||||||
while True:
|
while startFlag:
|
||||||
data = sock.recv(1024).decode('utf-8')
|
data = sock.recv(1024).decode('utf-8')
|
||||||
|
if data == 'exitok':
|
||||||
|
startFlag = False
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
print(data)
|
||||||
li = data.split('+') # 送來的座標可能一次有多個,規範以+隔開
|
li = data.split('+') # 送來的座標可能一次有多個,規範以+隔開
|
||||||
for i in li:
|
for i in li:
|
||||||
if i!="":
|
if i!="":
|
||||||
@ -118,5 +125,5 @@ def receiveDraw(sock):
|
|||||||
# pygame.draw.circle(screen, black, pos, 5, 0)
|
# pygame.draw.circle(screen, black, pos, 5, 0)
|
||||||
pygame.draw.circle(screen,black,pos,5,0)
|
pygame.draw.circle(screen,black,pos,5,0)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
sock.close()
|
||||||
|
|
||||||
3
main.py
3
main.py
@ -130,6 +130,9 @@ class Room:
|
|||||||
else: # it is from other client. He/she want to send answer to check the answer
|
else: # it is from other client. He/she want to send answer to check the answer
|
||||||
if data == self.problem:
|
if data == self.problem:
|
||||||
sock.send('y'.encode('utf-8'))
|
sock.send('y'.encode('utf-8'))
|
||||||
|
elif data == 'exit':
|
||||||
|
sock.send('exitok'.encode('utf-8'))
|
||||||
|
self.sockList.remove(sock)
|
||||||
else:
|
else:
|
||||||
sock.send('n'.encode('utf-8'))
|
sock.send('n'.encode('utf-8'))
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user