连接到服务器后,Corona 模拟器停止工作。

我有两个服务器文件与 Corona 模拟器一起工作。一个可以工作,但另一个不能。不确定这两个文件之间有什么不同。下面是我的服务器代码。

未工作:

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()

工作中:

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()

我把所有的代码放在一起,因为我担心会错过代码中的一些重要内容。 感谢即将到来的帮助。

点赞
用户1376249
用户1376249

你需要在消息末尾发送 "\n"。

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()

这是 HTTP 协议的要求。

2014-09-06 16:30:27