简体   繁体   English

Twisted聊天客户端只在发送消息后直接收到消息?

[英]Twisted chat client only receives message directly after sending a message?

I am writing a simple command line chat program with the Twisted framework. 我正在使用Twisted框架编写一个简单的命令行聊天程序。 In three separate command lines I have opened my chat server and two clients (see code below). 在三个单独的命令行中,我打开了聊天服务器和两个客户端(请参阅下面的代码)。

The problem I am having is that if I send a message from one client the next client won't receive it. 我遇到的问题是,如果我从一个客户端发送消息,下一个客户端将不会收到它。 However once that other client sends its own message it receives the message from the other client. 但是,一旦其他客户端发送自己的消息,它就会从另一个客户端收到消息。 Hope this is clear. 希望这很清楚。

Thanks 谢谢

Server: 服务器:

from twisted.protocols import basic

class MyChat(basic.LineReceiver):
    def connectionMade(self):
        print "Got new client!"
        self.factory.clients.append(self)

    def connectionLost(self, reason):
        print "Lost a client!"
        self.factory.clients.remove(self)

    def lineReceived(self, line):
        data = repr(line)
        print data
        for c in self.factory.clients:
            c.message(data)

    def message(self, message):
        self.transport.write(message + '\r\n')



from twisted.internet import reactor, protocol
from twisted.application import service, internet

factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []
reactor.listenTCP(8042, factory)
reactor.run()

Client: 客户:

from twisted.protocols import basic


# a client protocol

class EchoClient(basic.LineReceiver):

    def sendData(self):
        data = raw_input("> ")
        if data:
            print "sending %s...." % data
            self.transport.write(data + "\r\n")
        else:
            self.transport.loseConnection()

    def connectionMade(self):
        self.username = raw_input("Name: ")
        self.sendData()

    def lineReceived(self, data):
        print data
        self.sendData()

    def connectionLost(self, reason):
        print "connection lost"

class EchoFactory(protocol.ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"
        reactor.stop()


# this connects the protocol to a server runing on port 8000
def main():
    f = EchoFactory()
    reactor.connectTCP("localhost", 8042, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

Your sendData is blocking. 您的sendData正在阻止。 You can think of Twisted as a big while True loop that checks each cycle whether or not there are things to do. 您可以将Twisted视为一个很大的while True循环,它会检查每个循环是否有事情要做。 So once you enter the sendData function and call raw_input you in effect are stopping your whole program. 因此,一旦您输入sendData函数并调用raw_input您实际上就是停止整个程序。

Have a look at this question 看看这个问题

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

相关问题 Autobahn twisted websocket 服务器不会向客户端发送消息,除非它收到来自客户端的消息 - Autobahn twisted websocket server does not send messages to client unless it receives a message from a client 扭曲,在收到客户请求后返回自定义消息给客户端 - twisted , return custom message to client after receiving request from client MCCScript 仅在 Discord webhook 中发送聊天消息的第一个字 - MCCScript only sending the first word of chat message in Discord webhook 在Twisted中从一台服务器向另一台服务器发送消息 - Sending message from one server to another in Twisted Django频道-在consumers.py的循环中调用Group.send()时,客户端仅收到一条消息 - Django Channels - Client receives only one message when Group.send() is called in a loop in consumers.py 使用Python将聊天消息发送到LAN游戏 - Sending a chat message to a LAN game with Python 向 twitch 聊天发送消息不起作用 - Sending a message to twitch chat doesnt work 使用参数向客户端发送Websocket消息 - Sending websocket message to client with parameter zeromq:为什么经销商发送一条消息并从一位客户接收? - zeromq: why dealer sends one message and receives from one client? 套接字消息客户端/服务器:客户端不发送消息 - Socket Messaging Client/Server: Client not sending message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM