客户端不连接到队列,如何处理多个客户端进行在线游戏

时间:2019-07-27 01:11:17

标签: python sockets server client client-server

我正在使用pygame和python 3.7.3编写游戏,并且在编写完整的队列服务时遇到管理问题。目标是将每个已连接的用户成对分布,以便他们可以玩游戏。

但是,每次我打开2个客户端时,它们都会卡在等待服务器上的“确定”。

如果我打开其他客户端,则一两个完成任务。

这是服务器:

import socket
import os, sys
import time
import random
import threading


clients_list = []
matchmaking = []

def CoreGame(match):

    try:
        match[0]["connection"].sendall("OK".encode())
        match[1]["connection"].sendall("OK".encode())
    except:
        CoreGame(match)

def Handler():
    global clients_list
    global matchmaking
    for client in clients_list:
        conn = client["connection"]
        try:
            res = conn.recv(64)
        except:
            clients_list.remove(client)
            continue
        message = res.decode()
        if message.find("queue") != -1:
            matchmaking.append(client)
            if len(matchmaking) == 2:
                thread = threading.Thread(name="Game", target=CoreGame(matchmaking))
                thread.start()
                clients_list.remove(matchmaking[0])
                clients_list.remove(matchmaking[1])
                matchmaking = []


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.bind(("127.0.0.1", 4269))
sock.settimeout(2)

sock.listen(5)


client_dict = {}

def main(client_dict):
    global clients_list
    try:
        connection, address = sock.accept()
        client_dict["connection"] = connection
        client_dict["address"] = address

        if len(clients_list) > 0:
            for client in clients_list:
                if client["address"][1] is address[1]:
                    clients_list.remove(client)
                    return
        clients_list.append(client_dict) 
        print(clients_list)
        thread = threading.Thread(name="Handler", target=Handler())
        thread.start()
    except:
        return


while True:
    main(client_dict)

这是客户:

import pygame
from Cards import Cards
from Graphics import Graphics

graphics = Graphics()


import random
import time
import socket
import sys, os

def waitUntilExit():
    Exit = False
    while not Exit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                Exit = True

def sendingQueue(sock):
    try:
        sock.sendall("queue".encode())
    except:
        return 0
    try:
        res = sock.recv(64)
        if str(res.decode()).find("OK") != -1:
            return 1
        return 0
    except:
        return 0


try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    sock.connect(("127.0.0.1", 4269))
    sock.settimeout(2)
except:
    graphics.init()
    graphics.printConnectionError()
    waitUntilExit()
    pygame.quit()
    sys.exit(0)



graphics.init()
graphics.drawWaitingScreen()

res = sendingQueue(sock)
while res is not 1:
    sendingQueue(sock)
    #the code blocks here 


graphics.init()

graphics.drawGrids()
graphics.drawCardsBoxes()
#graphics.drawCemetery()
#graphics.drawReadyButton()

#load cards and shuffle decks
cards_handler = Cards()
cards_dictionary = cards_handler.load_images()
DECK = cards_dictionary["images"]
random.shuffle(DECK)

#event loop
Exit = False

while not Exit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Exit = True

图形和卡模块无关,因为它们可以正常工作。

0 个答案:

没有答案