drgame-cc 1.0.52 → 1.0.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/drscript/Define.ts +91 -5
- package/drscript/KeyAdapter.ts +11 -0
- package/drscript/PlatformAdapter.ts +6 -6
- package/drscript/README.md +1 -7
- package/drscript/component/ExitWin.ts +0 -4
- package/drscript/virtualgamepad/VirtualGamepad.ts +638 -0
- package/drscript/virtualgamepad/VirtualGamepadView.ts +311 -280
- package/package.json +1 -1
- package/drscript/virtualgamepad/VirtualGamepadClient.ts +0 -214
- package/drscript/virtualgamepad/VirtualGamepadConfig.ts +0 -70
- package/drscript/virtualgamepad/VirtualGamepadManager.ts +0 -809
package/package.json
CHANGED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
export interface VirtualGamepadClientOptions {
|
|
2
|
-
wsUrl: string;
|
|
3
|
-
roomId: string;
|
|
4
|
-
deviceId: string;
|
|
5
|
-
game: string;
|
|
6
|
-
userId: string;
|
|
7
|
-
userType: "xiaomi" | "guest";
|
|
8
|
-
guestId?: string;
|
|
9
|
-
token?: string;
|
|
10
|
-
maxMembers: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type VirtualGamepadConnectionState = "idle" | "connecting" | "open" | "joined" | "closed" | "error";
|
|
14
|
-
|
|
15
|
-
export default class VirtualGamepadClient {
|
|
16
|
-
private socket: WebSocket | null = null;
|
|
17
|
-
private options: VirtualGamepadClientOptions | null = null;
|
|
18
|
-
private manualClose: boolean = false;
|
|
19
|
-
private reconnectCount: number = 0;
|
|
20
|
-
private heartbeatTimer: any = -1;
|
|
21
|
-
private reconnectTimer: any = -1;
|
|
22
|
-
private readonly maxReconnectCount: number = 3;
|
|
23
|
-
private readonly heartbeatMs: number = 10000;
|
|
24
|
-
|
|
25
|
-
public onStateChange: ((state: VirtualGamepadConnectionState, reason?: string) => void) | null = null;
|
|
26
|
-
public onMessage: ((message: any) => void) | null = null;
|
|
27
|
-
|
|
28
|
-
public connect(options: VirtualGamepadClientOptions) {
|
|
29
|
-
this.options = options;
|
|
30
|
-
this.manualClose = false;
|
|
31
|
-
this.clearReconnect();
|
|
32
|
-
this.openSocket();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public close() {
|
|
36
|
-
this.manualClose = true;
|
|
37
|
-
this.clearHeartbeat();
|
|
38
|
-
this.clearReconnect();
|
|
39
|
-
if (this.socket) {
|
|
40
|
-
this.socket.onopen = null;
|
|
41
|
-
this.socket.onmessage = null;
|
|
42
|
-
this.socket.onclose = null;
|
|
43
|
-
this.socket.onerror = null;
|
|
44
|
-
try {
|
|
45
|
-
this.socket.close();
|
|
46
|
-
} catch (e) {
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
this.socket = null;
|
|
50
|
-
this.emitState("closed");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public send(message: any): boolean {
|
|
54
|
-
if (!this.socket || this.socket.readyState != WebSocket.OPEN) {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
this.socket.send(JSON.stringify(message));
|
|
59
|
-
return true;
|
|
60
|
-
} catch (e) {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private openSocket() {
|
|
66
|
-
if (!this.options || !this.options.wsUrl) return;
|
|
67
|
-
this.clearHeartbeat();
|
|
68
|
-
this.emitState("connecting");
|
|
69
|
-
let options = this.options;
|
|
70
|
-
let query: { [key: string]: string } = {
|
|
71
|
-
roomId: options.roomId,
|
|
72
|
-
deviceId: options.deviceId,
|
|
73
|
-
role: "game",
|
|
74
|
-
userId: options.userId,
|
|
75
|
-
userType: options.userType,
|
|
76
|
-
maxMembers: String(options.maxMembers)
|
|
77
|
-
};
|
|
78
|
-
if (options.guestId) query.guestId = options.guestId;
|
|
79
|
-
if (options.token) query.token = options.token;
|
|
80
|
-
let url = this.appendParams(options.wsUrl, query);
|
|
81
|
-
try {
|
|
82
|
-
this.socket = new WebSocket(url);
|
|
83
|
-
this.socket.onopen = this.handleOpen.bind(this);
|
|
84
|
-
this.socket.onmessage = this.handleMessage.bind(this);
|
|
85
|
-
this.socket.onclose = this.handleClose.bind(this);
|
|
86
|
-
this.socket.onerror = this.handleError.bind(this);
|
|
87
|
-
} catch (e) {
|
|
88
|
-
this.handleError(e);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
private handleOpen() {
|
|
93
|
-
if (!this.options) return;
|
|
94
|
-
this.reconnectCount = 0;
|
|
95
|
-
this.emitState("open");
|
|
96
|
-
let joinMessage: any = {
|
|
97
|
-
type: "join",
|
|
98
|
-
roomId: this.options.roomId,
|
|
99
|
-
role: "game",
|
|
100
|
-
deviceId: this.options.deviceId,
|
|
101
|
-
userId: this.options.userId,
|
|
102
|
-
userType: this.options.userType,
|
|
103
|
-
maxMembers: this.options.maxMembers,
|
|
104
|
-
game: this.options.game
|
|
105
|
-
};
|
|
106
|
-
if (this.options.guestId) joinMessage.guestId = this.options.guestId;
|
|
107
|
-
if (this.options.token) joinMessage.token = this.options.token;
|
|
108
|
-
console.log("[VirtualGamepad][WSS][Send] join", {
|
|
109
|
-
type: joinMessage.type,
|
|
110
|
-
roomId: joinMessage.roomId,
|
|
111
|
-
role: joinMessage.role,
|
|
112
|
-
deviceId: joinMessage.deviceId,
|
|
113
|
-
userId: joinMessage.userId,
|
|
114
|
-
userType: joinMessage.userType,
|
|
115
|
-
guestId: joinMessage.guestId || "",
|
|
116
|
-
maxMembers: joinMessage.maxMembers,
|
|
117
|
-
game: joinMessage.game,
|
|
118
|
-
hasToken: !!joinMessage.token
|
|
119
|
-
});
|
|
120
|
-
this.send(joinMessage);
|
|
121
|
-
this.startHeartbeat();
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
private handleMessage(event: MessageEvent) {
|
|
125
|
-
let data = event && event.data;
|
|
126
|
-
let message: any = null;
|
|
127
|
-
try {
|
|
128
|
-
message = typeof data == "string" ? JSON.parse(data) : data;
|
|
129
|
-
} catch (e) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
if (!message) return;
|
|
133
|
-
if (message.type != "pong") {
|
|
134
|
-
console.log("[VirtualGamepad][WSS][Receive]", message);
|
|
135
|
-
}
|
|
136
|
-
if (message.type == "joined" || message.type == "welcome") {
|
|
137
|
-
this.emitState("joined");
|
|
138
|
-
}
|
|
139
|
-
if (this.onMessage) {
|
|
140
|
-
this.onMessage(message);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
private handleClose(event?: any) {
|
|
145
|
-
this.clearHeartbeat();
|
|
146
|
-
this.socket = null;
|
|
147
|
-
this.emitState("closed", event && event.reason);
|
|
148
|
-
if (!this.manualClose) {
|
|
149
|
-
this.scheduleReconnect();
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
private handleError(event?: any) {
|
|
154
|
-
this.emitState("error", event && event.message);
|
|
155
|
-
if (!this.manualClose && (!this.socket || this.socket.readyState == WebSocket.CLOSED)) {
|
|
156
|
-
this.scheduleReconnect();
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
private startHeartbeat() {
|
|
161
|
-
this.clearHeartbeat();
|
|
162
|
-
this.heartbeatTimer = setInterval(() => {
|
|
163
|
-
if (!this.options) return;
|
|
164
|
-
this.send({
|
|
165
|
-
type: "ping",
|
|
166
|
-
roomId: this.options.roomId,
|
|
167
|
-
deviceId: this.options.deviceId,
|
|
168
|
-
role: "game",
|
|
169
|
-
timestamp: Date.now(),
|
|
170
|
-
ts: Date.now()
|
|
171
|
-
});
|
|
172
|
-
}, this.heartbeatMs);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
private scheduleReconnect() {
|
|
176
|
-
if (this.reconnectCount >= this.maxReconnectCount) {
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
this.clearReconnect();
|
|
180
|
-
this.reconnectCount++;
|
|
181
|
-
this.reconnectTimer = setTimeout(() => {
|
|
182
|
-
this.openSocket();
|
|
183
|
-
}, 1200 * this.reconnectCount);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private appendParams(url: string, params: { [key: string]: string }): string {
|
|
187
|
-
let query = [];
|
|
188
|
-
for (let key in params) {
|
|
189
|
-
query.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key] || ""));
|
|
190
|
-
}
|
|
191
|
-
let separator = url.indexOf("?") >= 0 ? "&" : "?";
|
|
192
|
-
return url + separator + query.join("&");
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
private emitState(state: VirtualGamepadConnectionState, reason?: string) {
|
|
196
|
-
if (this.onStateChange) {
|
|
197
|
-
this.onStateChange(state, reason);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
private clearHeartbeat() {
|
|
202
|
-
if (this.heartbeatTimer != -1) {
|
|
203
|
-
clearInterval(this.heartbeatTimer);
|
|
204
|
-
this.heartbeatTimer = -1;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
private clearReconnect() {
|
|
209
|
-
if (this.reconnectTimer != -1) {
|
|
210
|
-
clearTimeout(this.reconnectTimer);
|
|
211
|
-
this.reconnectTimer = -1;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
export const VIRTUAL_GAMEPAD_GAME_ID = "rexueqiangshou";
|
|
2
|
-
declare const global: any;
|
|
3
|
-
export const VIRTUAL_GAMEPAD_PLAYER_ID = "player1";
|
|
4
|
-
export const VIRTUAL_GAMEPAD_DEVELOPMENT_WS_URL = "ws://10.40.92.42:3000";
|
|
5
|
-
export const VIRTUAL_GAMEPAD_PRODUCTION_WS_URL = "wss://hyp.huyingpai.com/huyingpai/lewo/ws";
|
|
6
|
-
export const VIRTUAL_GAMEPAD_H5_URL = "https://docp.daoran.tv/vconsole/index.html";
|
|
7
|
-
export const VIRTUAL_GAMEPAD_DEVELOPMENT_WXACODE_URL = "https://hyp.huyingpai.com/API_USER_CS/wxacode/unlimited";
|
|
8
|
-
export const VIRTUAL_GAMEPAD_PRODUCTION_WXACODE_URL = "https://hyp.huyingpai.com/API_USER/wxacode/unlimited";
|
|
9
|
-
export const VIRTUAL_GAMEPAD_WXACODE_PAGE = "pages/gamepad/gamepad";
|
|
10
|
-
export const VIRTUAL_GAMEPAD_WXACODE_PROJECT_CODE = "lewo";
|
|
11
|
-
export const VIRTUAL_GAMEPAD_MAX_MEMBERS = 2;
|
|
12
|
-
export const VIRTUAL_GAMEPAD_STOP_TIMEOUT_MS = 500;
|
|
13
|
-
export type VirtualGamepadEnvironment = "development" | "production";
|
|
14
|
-
export const VIRTUAL_GAMEPAD_DEFAULT_ENVIRONMENT: VirtualGamepadEnvironment = "production";
|
|
15
|
-
export type VirtualGamepadQrMode = "h5" | "json";
|
|
16
|
-
export const VIRTUAL_GAMEPAD_QR_MODE: VirtualGamepadQrMode = "h5";
|
|
17
|
-
|
|
18
|
-
export default class VirtualGamepadConfig {
|
|
19
|
-
public static getWsUrl(): string {
|
|
20
|
-
return this.getRuntimeValue("RXQS_GAMEPAD_WS_URL") ||
|
|
21
|
-
(this.getEnvironment() == "production" ? VIRTUAL_GAMEPAD_PRODUCTION_WS_URL : VIRTUAL_GAMEPAD_DEVELOPMENT_WS_URL);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public static getWxacodeUrl(): string {
|
|
25
|
-
return this.getRuntimeValue("RXQS_GAMEPAD_WXACODE_URL") ||
|
|
26
|
-
(this.getEnvironment() == "production" ? VIRTUAL_GAMEPAD_PRODUCTION_WXACODE_URL : VIRTUAL_GAMEPAD_DEVELOPMENT_WXACODE_URL);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public static getEnvironment(): VirtualGamepadEnvironment {
|
|
30
|
-
let environment = this.getRuntimeValue("RXQS_GAMEPAD_ENVIRONMENT");
|
|
31
|
-
if (environment == "development" || environment == "production") {
|
|
32
|
-
return environment;
|
|
33
|
-
}
|
|
34
|
-
if (environment == "test") {
|
|
35
|
-
return "development";
|
|
36
|
-
}
|
|
37
|
-
return VIRTUAL_GAMEPAD_DEFAULT_ENVIRONMENT;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public static getH5Url(): string {
|
|
41
|
-
return this.getRuntimeValue("RXQS_GAMEPAD_H5_URL") || VIRTUAL_GAMEPAD_H5_URL;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public static getRoomId(): string {
|
|
45
|
-
return this.getRuntimeValue("RXQS_GAMEPAD_ROOM_ID");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public static getToken(): string {
|
|
49
|
-
return this.getRuntimeValue("RXQS_GAMEPAD_TOKEN");
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public static getQrMode(): VirtualGamepadQrMode {
|
|
53
|
-
let mode = this.getRuntimeValue("RXQS_GAMEPAD_QR_MODE");
|
|
54
|
-
return mode == "json" ? "json" : VIRTUAL_GAMEPAD_QR_MODE;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private static getRuntimeValue(key: string): string {
|
|
58
|
-
let runtimeRoot: any = typeof window != "undefined" ? window : (typeof global != "undefined" ? global : null);
|
|
59
|
-
if (runtimeRoot && runtimeRoot[key]) {
|
|
60
|
-
return String(runtimeRoot[key]);
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
if (cc && cc.sys && cc.sys.localStorage) {
|
|
64
|
-
return cc.sys.localStorage.getItem(key) || "";
|
|
65
|
-
}
|
|
66
|
-
} catch (e) {
|
|
67
|
-
}
|
|
68
|
-
return "";
|
|
69
|
-
}
|
|
70
|
-
}
|