我开始学习扭曲,并试图完成我的第一个任务。我有下一个服务器代码:
class TextProtocol(Protocol):
def connectionMade(self):
text = self.factory.text
self.transport.write(text)
self.transport.loseConnection()
class TextServerFactory(ServerFactory):
protocol = TextProtocol
def __init__(self, text):
self.text = text
from twisted.internet import reactor
reactor.listenTCP(4444, TextServerFactory("Twisted"))
reactor.run()
此代码立即向客户端发送文本。但实际上我想通过信件发送数据:如果我们有很多客户机,服务器应该依次发送信件。我认为服务器日志应该是这样的:
Client #1 connected
Sending "T" to client #1
Sending "w" to client #1
Client #2 connected
Sending "T" to client #2
Sending "i" to client #1
Sending "w" to client #2
Sending "s" to client #1
Sending "i" to client #2
。。。。。你知道吗
如果我在协议中创建循环,它将是阻塞操作。 你能帮我做这个吗?你知道吗
看看
twisted.internet.task.LoopingCall
和twisted.internet.IReactorTime.callLater
。你知道吗TCP将按它认为合适的方式对事物进行分组/批处理/拆分(它试图优化连接),请参阅Glyph的this answer或这篇更详细的How can I force a socket to send the data in its buffer文章
如果你真的想这样做,并提醒你试图强迫TCP做它不想做的事情,那么Jean-Paul的答案是,如果你一次只写一个字符,每个字符之间有几秒钟的等待,那么你就可以在TCP上得到一个字符一个字符的刷新。你知道吗
相关问题 更多 >
编程相关推荐