Corona Simulator stop working after connecting to

2019-09-07 23:07发布

问题:

I have 2 server file work with corona simulator. One is work but another isn't. Not sure what is the different between these 2 file. Below is my server code.

Non-working:

class Chat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
    def connectionLost(self, reason):
        self.factory.clients.remove(self)
    def dataReceived(self,data):
        for c in self.factory.clients:
            c.message(data)
            print data
    def message(self, data):
        self.transport.write(data)

factory = Factory()
factory.clients = []
factory.protocol = Chat

reactor.listenTCP(8080,factory)
reactor.run()

Working:

class IphoneChat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
        print "Clients are " ,self.factory.clients
    def connectionLost(self, reason):
        self.factory.clients.remove(self)
    def dataReceived(self, data):
        print "The data is " ,data
        for c in self.factory.clients:
            c.message(data)
    def message(self, message):
        self.transport.write(message + '\n')

factory = Factory()
factory.clients = []
factory.protocol = IphoneChat
reactor.listenTCP(8080, factory)
print "Server Start!!!"
reactor.run()

I put all of my code because I afraid of missing something important about the code. Thank you for incoming help.

回答1:

You need to send "\n" at the end of message

class Chat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
    def connectionLost(self, reason):
        self.factory.clients.remove(self)
    def dataReceived(self,data):
        for c in self.factory.clients:
            c.message(data)
            print data
    def message(self, data):
        self.transport.write(data + '\n')

factory = Factory()
factory.clients = []
factory.protocol = Chat

reactor.listenTCP(8080,factory)
reactor.run()

Its HTTP protocol requirement.