本文整理汇总了Python中server.Server.broadcastData方法的典型用法代码示例。如果您正苦于以下问题:Python Server.broadcastData方法的具体用法?Python Server.broadcastData怎么用?Python Server.broadcastData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类server.Server
的用法示例。
在下文中一共展示了Server.broadcastData方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ServerInst
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import broadcastData [as 别名]
#.........这里部分代码省略.........
print "User already exists"
user_found=True
data = {}
data[0] = "error"
data[1] = "User already exists"
self.server.sendData(data,package[1])
# send something back to the client saying to change username
if not user_found:
new_user={}
new_user['name']=package[0][1]
new_user['connection']=package[1]
new_user['ready']=False
new_user['new_dest']=False
new_user['new_spell']=False
self.users[len(self.users)]=new_user
data = {}
data[0] = "which"
data[1] = len(self.users)-1
self.server.sendData(data,package[1])
# else check to make sure connection has username
for u in range(len(self.users)):
if self.users[u]['connection']==package[1]:
print "Packet from "+self.users[u]['name']
# process packet
update_warlocks=False
# if chat packet
if package[0][0]=='chat':
print "Chat: "+package[0][1]
valid_packet=True
# Broadcast data to all clients ("username: message")
data = {}
data[0]='chat'
data[1]=self.users[u]['name']+": "+package[0][1]
self.server.broadcastData(data)
# else if ready packet
elif package[0][0]=='ready':
print self.users[u]['name']+" is ready!"
self.users[u]['ready']=True
valid_packet=True
update_warlocks=True
# else if unready packet
elif package[0][0]=='unready':
print self.users[u]['name']+" is not ready!"
self.users[u]['ready']=False
valid_packet=True
update_warlocks=True
if update_warlocks:
data = {}
data[0]='warlocks'
data[1]=len(self.users)
self.server.broadcastData(data)
# break out of for loop
break
#else:
# print str(self.users[u]['connection'])+" "+str(package[1])
if not valid_packet:
data = {}
data[0] = "error"
data[1] = "Please Login"
self.server.sendData(data,package[1])
print "User not logged in"
else:
print "Data in packet wrong size"
else:
print "Packet wrong size"
# if all players are ready and there is X of them
示例2: ServerInst
# 需要导入模块: from server import Server [as 别名]
# 或者: from server.Server import broadcastData [as 别名]
class ServerInst():
def __init__(self):
# Initialise Window
self.showbase=ShowBase()
# Disable Mouse Control for camera
self.showbase.disableMouse()
# Start our server up
self.server = Server(9099, compress=True)
self.users={}
for i in range(num_users):
new_user={}
new_user['name']='cake'
new_user['connection']='cake'
new_user['ready']=True
new_user['new_dest']=False
new_user['new_spell']=False
self.users[len(self.users)]=new_user
camera.setPos(0,0,45*num_users)
camera.lookAt(0,0,0)
#taskMgr.doMethodLater(0.5, self.pregame_loop, 'Lobby Loop')
taskMgr.doMethodLater(0.5, self.lobby_loop, 'Lobby Loop')
# handles new joining clients and updates all clients of chats and readystatus of players
def lobby_loop(self,task):
# if in lobby state
temp=self.server.getData()
if temp!=[]:
for i in range(len(temp)):
valid_packet=False
package=temp[i]
if len(package)==2:
print "Received: " + str(package) +" "+ str(package[1].getAddress())
if len(package[0])==2:
# if username is sent, assign to client
if package[0][0]=='username':
user_found=False
valid_packet=True
for u in range(len(self.users)):
if self.users[u]['name']==package[0][1]:
print "User already exists"
user_found=True
data = {}
data[0] = "error"
data[1] = "User already exists"
self.server.sendData(data,package[1])
# send something back to the client saying to change username
if not user_found:
new_user={}
new_user['name']=package[0][1]
new_user['connection']=package[1]
new_user['ready']=False
new_user['new_dest']=False
new_user['new_spell']=False
self.users[len(self.users)]=new_user
data = {}
data[0] = "which"
data[1] = len(self.users)-1
self.server.sendData(data,package[1])
# else check to make sure connection has username
for u in range(len(self.users)):
if self.users[u]['connection']==package[1]:
print "Packet from "+self.users[u]['name']
# process packet
update_warlocks=False
# if chat packet
if package[0][0]=='chat':
print "Chat: "+package[0][1]
valid_packet=True
# Broadcast data to all clients ("username: message")
data = {}
data[0]='chat'
data[1]=self.users[u]['name']+": "+package[0][1]
self.server.broadcastData(data)
# else if ready packet
elif package[0][0]=='ready':
print self.users[u]['name']+" is ready!"
self.users[u]['ready']=True
valid_packet=True
update_warlocks=True
# else if unready packet
elif package[0][0]=='unready':
print self.users[u]['name']+" is not ready!"
self.users[u]['ready']=False
valid_packet=True
update_warlocks=True
if update_warlocks:
data = {}
data[0]='warlocks'
data[1]=len(self.users)
self.server.broadcastData(data)
# break out of for loop
break
#else:
# print str(self.users[u]['connection'])+" "+str(package[1])
#.........这里部分代码省略.........