[egret+pomelo]学习笔记(1)

[egret+pomelo]学习笔记(2)

[egret+pomelo]学习笔记(3)

pomelo

pomelo服务端介绍(game-server/config/server.json):

{
"development": {
"connector": [
{"id": "connector-server-1", "host": "127.0.0.1", "port": 3150, "clientPort": 3010, "frontend": true},
{"id": "connector-server-2", "host": "127.0.0.1", "port": 3151, "clientPort": 3011, "frontend": true}
],
"area": [
{"id": "area-server-1", "host": "127.0.0.1", "port": 3250, "areaId": 1}
],
"gate": [
{"id": "gate-server-1", "host": "127.0.0.1", "clientPort": 3014, "frontend": true}
]
}
}

可以看出,服务端是由以下几个部分构成:

* 2 个 `connector` 服务器,主要用于接受和发送消息。
* 1 个 `gate` 服务器,主要用于负载均衡,将来自客户端的连接分散到两个 `connector` 服务器上。
* 1 个 `area` 服务器,主要用于驱动游戏场景,和游戏逻辑

由服务端的关系图可以看出,访问pomelo,首先应该访问gate服务端,让服务端为其分配合适的connector以处理当前用户的请求。

egret

1.链接gate服务端,将玩家名称传递给gate服务端以作为注册

config.global.pomelo.request("gate.gateHandler.queryEntry", { uid: config.global.playerName }, this.onGateMsg);

2.当gate服务端链接返回成功后,链接gate服务端返回的地址,链接分配给自己的connector服务端

    private onGateMsg(gate_data) {
this.connectIp = gate_data.host;
this.connectPort = gate_data.port;
config.global.pomelo.disconnect();
trace("正在尝试链接connect服务端...");
config.global.pomelo.open(network.PomeloService.CONNECTION, this.connectIp, this.connectPort);
}

3.使用connector.entryHandler.entry在connector中注册,注册成功后,使用area.playerHandler.enterScene访问游戏场景信息

    private onConnectSucceed() {
trace("CONNECT服务端链接成功");
trace("开始注册服务端信息...");
config.global.pomelo.request('connector.entryHandler.entry', { name: config.global.playerName }, this.onEntryMsg);
} private onEntryMsg(entry_data) {
if (entry_data.code === 200) {
trace("注册信息成功");
trace("开始申请进入游戏...");
config.global.pomelo.request('area.playerHandler.enterScene', { name: config.global.playerName, playerId: entry_data.playerId }, (respose) => {
Global.dispatchEvent(events.PomeloServerEvents.MAPMSG, respose);
trace("进入游戏成功");
trace("开始解析地图信息");
});
} else {
trace("注册服务端信息出现问题,请检查提交信息");
}
}

4.游戏中会将地下散落的物品、玩家的位置以json格式返回 给客户端(返回的数据示例)

{
"code": 200,
"data": {
"area": {
"id": "1",
"entities": {
"1": {
"x": 2041,
"y": 256,
"entityId": 1,
"kindId": "3",
"kindName": "极限法剑",
"areaId": 1,
"type": "treasure",
"imgId": 301804,
"score": 20
},
"2": {
"x": 1354,
"y": 633,
"entityId": 2,
"kindId": 86,
"kindName": "威严腿铠",
"areaId": 1,
"type": "treasure",
"imgId": 344721,
"score": 80
},
"3": {
"x": 719,
"y": 460,
"entityId": 3,
"kindId": 103,
"kindName": "护身符",
"areaId": 1,
"type": "treasure",
"imgId": 980057,
"score": 1
},
"4": {
"x": 246,
"y": 1003,
"entityId": 4,
"kindId": 64,
"kindName": "玉树Belt",
"areaId": 1,
"type": "treasure",
"imgId": 371501,
"score": 25
},
"5": {
"x": 1343,
"y": 426,
"entityId": 5,
"kindId": 15,
"kindName": "紧身衣",
"areaId": 1,
"type": "treasure",
"imgId": 331203,
"score": 21
},
"6": {
"x": 417,
"y": 336,
"entityId": 6,
"kindId": 75,
"kindName": "幻彩玉带",
"areaId": 1,
"type": "treasure",
"imgId": 374224,
"score": 90
},
"7": {
"x": 1311,
"y": 262,
"entityId": 7,
"kindId": 73,
"kindName": "百窜云",
"areaId": 1,
"type": "treasure",
"imgId": 374222,
"score": 80
},
"8": {
"x": 1385,
"y": 870,
"entityId": 8,
"kindId": 57,
"kindName": "荧光坠子",
"areaId": 1,
"type": "treasure",
"imgId": 384921,
"score": 71
},
"9": {
"x": 432,
"y": 1044,
"entityId": 9,
"kindId": 111,
"kindName": "灵魂之石",
"areaId": 1,
"type": "treasure",
"imgId": 90102,
"score": 85
},
"10": {
"x": 910,
"y": 451,
"entityId": 10,
"kindId": 21,
"kindName": "锁子甲",
"areaId": 1,
"type": "treasure",
"imgId": 334303,
"score": 80
},
"11": {
"x": 982,
"y": 160,
"entityId": 11,
"kindId": 21,
"kindName": "锁子甲",
"areaId": 1,
"type": "treasure",
"imgId": 334303,
"score": 80
},
"12": {
"x": 746,
"y": 374,
"entityId": 12,
"kindId": 32,
"kindName": "流云",
"areaId": 1,
"type": "treasure",
"imgId": 394224,
"score": 64
},
"13": {
"x": 894,
"y": 827,
"entityId": 13,
"kindId": 32,
"kindName": "流云",
"areaId": 1,
"type": "treasure",
"imgId": 394224,
"score": 64
},
"14": {
"x": 1027,
"y": 724,
"entityId": 14,
"kindId": 73,
"kindName": "百窜云",
"areaId": 1,
"type": "treasure",
"imgId": 374222,
"score": 80
},
"15": {
"x": 699,
"y": 156,
"entityId": 15,
"kindId": 60,
"kindName": "Belt",
"areaId": 1,
"type": "treasure",
"imgId": 371101,
"score": 1
},
"16": {
"x": 881,
"y": 792,
"entityId": 16,
"kindId": 109,
"kindName": "圣母之镜",
"areaId": 1,
"type": "treasure",
"imgId": 90412,
"score": 61
},
"17": {
"x": 1442,
"y": 96,
"entityId": 17,
"kindId": 62,
"kindName": "银Belt",
"areaId": 1,
"type": "treasure",
"imgId": 371201,
"score": 13
},
"18": {
"x": 1754,
"y": 265,
"entityId": 18,
"kindId": 84,
"kindName": "独孤腿铠",
"areaId": 1,
"type": "treasure",
"imgId": 344701,
"score": 66
},
"19": {
"x": 1384,
"y": 279,
"entityId": 19,
"kindId": 13,
"kindName": "布衣",
"areaId": 1,
"type": "treasure",
"imgId": 331103,
"score": 1
},
"20": {
"x": 1277,
"y": 439,
"entityId": 20,
"kindId": 108,
"kindName": "钻石之星",
"areaId": 1,
"type": "treasure",
"imgId": 90472,
"score": 51
},
"21": {
"x": 53,
"y": 313,
"entityId": 21,
"kindId": 53,
"kindName": "骷髅吊坠",
"areaId": 1,
"type": "treasure",
"imgId": 381701,
"score": 31
},
"22": {
"x": 965,
"y": 367,
"entityId": 22,
"kindId": 73,
"kindName": "百窜云",
"areaId": 1,
"type": "treasure",
"imgId": 374222,
"score": 80
},
"23": {
"x": 146,
"y": 94,
"entityId": 23,
"kindId": 69,
"kindName": "攫魂铃",
"areaId": 1,
"type": "treasure",
"imgId": 371803,
"score": 55
},
"24": {
"x": 374,
"y": 400,
"entityId": 24,
"kindId": 100,
"kindName": "荒芜之鞋",
"areaId": 1,
"type": "treasure",
"imgId": 364724,
"score": 70
},
"25": {
"x": 1668,
"y": 807,
"entityId": 25,
"kindId": 105,
"kindName": "先祖护符",
"areaId": 1,
"type": "treasure",
"imgId": 980055,
"score": 21
},
"26": {
"x": 875,
"y": 384,
"entityId": 26,
"kindId": 39,
"kindName": "布帽",
"areaId": 1,
"type": "treasure",
"imgId": 321101,
"score": 17
},
"27": {
"x": 1799,
"y": 146,
"entityId": 27,
"kindId": 105,
"kindName": "先祖护符",
"areaId": 1,
"type": "treasure",
"imgId": 980055,
"score": 21
},
"28": {
"x": 1543,
"y": 384,
"entityId": 28,
"kindId": 31,
"kindName": "风云雷电",
"areaId": 1,
"type": "treasure",
"imgId": 394223,
"score": 56
},
"29": {
"x": 668,
"y": 1083,
"entityId": 29,
"kindId": 73,
"kindName": "百窜云",
"areaId": 1,
"type": "treasure",
"imgId": 374222,
"score": 80
},
"30": {
"x": 1884,
"y": 420,
"entityId": 30,
"kindId": 61,
"kindName": "缎带",
"areaId": 1,
"type": "treasure",
"imgId": 371103,
"score": 7
},
"31": {
"x": 2022,
"y": 715,
"entityId": 31,
"kindId": 48,
"kindName": "媚狐头饰",
"areaId": 1,
"type": "treasure",
"imgId": 324701,
"score": 85
},
"32": {
"x": 1568,
"y": 1138,
"entityId": 32,
"kindId": "9",
"kindName": "丰城剑",
"areaId": 1,
"type": "treasure",
"imgId": 304504,
"score": 68
},
"33": {
"x": 1219,
"y": 747,
"entityId": 33,
"kindId": 63,
"kindName": "水晶Belt",
"areaId": 1,
"type": "treasure",
"imgId": 371401,
"score": 19
},
"34": {
"x": 1696,
"y": 1146,
"entityId": 34,
"kindId": 16,
"kindName": "鳞甲",
"areaId": 1,
"type": "treasure",
"imgId": 331303,
"score": 31
},
"35": {
"x": 881,
"y": 563,
"entityId": 35,
"kindId": 54,
"kindName": "苍魂珠",
"areaId": 1,
"type": "treasure",
"imgId": 384222,
"score": 41
},
"36": {
"x": 1983,
"y": 456,
"entityId": 36,
"kindId": 101,
"kindName": "玉衡之履",
"areaId": 1,
"type": "treasure",
"imgId": 364803,
"score": 85
},
"37": {
"x": 268,
"y": 164,
"entityId": 37,
"kindId": 88,
"kindName": "斩魂腿甲",
"areaId": 1,
"type": "treasure",
"imgId": 344922,
"score": 90
},
"38": {
"x": 1373,
"y": 1047,
"entityId": 38,
"kindId": "8",
"kindName": "寒雪枪",
"areaId": 1,
"type": "treasure",
"imgId": 304501,
"score": 60
},
"39": {
"x": 964,
"y": 790,
"entityId": 39,
"kindId": 26,
"kindName": "折扇",
"areaId": 1,
"type": "treasure",
"imgId": 391201,
"score": 18
},
"40": {
"x": 1257,
"y": 1099,
"entityId": 40,
"kindId": 66,
"kindName": "白面狼牙",
"areaId": 1,
"type": "treasure",
"imgId": 371601,
"score": 37
},
"54": {
"x": 436,
"y": 208,
"entityId": 54,
"kindId": 210,
"areaId": 1,
"id": 214,
"type": "player",
"name": "ss",
"walkSpeed": 240,
"score": 0
}
},
"width": 2200,
"height": 1201
},
"playerId": 214
}
}

5.客户端根据返回的json,显示装备和玩家位置,并监听服务端中的移动以及状态更改事件,来做出相应的操作。

下面来看一下,pomelo服务端中是怎么处理前端的请求的。

在treasures的项目文档docs/tutorial.md 中给出的示例如下图

在(1)中,客户端通过pomelo.request方法,将gate.gateHandler.queryEntry 以及参数 uid 传递给gate服务端,下一步,我们进入服务端看一下(2)路径中的gateHandler文件

该方法为当前用户获取一个connector服务端,将用户uid参数与获取到的connector服务端绑定,并将分配后的connector服务端的获取状态code,链接host,端口port,返回给客户端进行处理,客户端在通过gate服务端接收到返回的参数后,就可以将gate服务端断开,然后链接分配给自己的connector服务端进行链接,通过 connector.entryHandler.entry,connector服务端会在用户第一次链接的时候初始化session信息,这样每次的服务端请求的时候,都不用再次 传递多余的数据。

entry方法成功后会向用户返回一个状态code 和玩家唯一标识playerId,客户端记住玩家唯一标识,以区分同场景中多个玩家时,哪一个玩家为自己的玩家。

这样一条用户请求和服务端返回的流程就走完了。

[egret+pomelo]实时游戏杂记(2)的更多相关文章

  1. [egret+pomelo]实时游戏杂记(3)

    [egret+pomelo]学习笔记(1) [egret+pomelo]学习笔记(2) [egret+pomelo]学习笔记(3) 服务端的请求流程走完了一遍,下面就该看一下,在目前的服务端中,各服务 ...

  2. [egret+pomelo]实时游戏杂记(1)

    [egret+pomelo]学习笔记(1) [egret+pomelo]学习笔记(2) [egret+pomelo]学习笔记(3) 资料 egret pomelo pomelo捡宝项目 准备工作 1. ...

  3. [egret+pomelo]实时游戏杂记(4)

    了解了前后端的通信,下面就可以开始自己的业务逻辑了,首先玩家输入名称,选择角色后进入游戏世界. 服务端的demo中已经提供了一些简单的角色信息和属性,数据地址位于 game-server/config ...

  4. [egret+pomelo]实时对战游戏杂记(5)

    之前大体了解了pomelo服务端的运行的大体运行流程,下面详细的学习一下在服务端比较重要的一个容器模块bearcat,在bearcat的wiki中我们可以对其有个大概的了解,在服务端示例的代码中也大量 ...

  5. Egret 之 消除游戏 开发 PART 6 Egret elimination game development PART 6

    Egret 之 消除游戏 开发 PART 6 Egret elimination game development PART 6 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱: ...

  6. Pomelo分布式游戏服务器框架

    Pomelo介绍&入门 目录 前言&介绍 安装Pomelo 创建项目并启动 创建项目 项目结构说明 启动 测试连接 聊天服务器 新建gate和chat服务器 配置master.json ...

  7. 为什么MOBA和吃鸡类游戏不推荐用tcp协议 延迟不利于实时游戏

    http://news.gamedog.cn/a/20171221/2287418.html 我们知道,不同类型的游戏因为玩法.竞技程度不一样,采用的同步算法不一样,对网络延迟的要求也不一样.例如,M ...

  8. 微信小游戏 查看egret的小游戏支持库版本

    在开发者工具 console输入egret.wxgame

  9. egret之消除游戏开发

    1.地图 (1)地图形状不同,尺寸不变 (2)背景图变化 2.步数 (1)不同关卡步数不同 (2)步数为01,游戏失败 3.道具 4.消除 (1)>=3可消除 (2)不可消除时,自动打乱 5.数 ...

随机推荐

  1. php猴子吃桃

    <?php header("content-type:text/html;charset=utf-8"); /* 有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个! 以后 ...

  2. Action window Flags

    Action window 主要字段使用 含义     target 值 作用 current 当前窗口 new 新窗口 inline 内联编辑 fullscreen 全屏 main 当前窗口的主动作 ...

  3. 微信开源组件WCDB漫谈及Demo

    代码地址如下:http://www.demodashi.com/demo/12422.html 前言 移动端的数据库选型一直是一个难题,直到前段时间看到了WeMobileDev(微信前端团队)放出了第 ...

  4. mysql报错1872: Slave failed to initialize relay log info structure from the repository

    ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository 在一台主机上增加 ...

  5. android:scrollbar的一些属性

    1. activity_maim.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  6. MySQL系列:innodb源代码分析之线程并发同步机制

    innodb是一个多线程并发的存储引擎,内部的读写都是用多线程来实现的,所以innodb内部实现了一个比較高效的并发同步机制. innodb并没有直接使用系统提供的锁(latch)同步结构,而是对其进 ...

  7. 【PyCharm编辑器】之报:Spellchecker inspection helps locate typos and misspelling in your code, comments and literals, and fix them in one click.问题

    如上图,输入一个单词时会出现波浪线,报:Spellchecker inspection helps locate typos and misspelling in your code, comment ...

  8. JavaScript -- JavaScript DOM 编程艺术(第2版)

    /* 渐进增强 平稳退化 网页 结构层(structural layer): HTML 表示层(presentation layer): CSS <link rel="styleshe ...

  9. [转]Unity3D Editor 编辑器简易教程

    Star 自定义编辑器简易教程 an introduction to custom editors 原文地址 http://catlikecoding.com/unity/tutorials/star ...

  10. springcloud和kubernetes对比

    由于这两个都不熟,所以在考虑学哪个. 先说结论:都要学,但是重点学k8s,k8s是一个更加完善的解决方案,springcloud被淘汰只是时间的问题. 从自己的经历和网上的文章两方面分析 个人经历: ...