0

Twisted のサンプル チャット サーバーに大まかに基づいたサーバーがあります。telnetで動作します。ここで、クライアントを接続する必要があります。サンプルのワンタイム クライアントをコピーしました。問題は、サーバーが「会話」を開始するはずの「hello」文字列を受信しないことです。FAQ でよくある間違いはリアクターの実行をブロックすることだと読みましたが、以下のコードのどこでそれを行うことができるかわかりません。誰が何が悪いのか教えてもらえますか?

from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint

class Greeter(Protocol):
    def sendMessage(self, msg):
        self.transport.write("%s\n" % msg)

class GreeterFactory(Factory):
    def buildProtocol(self, addr):
        return Greeter()

def gotProtocol(p):
    p.sendMessage("hello")
    reactor.callLater(1, p.sendMessage, "/p2")
    reactor.callLater(2, p.transport.loseConnection)

point = TCP4ClientEndpoint(reactor, "localhost", 8123)
d = point.connect(GreeterFactory())
d.addCallback(gotProtocol)
reactor.run()
4

1 に答える 1

2

わかった。これが他の誰かに役立つことを願っています。上記の私の問題は、私のサーバー(lineReceivedを使用):

    def lineReceived(self, line):
        print "line received: %s" % line
        if self.state == "GETNAME":
            self.handle_GETNAME(line)
        else:
            self.handle_CHAT(line)

キャリッジ リターンとラインフィードが必要です。したがって、上記の sendMessage を次のように変更します。

    def sendMessage(self, msg):
        self.transport.write("%s\r\n" % msg)

それは今魔法のように動作します!イライラするほどシンプル。

于 2013-02-18T16:04:35.113 に答える