A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(net): a mini-game socket whose dial is refused on a phone redials
esengine72e4e6eecd52
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 469b06251..966d481eb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,7 @@ published separately; it ships inside the editor.### Fixed+- On a WeChat phone, a socket whose connection is refused now reports that it closed, so a development build keeps redialing the editor (and a game's socket reconnects) instead of giving up after the first refusal.- A shipping package no longer carries the development debug channel's code (13 KB on a WeChat package): only a development export that names an editor imports it, as `esengine/debug-channel`.- Capturing a development build whose tab is in the background says so at once, instead of waiting 30 seconds for a frame the browser will not draw.- A web game opened in a background tab starts paused and reports itself hidden until it is shown; before, it counted as visible.diff --git a/sdk/src/net/MiniGameSocket.ts b/sdk/src/net/MiniGameSocket.tsindex 38f35a874..1d7699310 100644--- a/sdk/src/net/MiniGameSocket.ts+++ b/sdk/src/net/MiniGameSocket.ts@@ -69,14 +69,23 @@ export class MiniGameSocket implements PlatformSocket {this.events_.emit('message', res.data);});- task.onClose((res: { code: number; reason: string }) => {+ let ended = false;+ const end = (code: number, reason: string): void => {+ if (ended) return;+ ended = true;this.readyState = 'closed';- this.task_ = null;- this.events_.emit('close', res.code, res.reason);- });+ if (this.task_ === task) this.task_ = null;+ this.events_.emit('close', code, reason);+ };++ task.onClose((res: { code: number; reason: string }) => end(res.code, res.reason));task.onError((err: unknown) => {+ const failedToOpen = this.readyState === 'connecting';this.events_.emit('error', err);+ // A WeChat phone ends a refused dial with an error and no close, and a+ // caller that redials on close would wait for it forever.+ if (failedToOpen) end(1006, (err as { errMsg?: string } | null)?.errMsg ?? 'connect failed');});}diff --git a/sdk/tests/minigame-socket.test.ts b/sdk/tests/minigame-socket.test.tsnew file mode 100644index 000000000..f45bd8666--- /dev/null+++ b/sdk/tests/minigame-socket.test.ts@@ -0,0 +1,69 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file A mini-game socket whose dial is refused says it closed, once: on a+ * WeChat phone the host reports only the error, and a caller that+ * redials on close stopped trying after the first refusal.+ */+import { describe, it, expect } from 'vitest';+import { MiniGameSocket } from '../src/net/MiniGameSocket';+import type { MiniGameGlobal } from '../src/platform/minigame/api';++function host() {+ const tasks: Array<{ open(): void; error(e: unknown): void; close(code: number): void }> = [];+ const g = {+ connectSocket: () => {+ const h: Record<string, (x?: unknown) => void> = {};+ const task = {+ onOpen: (f: () => void) => { h.open = f; },+ onMessage: () => {},+ onClose: (f: (x: unknown) => void) => { h.close = f; },+ onError: (f: (x: unknown) => void) => { h.error = f; },+ send: () => {}, close: () => {},+ };+ tasks.push({ open: () => h.open(), error: (e) => h.error(e), close: (code) => h.close({ code, reason: '' }) });+ return task;+ },+ } as unknown as MiniGameGlobal;+ return { g, tasks };+}++describe('a mini-game socket', () => {+ it('closes when its dial is refused with an error alone, as a WeChat phone does', () => {+ const { g, tasks } = host();+ const s = new MiniGameSocket({ url: 'ws://192.168.31.243:37999/' }, g);+ const closes: number[] = [];+ s.on('close', (code) => closes.push(code));+ s.connect();+ tasks[0].error({ errCode: 1004, errMsg: 'open fail: _code:111,_msg:Connection refused' });+ expect(closes).toEqual([1006]);+ expect(s.readyState).toBe('closed');+ // Closed, so it can dial again.+ s.connect();+ expect(tasks).toHaveLength(2);+ });++ it('says so once when the host reports the error and then the close', () => {+ const { g, tasks } = host();+ const s = new MiniGameSocket({ url: 'ws://h/' }, g);+ const closes: number[] = [];+ s.on('close', (code) => closes.push(code));+ s.connect();+ tasks[0].error({ errMsg: 'refused' });+ tasks[0].close(1006);+ expect(closes).toEqual([1006]);+ });++ it('waits for the close after an error on an open socket', () => {+ const { g, tasks } = host();+ const s = new MiniGameSocket({ url: 'ws://h/' }, g);+ const closes: number[] = [];+ s.on('close', (code) => closes.push(code));+ s.connect();+ tasks[0].open();+ tasks[0].error({ errMsg: 'reset' });+ expect(closes).toEqual([]);+ tasks[0].close(1006);+ expect(closes).toEqual([1006]);+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 545382832..69ed25219 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -1110,4 +1110,6 @@ export const NOTED = {{ note: 'Connected development builds are named by the device they run on — `web (Chrome · Android)`, a phone\'s model on a mini-game — and numbered when two read alike, in the Profiler, Outliner, Frame Debugger and `device_targets`.' },'fix(debug): a shipping package no longer carries the debug channel':{ note: 'A shipping package no longer carries the development debug channel\'s code (13 KB on a WeChat package): only a development export that names an editor imports it, as `esengine/debug-channel`.' },+ 'fix(net): a mini-game socket whose dial is refused on a phone redials':+ { note: 'On a WeChat phone, a socket whose connection is refused now reports that it closed, so a development build keeps redialing the editor (and a game\'s socket reconnects) instead of giving up after the first refusal.' },};