【发布时间】:2019-04-08 05:48:36
【问题描述】:
我有一个使用 TCP 和 Twisted 的服务器-客户端代码。我希望创建的第一个对等对象(按第一个连接的客户端的顺序)服务( 发送消息)未来即将到来的客户。所以我保存了第一个对等点(全局列表),并将它用于所有即将到来的连接,但它只服务于第一个客户端(它连接到),而忽略其他客户端。
我怎样才能让对等体同时为所有连接的客户端提供服务?(我将测试不超过 3 个客户端)。
def connectionMade(self):
global connectedList
if self.pt == 'client':
self.connected = True
else:
print "Connected from", self.transport.client
try:
self.transport.write('<connection up>')
except Exception, e:
print e.args[0]
self.ts = time.time()
reactor.callLater(5, self.sendUpdate)
connectedList.append(self.transport) # add peer object
def sendUpdate(self):
global updateCounter, connectedList
print "Sending update"
try:
updateCounter += 1
print(connectedList[0])
# Send updates through first connected peer
connectedList[0].write('<update ' + str(updateCounter) + '>')
except Exception, ex1:
print "Exception trying to send: ", ex1.args[0]
if self.connected == True:
reactor.callLater(5, self.sendUpdate)
【问题讨论】:
-
请给你的代码??
-
@sanduniYW 代码添加
标签: python tcp client-server twisted