【问题标题】:Corona Simulator stop working after connecting to serverCorona Simulator 连接到服务器后停止工作
【发布时间】:2014-09-06 16:16:57
【问题描述】:

我有 2 个与电晕模拟器一起使用的服务器文件。一个是工作,另一个不是。不确定这两个文件有什么不同。下面是我的服务器代码。

不工作:

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

我把我所有的代码都放了,因为我害怕错过一些关于代码的重要内容。 感谢您的帮助。

【问题讨论】:

    标签: python lua coronasdk twisted


    【解决方案1】:

    您需要在消息末尾发送“\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 协议要求。

    【讨论】:

    • 我只知道。非常感谢。 :D
    • 嗨 iBad,我尝试使用 Ruby 服务器而不是 Python。红宝石有类似的要求吗?我用 puts "aaa" 但似乎有同样的问题。
    • 嗨,它的协议要求不依赖于语言。 Corona 客户只是坐在那里等待'\n'
    • 我尝试在 puts 方法中添加 \n 但它仍然不起作用。这是我的代码是 Ruby stackoverflow.com/questions/25706661/…
    猜你喜欢
    • 2013-09-06
    • 2018-11-14
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多