我正在使用扭曲的框架运行聊天服务器,它适用于一个组,但我需要多个组,以便用户可以加入特定组,他可以发送/接收消息到该特定组,因此如何创建不止一个小组或工作功能'加入室(自我,房间名)'和用户将被重定向到该组?
from twisted.internet import reactor,protocol
from twisted.protocols import basic
from twisted.internet.protocol import Protocol, ClientFactory
import time
def t():
return "["+ time.strftime("%H:%M:%S") +"] "
class EchoProtocol(basic.LineReceiver):
name = "Unnamed"
def connectionMade(self):
self.transport.write("WhiteNOISE"+"\n")
self.sendLine("Enter A name Below...")
self.sendLine("")
self.count = 0
self.factory.clients.append(self)
self.factory.group.append(self)
print t() + "+ Connection from: "+ self.transport.getPeer().host
def connectionLost(self, reason):
self.sendMsg("- %s left." % self.name)
print t() + "- Connection lost: "+ self.name
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
elif command == "quit":
self.transport.loseConnection()
return
elif command == "/ul":
self.chatters()
return()
print msg
self.sendMsg(msg)
def username(self, line):
for x in self.factory.clients:
if x.name == line:
self.sendLine("This username is taken; please choose another")
return
self.name = line
self.chatters()
self.sendLine("You have been connected!")
self.sendLine("")
self.count += 1
self.sendMsg("+ %s joined." % self.name)
print '%s~ %s connected as: %s' % (t(), self.transport.getPeer().host, self.name)
def chatters(self):
x = len(self.factory.clients) - 1
s = 'is' if x == 1 else 'are'
p = 'person' if x == 1 else 'people'
self.sendLine("There %s %i other %s connected:" % (s, x, p) )
for client in self.factory.clients:
if client is not self:
self.sendLine(client.name)
self.sendLine("")
def sendMsg(self, message):
for client in self.factory.clients:
client.transport.write( message + '\n')
class EchoServerFactory(protocol.ClientFactory):
protocol = EchoProtocol
clients = []
if __name__ == "__main__":
reactor.listenTCP(5001, EchoServerFactory())
print "Chat Server Started"
reactor.run()
答案 0 :(得分:1)
这是一些伪代码,我看到你已经在你的工厂创建了变量命名组,所以我猜你有类似的想法,但尝试使用字典而不是列表,这样你就可以更优雅地命名和访问你的组。
当然这意味着您的"msg"
命令应该指定了组,以便您知道将消息发送到哪个组。
class EchoServerFactory(protocol.ClientFactory):
protocol = EchoProtocol
clients = []
groups = {}
def dataReceived(self, data):
# Split string ,error checking ...
if command == "join":
self.factory.groups.get(content, []).append(self)
# handle other commands
def sendMsg(self, message, group):
for client in self.factory.groups[group]:
client.transport.write( message + '\n')
这实际上是微不足道的,例如没有回滚,因此新客户端看不到聊天记录,消息传递不可靠,因为您不使用任何类型的确认告知服务器客户端确实收到了消息,或者服务器应该将消息重新发送给某些客户端。
更好,更强大的解决方案可能包括:
以下是对reliable and unreliable的含义的简短说明。
可靠的消息传递是跨越消息传递消息的概念 不可靠的基础设施,同时能够做出某些保证 关于消息的成功传输;例如,那 如果邮件已传递,则最多只传递一次,或全部传递 成功发送的邮件按特定顺序到达。
Disclamer:我只提出了过去使用的技术和想法,可能还有数百种方法可以实现[简单,强大]群聊。