1.游戏配置

和其它棋牌游戏一样,德州扑克的前后端通讯,我们用的是http(s)+websocket协议,数据格式为JSON.数据传输用md5加密。 各种玩法的不一样,如房卡,匹配模式等,区别只在于开始游戏前初始化的一些数据会不一样。在前面的接口拿到游戏的配置数据之后,就开始进行websocket通讯,开始游戏.(所有通信字段前后端约定驼峰写法)配置数据包括但不限于以下数据:

1
2
3
4
5
6
7
8
9
10
11
12
{userId:"13123",//用户id
liveId:"123123"//主播id
rommId:"324234",//直播间
configId:"3113",
config:{......},
gameId:"1313",
gameServerUrl:"ws://test.xxxx.com",
tableId:"32234",//游戏桌
password:'23123',
token:'123123'
code:'0'
}

2.连接websocket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

module WebSocketManager {
export var sock: egret.WebSocket;
export var data: any;
export var eventType: string;


//连接服务器
export function connectServer(host: string = "", port?: number) {
if (sock) {
sock.close();
sock.removeEventListener(egret.ProgressEvent.SOCKET_DATA, onReceiveMessage, this);
sock.addEventListener(egret.Event.CONNECT, onSocketOpen, this);
}
sock = new egret.WebSocket();
sock.type = egret.WebSocket.TYPE_STRING;
if (port) {
sock.connect(host, port);
} else {
sock.connectByUrl(host);
}

sock.addEventListener(egret.ProgressEvent.SOCKET_DATA, onReceiveMessage, this);
sock.addEventListener(egret.Event.CONNECT, onSocketOpen, this);
sock.addEventListener(egret.Event.CLOSE, onSocketClose, this);
sock.addEventListener(egret.IOErrorEvent.IO_ERROR, onError, this);

}

export function onReceiveMessage(e: egret.Event) {
console.log('onSocket_Data', e.data);
let res = JSON.parse(sock.readUTF());
console.log('res', res);
this.eventType = res.eventType;
this.data = res.data;
console.log("Websocket收到数据", this.eventType, this.data, res.code);
this.data['code'] = res.code;
if (this.eventType == RoomProtocal.ON_RECONNECT) {
this.fromReconn = true;
}
sock.dispatchEventWith(this.eventType, true, this.data);
}
let fromReconn: boolean = false;
export function onSocketOpen() {
console.log("Websocket连接成功");
Global.hideWaritPanel();
egret.log('onSocketOpen');
sendPong();
if (this.fromReconn) {
this.fromReconn = false;
game.AppFacade.getInstance().sendNotification(SceneNotify.BEGIN);
} else {
game.AppFacade.getInstance().sendNotification(PanelNotify.OPEN_GAMECONTENT);
game.AppFacade.getInstance().sendNotification(SysNotify.CONNECT_SERVER_SUCCESS);
}
}

export function onSocketClose() {
console.log("Websocket连接关闭");
egret.clearInterval(intervalId);
game.AppFacade.getInstance().sendNotification(SysNotify.CONNECT_SERVER_DISCONNECT);
}

export function onError(event: egret.IOErrorEvent) {
console.log("Websocket传输错误:%s", JSON.stringify(event));
}
var intervalId;
//发送心跳包
function sendPong() {
let data = { eventType: RoomProtocal.ON_PONG, data: {} };
intervalId = egret.setInterval(() => {
sock.writeUTF(JSON.stringify(data));
}, this, 1500);
}

export function clearSock(): void {
if (sock) {
sock.close();
sock.removeEventListener(egret.ProgressEvent.SOCKET_DATA, onReceiveMessage, this);
sock.removeEventListener(egret.Event.CONNECT, onSocketOpen, this);
sock.removeEventListener(egret.Event.CLOSE, onSocketClose, this);
sock.removeEventListener(egret.IOErrorEvent.IO_ERROR, onError, this);
}
}


}

发送的数据格式约定为{ eventType: RoomProtocal.ON_PONG, data: {},code:003 },前面键值对为消息名称,后面的为数据对象。

如果是重连eventType的值为:ON_RECONNECT;code为错误码,包括但不限于如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static Done = 0;//ok
public static Network_Error = -1;//网络错误
public static Code_Error = -2;//
public static Data_Error = -3;//
public static Params_Error = -4;//
public static Table_Socket_Error = -10;//
public static Server_Close = -11;//
public static Game_Config_Not_Found = -101;//

public static User_Token_Error = -201;//
public static User_Not_Found = - 202;//
public static User_Info_Error = - 203;//
public static User_Money_Is_Zero = - 204;//

public static Table_Not_Found = - 300;//
public static Table_Is_Full = - 301;//
public static Table_Money_Not_Enough = - 302;//
public static Table_Info_Error = - 303;//
public static Table_In_Gaming = - 304;//
public static USER_In_Other_ROOM = - 310;//
public static Table_In_Other = - 311;//

public static Table_Pswd_Error = - 305;//
public static Table_CLOSED = - 307;//
public static Table_Chaoshi = -308;
public static Table_GuanliJiesan = -312;

双方通信的消息前后端可以约定(ON开头是客户端收到的,REQ开头的是服务端收到的),包括但不限于以下列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static ON_GAME_START = 'ON_GAME_START';//游戏开始
public static ON_ROUND_START = 'ON_ROUND_START';//回合开始
public static BEREPLACE = "BEREPLACE";//账号重复登陆
public static ON_GAME_OVER = 'ON_GAME_OVER';//游戏结束
public static ON_EXIT = 'ON_EXIT';//退出房间
public static ON_TABLE_CLOSED = 'ON_TABLE_CLOSED';//主播关闭游戏桌
public static ON_ENTER = 'ON_ENTER';//玩家进入房间
public static ON_PONG = "ON_PONG";//心跳包

public static ON_ADD_BET = "ON_ADD_BET"; //加注
public static ON_FOLLOW_BET = "ON_FOLLOW_BET"; //跟注
public static ON_GIVEUP_BET = "ON_GIVEUP_BET"; //让牌
public static ON_TALK = 'ON_TALK';
public static ON_STAND_UP = "REQ_STAND_UP";//请求站起
public static ON_SIT_DOWN = "REQ_SIT_DOWN";//请求坐下

public static REQ_ADD_BET = "REQ_ADD_BET"; //加注
public static REQ_FOLLOW_BET = "REQ_FOLLOW_BET"; //跟注
public static REQ_GIVEUP_BET = "REQ_GIVEUP_BET"; //让牌
public static REQ_TALK = 'REQ_TALK';//打字
public static REQ_STAND_UP = "REQ_STAND_UP";//请求站起
public static REQ_SIT_DOWN = "REQ_SIT_DOWN";//请求坐下
public static REQ_PONG = "ON_PONG";//心跳包
public static REQ_LOGIN = "REQ_LOGIN";//请求登陆