我有两个服务器文件与电晕模拟器一起工作。一个是工作,但另一个不是。不确定这两个文件之间有什么不同。以下是我的服务器代码。
非工作:
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()
我把所有代码都放了,因为我害怕丢失一些关于代码的重要内容。 感谢您的帮助。
答案 0 :(得分: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协议要求。