我正在关注这个:http ://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server教程。
我让这些东西工作得很好,并且在这方面完全是个菜鸟,我一生都无法弄清楚如何将 Python 布尔值添加到教程中提到的“chatserver.py”文档中,粘贴在下面。
我不仅想添加一个,还想用一个按钮从 iPhone 应用程序切换它,然后从服务器请求知道它。
比如,一个切换布尔值的按钮(如何发送切换消息)以及如何询问服务器布尔值的状态是什么
为这个非常开放的问题道歉。
这是当前服务器:(Uber 复杂,哇!)
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
class IphoneChat(Protocol):
def connectionMade(self):
#self.transport.write("""connected""")
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 "data is ", data
a = data.split(':')
if len(a) > 1:
command = a[0]
content = a[1]
msg = ""
if command == "iam":
self.name = content
msg = self.name + " has joined"
elif command == "msg":
msg = self.name + ": " + content
print msg
for c in self.factory.clients:
c.message(msg)
def message(self, message):
self.transport.write(message + '\n')
factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(80, factory)
print "Iphone Chat server started"
reactor.run()