Python PodSixNet 错误

Python PodSixNet Error

我正在尝试在我的一款游戏中使用名为 PodSixNet 的多人游戏模块,但在尝试 运行 客户端时 assyncore.py 出现错误。但是,服务器运行完美。我开发了简单的测试客户端和服务器程序,但是当我 运行 客户端时,我仍然得到同样的错误。这里分别是我的客户端和服务器程序:

import PodSixNet, time
from PodSixNet.Connection import ConnectionListener, connection
from time import sleep

class MyNetworkListener(ConnectionListener):

    connection.Connect()


    def Network(self, data):
        print data


gui = MyPlayerListener()
while 1:
    connection.Pump()
    gui.Pump()

.

import PodSixNet, time
from time import sleep
from PodSixNet.Channel import Channel
from PodSixNet.Server import Server

class ClientChannel(Channel):

    def Network(self, data):
        print data

class MyServer(Server):

    channelClass = ClientChannel

    def Connected(self, channel, addr):
        print "new connection:", channel

myserver = MyServer()

while True:
    myserver.Pump()
    sleep(0.0001)

这是我 运行 客户端时返回的错误:

    Traceback (most recent call last):
      File "C:\Users\Matt\Desktop\The 37th Battalion\PodSixNet Tests\client.py",  line 5, in <module>
        class MyNetworkListener(ConnectionListener):
      File "C:\Users\Matt\Desktop\The 37th Battalion\PodSixNet Tests\client.py",   line 7, in MyNetworkListener
    connection.Connect()
  File "C:\Python27\lib\asyncore.py", line 418, in __getattr__
    retattr = getattr(self.socket, attr)
  File "C:\Python27\lib\asyncore.py", line 418, in __getattr__
    retattr = getattr(self.socket, attr)
  File "C:\Python27\lib\asyncore.py", line 418, in __getattr__
    retattr = getattr(self.socket, attr)
  File "C:\Python27\lib\asyncore.py", line 418, in __getattr__
    retattr = getattr(self.socket, attr)

此错误一直持续到我达到最大递归深度。将不胜感激。

谢谢, 大卫

首先,你必须告诉服务器它应该监听哪个端口:

server.py

import PodSixNet, time
from time import sleep
from PodSixNet.Channel import Channel
from PodSixNet.Server import Server

class ClientChannel(Channel):

    def Network(self, data):
        print data

class MyServer(Server):

    channelClass = ClientChannel

    def __init__(self, *args, **kwargs):
        Server.__init__(self, *args, **kwargs)

    def Connected(self, channel, addr):
        print "new connection:", channel

# use the localaddr keyword to tell the server to listen on port 1337
myserver = MyServer(localaddr=('localhost', 1337))

while True:
    myserver.Pump()
    sleep(0.0001)

然后,客户端必须连接到这个端口:

client.py

import PodSixNet, time
from PodSixNet.Connection import ConnectionListener, connection
from time import sleep

class MyNetworkListener(ConnectionListener):

    def __init__(self, host, port):
        self.Connect((host, port))

    def Network(self, data):
        print data

# tell the client which server to connect to
gui = MyNetworkListener('localhost', 1337)
while 1:
    connection.Pump()
    gui.Pump()

您不必在 connection 上调用 Connect(),而是在 ConnectionListener 实例上调用。