源地址:http://bbs.9miao.com/thread-45790-1-2.html
本例演示的是模拟游戏服务端,让角色在场景1中跳转到场景2中。在实际游戏中,client将要跳转的角色id和目标场景id发给net,net转发给gate,gate将目标场景id与角色当前所在场景id进行对比,一样则返回跳转失败,不一样则在目标场景服务器重新实例化一个角色,然后将之前的场景服务器中的角色实例删除。因为此次只是掩饰原理,所以实际中的各种验证等就不进行处理了,有问题可以在论坛发帖探讨。
1.创建工程
命令行下输入firefly-admin.py createproject communication(linux在终端输入)
<ignore_js_op>
firefly会创建一个名为communication的工程
<ignore_js_op>
2.导入工程
将工程导入到eclipse中
<ignore_js_op>
3.配置参数
配置config.json中的相应参数
- {
- "master":{"rootport":8999,"webport":9998},
- "servers":{
- "gate":{"rootport":10000,"name":"gate","app":"app.gate.gateserver"},
- "net":{"netport":11009,"name":"net","remoteport":[{"rootport":10000,"rootname":"gate"}],"app":"app.net.netserver"},
- "scene1":{"remoteport":[{"rootport":10000,"rootname":"gate"}],"name":"scene1","app":"app.scene.sceneserver"},
- "scene2":{"remoteport":[{"rootport":10000,"rootname":"gate"}],"name":"scene2","app":"app.scene.sceneserver"}
- },
- "db":{
- "host":"localhost",
- "user":"root",
- "passwd":"111",
- "port":3306,
- "db":"anheisg",
- "charset":"utf8"
- },
- "memcached":{
- "urls":["127.0.0.1:11211"],
- "hostname":"test"
- }
- }
复制代码
具体参数含义和配置说明详见:http://bbs.9miao.com/forum.php?m ... 6orderby%3Ddateline
4.编写代码
在上面填写的server配置的app对应路径下分别建立netserver.py、gateserver.py和sceneserver.py,这里只列出了几个重要文件,其他如User.py、UserManager.pu等详见附件。
1)netserver.py:
- #coding:utf8
- from firefly.server.globalobject import netserviceHandle,GlobalObject
- @netserviceHandle
- def login_1004(_conn,data):
- data = eval(data)#客户端传来的number是str型,转换成dict型
- pid = data.get("pid")#获取pid
- sid = data.get("sid")#获取sid
- result = GlobalObject().remote['gate'].callRemote("enterScene_1003",pid,sid)#调用root服务器(gate)的enterScene_1003方法,参数为pid,sid
- return result#返回结果
复制代码
2)gateserver.py:
- #coding:utf8
- from User import User
- from UserManager import UserManager
- from firefly.server.globalobject import GlobalObject,rootserviceHandle
- @rootserviceHandle
- def enterScene_1003(pid,sid):
- '''进入场景
- @param pid: int 角色id
- @param sid: str 要跳转的场景id
- '''
- if not UserManager().isHaveUser(pid):#如果用户管理器中没有该用户
- user = User(pid,sid)#实例化角色
- UserManager().addUser(user)#添加到用户管理器中
- result = GlobalObject().root.callChildByName(sid,"enterScene_1001",pid,sid)#调用node服务器(sid)的enterScene_1001方法,参数为pid,sid
- return result#返回结果
- else:#如果用户管理器中有该用户
- user = UserManager().getUser(pid)#获取该用户实例
- nowSid = user.sid#用户当前所在场景id
- if nowSid == sid:#用户当前所在场景id与要跳转的场景id相同
- return "Failure"#返回失败信息
- else:#否则
- result1 = GlobalObject().root.callChildByName(sid,"enterScene_1001",pid,sid)#调用node服务器(sid)的enterScene_1001方法,参数为pid,sid
- GlobalObject().root.callChildByName(nowSid,"deletePlayer_1002",pid)#调用node服务器(nowSid)的deletePlayer_1002方法,参数为pid
- UserManager().updateSid(pid, sid)#更新用户前所在场景id
- return result1#返回结果
复制代码
3)sceneserver.py
- #coding:utf8
- from firefly.server.globalobject import remoteserviceHandle
- from Player import Player
- from PlayersManager import PlayersManager
- @remoteserviceHandle("gate")
- def enterScene_1001(pid,sid):
- '''进入场景'''
- player = Player(pid,sid)#实例化角色实例
- PlayersManager().addPlayer(player)#将角色添加到角色管理器中
- return "I'm now at %s" % sid#返回跳转成功信息
- @remoteserviceHandle("gate")
- def deletePlayer_1002(pid):
- '''删除角色实例
- '''
- if PlayersManager().isHavePlayer(pid):#如果角色管理器中有该角色
- user = PlayersManager().getPlayer(pid)#获取该角色实例
- sid = user.sid#获取角色所在场景实例
- PlayersManager().dropPlayer(pid)#从角色管理器中删除该角色
- print 'I left %s' % sid
复制代码
4)最后编写客户端——client.py:
- #coding:utf8
- from socket import AF_INET,SOCK_STREAM,socket
- import struct
- import time
- def sendData(sendstr,commandId):
- '''定义协议头
- '''
- HEAD_0 = chr(0)
- HEAD_1 = chr(0)
- HEAD_2 = chr(0)
- HEAD_3 = chr(0)
- ProtoVersion = chr(0)
- ServerVersion = 0
- sendstr = sendstr
- data = struct.pack('!sssss3I',HEAD_0,HEAD_1,HEAD_2,\
- HEAD_3,ProtoVersion,ServerVersion,\
- len(sendstr)+4,commandId)
- senddata = data+sendstr
- return senddata
- def resolveRecvdata(data):
- '''解析数据,根据定义的协议头解析服务器返回的数据
- '''
- head = struct.unpack('!sssss3I',data[:17])
- lenght = head[6]
- message = data[17:17+lenght]
- return message
- if __name__ == "__main__":
- HOST='localhost'
- PORT=11009
- ADDR=(HOST , PORT)
- client = socket(AF_INET,SOCK_STREAM)
- client.connect(ADDR)#建立socket连接
- client.sendall(sendData("{'pid':100001,'sid':'scene1'}",1004))#向服务器发送消息
- message = client.recv(1024)#接收服务器返回的消息
- message = resolveRecvdata(message)#解析消息
- print message
- time.sleep(3)
- client.sendall(sendData("{'pid':100001,'sid':'scene2'}",1004))#向服务器发送消息
- message = client.recv(1024)#接收服务器返回的消息
- message = resolveRecvdata(message)#解析消息
- print message
- time.sleep(3)
- client.sendall(sendData("{'pid':100001,'sid':'scene2'}",1004))#向服务器发送消息
- message = client.recv(1024)#接收服务器返回的消息
- message = resolveRecvdata(message)#解析消息
- print message
- client.close()
复制代码
5.运行程序
1)运行服务器,打开附件下载的communication文件夹,打开命令行窗口,输入python startmaster.py,回车,如图:
<ignore_js_op>
2)运行客户端,第一次登陆时,角色进入了场景1,成功;第二次由场景1台转到场景2,成功;第三次由场景2跳转到场景2,失败。
<ignore_js_op>