简体   繁体   English

Twisted服务器/客户端之间的多个呼叫/响应消息

[英]Multiple call/response messages between Twisted server/client

I have a basic server and client implemented in Twisted. 我有一个在Twisted中实现的基本服务器和客户端。 My goal is to have the client process some data, report its progress back to the server, and repeat until all the data is processed. 我的目标是让客户端处理一些数据,将其进度报告给服务器,然后重复进行,直到处理完所有数据为止。 The client is able to send an initial message to the server but it is not receiving the server's response letting it know it is ok to start processing that data. 客户端能够向服务器发送初始消息,但是客户端没有收到服务器的响应,让它知道可以开始处理该数据了。 Here is the code I have. 这是我的代码。

Server: 服务器:

from twisted.internet import reactor, protocol

PORT = 9000

progress = 0

class MyServer(protocol.Protocol):

    def dataReceived(self, data):
        global progress
        print(data)
        if data != "Ready?":
            progress = int(data)
        self.transport.write("Got it.")
        self.transport.loseConnection()

    def connectionLost(self, reason):
        global progress
        if progress == 10:
            print("Completed.")
            reactor.stop()

class MyServerFactory(protocol.Factory):
    protocol = MyServer

factory = MyServerFactory()
reactor.listenTCP(PORT, factory)
reactor.run()

Client: 客户:

from twisted.internet import reactor, protocol
import time

HOST = 'localhost'
PORT = 9000

progress = 0

class MyClient(protocol.Protocol):

    def connectionMade(self):
        self.transport.write("Ready?")
        self.transport.loseConnection()

    def dataReceived(self, data):
        global progress
        progress += 1
        print(progress)
        self.transport.write(str(progress))
        self.loseConnection()

    def connectionLost(self, reason):
        global progress
        if progress == 10:
            print("Completed.")
            reactor.stop()

class MyClientFactory(protocol.ClientFactory):
    protocol = MyClient

factory = MyClientFactory()
reactor.connectTCP(HOST, PORT, factory)

reactor.run()

I figured out that my issue was that I was prematurely closing the connection. 我发现我的问题是我过早关闭了连接。 In some earlier testing I was trying to send multiple messages within the dataReceived function which were getting concatenated into a single message. 在一些较早的测试中,我试图在dataReceived函数中发送多个消息,这些消息被串联为一条消息。 This led me to believe that you must lose the connection for each message to go through. 这使我相信,要通过每条消息,您都必须断开连接。 Rather, you simply need to let the function finish before sending another message. 相反,您只需要在发送另一条消息之前让函数完成即可。 Here is the updated code that is working as intended. 这是按预期工作的更新代码。

Server: 服务器:

from twisted.internet import reactor, protocol

PORT = 9000

progress = 0

class MyServer(protocol.Protocol):

    def dataReceived(self, data):
        global progress
        print(data)
        if data != "Ready?":
            progress = int(data)
        self.transport.write("Got it")
        if progress == 10:
            self.transport.loseConnection()

    def connectionLost(self, reason):
        print("Completed.")
        reactor.stop()

class MyServerFactory(protocol.Factory):
    protocol = MyServer

factory = MyServerFactory()
reactor.listenTCP(PORT, factory)
reactor.run()

Client: 客户:

from twisted.internet import reactor, protocol
import time

HOST = 'localhost'
PORT = 9000

progress = 0

class MyClient(protocol.Protocol):

    def connectionMade(self):
        self.transport.write("Ready?")

    def dataReceived(self, data):
        global progress
        progress += 1
        print(progress)
        self.transport.write(str(progress))
        if progress == 10:
            self.transport.loseConnection()

    def connectionLost(self, reason):
        print("Completed.")
        reactor.stop()

class MyClientFactory(protocol.ClientFactory):
    protocol = MyClient

factory = MyClientFactory()
reactor.connectTCP(HOST, PORT, factory)

reactor.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM