Blender Multiplayer-将客户端广播到服务器

时间:2019-05-08 09:12:06

标签: python-3.x blender

我目前正在客户端对服务器IP进行硬编码,但是我确实希望客户端通过广播(或其他方法)从服务器获取IP。我将以下信息作为信息:

服务器/客户端:基于UDP 端口:需要手动填写

我已经尝试添加一些示例代码,但这在其他StackOverflow答案中无效。

这是服务器的一部分:

class Server:
    ip = socket.gethostbyname(socket.gethostname())
    def __init__(self,host = ip, port=11155):
        self.socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        self.socket.setblocking(False)
        self.socket.bind((host,port))

        self.addr_user = {}

    def receive(self):
        while True:
            try:
                data, addr = self.socket.recvfrom(1024)

                if not addr in self.addr_user:
                    user = User(data.decode())
                    scene = logic.getCurrentScene()
                    spawner = scene.objects["Spawner"]
                    avatar = scene.addObject("Avatar", spawner)
                    avatar.children[0]["Text"] = user.name
                    avatar["user"] = user

                    self.addr_user[addr] = user
                else:
                    user = self.addr_user[addr]
                    data = pickle.loads(data)
                    user.keyboard.updateState(data[0])
                    user.mousePosition.updateMousePosition(data[1])


            except socket.error:
                break

这是以下内容的客户端部分:

class Client:
    def __init__(self,server_ip ="IPOFSERVER GOES HERE", server_port=11155):
        self.socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        self.socket.setblocking(False)


        self.serv_addr = (server_ip,server_port)

        self.entities = {}

        self.main = self.state_sendName

def receive(self):
        while True:
            try:
                data, addr = self.socket.recvfrom(1024)

                state = pickle.loads(data)

                for k in state:

                    if not k in self.entities:

                        scene = logic.getCurrentScene()
                        spawner = scene.objects["Spawner"]
                        entity = scene.addObject(k[0], spawner)
                        entity.children[0]["Text"] = k[1]
                        self.entities[k] = entity
                    else:
                        entity = self.entities[k]

                    entity.worldPosition = Vector(state[k][0])
                    entity.worldOrientation = Vector(state[k][1])
                    getChildren(entity).worldOrientation = Vector(state[k][2])




            except socket.error:
                break

我希望,如果客户端开始启动,则将通过发送广播数据包以使“客户端”正在搜索服务器来启动“配对”过程。当服务器收到客户端的数据包时,服务器需要使用其IP进行响应,以便可以添加它并建立连接!

0 个答案:

没有答案