【问题标题】:broadcastMessage in Twisted chat server扭曲聊天服务器中的广播消息
【发布时间】:2017-08-22 14:31:41
【问题描述】:

我正在 Twisted 中编写聊天服务器,但在理解 broadcastMessage() 方法时遇到问题:

def broadcastMessage(self, message):
    print list(self.factory.users.iteritems())

    for name, protocol in self.factory.users.iteritems():
        if protocol != self:
            protocol.sendLine(message)

我知道 iteritems() 应该产生一个元组,例如('Roman', <__main__.ChatProtocol instance at 0x7fc80b8b67a0>)。现在,当遍历这个元组 namesprotocols 时,我们正在比较 protocol 如果它不是 self 实例,仅仅是因为我们不想为发送它的用户打印消息? (我说对了吗?)

所以期望它可以工作,但由于某种原因它没有。这是代码

from twisted.internet import protocol, reactor
from twisted.protocols.basic import LineReceiver

class ChatProtocol(LineReceiver):
    def __init__(self, factory):
        self.factory = factory
        self.name = None
        self.state = "REGISTER"

    def connectionMade(self):
        self.sendLine("What's your name?")

    def connectionLost(self, reason):
        if self.name in self.factory.users:
            del self.factory.users[self.name]
            self.broadcastMessage("%s has left the channel." % (self.name,))

    def lineReceived(self, line):
        if self.state == "REGISTER":
            self.handle_REGISTER(line)
        else:
            self.handle_CHAT(line)

    def handle_REGISTER(self, name):
        if name in self.factory.users:
            self.sendLine("Name taken, please choose another.")
            return
        self.sendLine("Welcome, %s!" % (name,))
        self.broadcastMessage("%s has joined the channel." % (name,))
        self.name = name
        self.factory.users[name] = self
        self.state = "CHAT"

    def handle_CHAT(self, message):
        message = "<%s> %s" % (self.name, message)
        self.broadcastMessage(message)

    def broadcastMessage(self, message):
        for name, protocol in self.factory.users.iteritems():
            if protocol != self:
                protocol.sendLine(message)

class ChatFactory(protocol.Factory):
    def __init__(self):
        self.users = {}

    def buildProtocol(self, addr):
        return ChatProtocol(self)

reactor.listenTCP(8000, ChatFactory())
reactor.run()

这是终端会话

(venv) metal@space ~/Documents/learning/twisted/chat_server $ telnet localhost 8000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
What's your name?
Roman
Welcome, Roman!

P.S.:我正在使用 Telnet 发送消息。

【问题讨论】:

    标签: python python-2.7 chat twisted


    【解决方案1】:

    你确定它不起作用吗?根据您的终端会话记录和本地测试,它看起来对我有用。

    在您的会话记录中,您输入了“Roman”这个名字并收到了“Welcome, Roman!”的响应。这是由于以下行:

    self.sendLine("Welcome, %s!" % (name,))
    

    与同一服务器的不同连接收到消息“Roman 已加入频道”。由于行:

    self.broadcastMessage("%s has joined the channel." % (name,))
    

    此外,broadcastMessage 调用无法向加入的用户发送“已加入”消息,因为在其调用时,“已加入”用户尚未添加到工厂的 users 字典中。

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      相关资源
      最近更新 更多