chore: adjust player tank's size
This commit is contained in:
@@ -51,9 +51,10 @@ class NetworkManager {
|
||||
/**
|
||||
* Connect to the WebSocket server.
|
||||
* @param {string} serverUrl - WebSocket server URL (e.g., 'wss://example.com/ws').
|
||||
* @param {number} [timeoutMs=10000] - Connect timeout in milliseconds.
|
||||
* @returns {Promise<boolean>} Whether connection succeeded.
|
||||
*/
|
||||
connect(serverUrl) {
|
||||
connect(serverUrl, timeoutMs = 10000) {
|
||||
return new Promise((resolve) => {
|
||||
if (this._connected || this._connecting) {
|
||||
resolve(this._connected);
|
||||
@@ -64,20 +65,53 @@ class NetworkManager {
|
||||
this._connecting = true;
|
||||
this._shouldReconnect = true;
|
||||
|
||||
// Guard: make sure resolve is called exactly once.
|
||||
let settled = false;
|
||||
const finish = (ok, reason) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
if (connectTimer) {
|
||||
clearTimeout(connectTimer);
|
||||
connectTimer = null;
|
||||
}
|
||||
if (!ok) {
|
||||
// Tear down broken socket so next connect() starts clean.
|
||||
this._connecting = false;
|
||||
this._shouldReconnect = false; // a first-time failure should NOT auto-reconnect
|
||||
if (this._ws) {
|
||||
try { this._ws.close({}); } catch (e) { /* ignore */ }
|
||||
this._ws = null;
|
||||
}
|
||||
console.warn('[NetworkManager] connect() failed:', reason || 'unknown');
|
||||
}
|
||||
resolve(ok);
|
||||
};
|
||||
|
||||
// Connection timeout guard (e.g. DNS/TLS hang on cellular).
|
||||
let connectTimer = setTimeout(() => {
|
||||
finish(false, `connect timeout after ${timeoutMs}ms, url=${serverUrl}`);
|
||||
}, timeoutMs);
|
||||
|
||||
try {
|
||||
this._ws = wx.connectSocket({
|
||||
url: serverUrl,
|
||||
header: { 'content-type': 'application/json' },
|
||||
// Surface wx.connectSocket API-level failures (invalid url / domain not whitelisted / etc.)
|
||||
success: (res) => { console.log('[NetworkManager] wx.connectSocket invoked:', res && res.errMsg); },
|
||||
fail: (err) => {
|
||||
console.error('[NetworkManager] wx.connectSocket API failed:', JSON.stringify(err));
|
||||
finish(false, `wx.connectSocket fail: ${err && err.errMsg}`);
|
||||
},
|
||||
});
|
||||
|
||||
this._ws.onOpen(() => {
|
||||
console.log('[NetworkManager] Connected to server');
|
||||
console.log('[NetworkManager] Connected to server:', serverUrl);
|
||||
this._connected = true;
|
||||
this._connecting = false;
|
||||
this._reconnectAttempts = 0;
|
||||
this._startHeartbeat();
|
||||
this._emit('connected');
|
||||
resolve(true);
|
||||
finish(true);
|
||||
});
|
||||
|
||||
this._ws.onMessage((res) => {
|
||||
@@ -85,28 +119,45 @@ class NetworkManager {
|
||||
});
|
||||
|
||||
this._ws.onError((err) => {
|
||||
console.error('[NetworkManager] WebSocket error:', err);
|
||||
this._connecting = false;
|
||||
// Log as much context as possible; wx error objects vary across platforms.
|
||||
console.error('[NetworkManager] WebSocket error:',
|
||||
(err && (err.errMsg || err.message)) || err,
|
||||
'url=', serverUrl);
|
||||
this._emit('error', err);
|
||||
resolve(false);
|
||||
// If the error arrives before we ever got onOpen, treat it as a connect failure.
|
||||
if (!this._connected) {
|
||||
finish(false, `onError before open: ${err && (err.errMsg || err.message)}`);
|
||||
} else {
|
||||
// Runtime error on an established connection — let onClose handle reconnection.
|
||||
this._connecting = false;
|
||||
}
|
||||
});
|
||||
|
||||
this._ws.onClose((res) => {
|
||||
console.log('[NetworkManager] Connection closed:', res.code, res.reason);
|
||||
const code = res && res.code;
|
||||
const reason = res && res.reason;
|
||||
console.log('[NetworkManager] Connection closed:', code, reason, 'url=', serverUrl);
|
||||
|
||||
const wasConnected = this._connected;
|
||||
this._connected = false;
|
||||
this._connecting = false;
|
||||
this._stopHeartbeat();
|
||||
this._emit('disconnected', { code: res.code, reason: res.reason });
|
||||
this._emit('disconnected', { code, reason });
|
||||
|
||||
// Auto-reconnect if needed
|
||||
// If onClose arrives before onOpen, this is a connect failure.
|
||||
if (!wasConnected) {
|
||||
finish(false, `onClose before open: code=${code} reason=${reason}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Auto-reconnect only for drops on an already-established connection.
|
||||
if (this._shouldReconnect && this._reconnectAttempts < this._maxReconnectAttempts) {
|
||||
this._attemptReconnect();
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('[NetworkManager] Failed to create WebSocket:', e);
|
||||
this._connecting = false;
|
||||
resolve(false);
|
||||
finish(false, `exception: ${e && e.message}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -145,10 +196,17 @@ class NetworkManager {
|
||||
return;
|
||||
}
|
||||
|
||||
// Always include the player's current nickname (if any) so the server
|
||||
// can propagate it to other clients. Falls back silently when the
|
||||
// profile is not yet available.
|
||||
const profile = (typeof GameGlobal !== 'undefined') ? GameGlobal.playerProfile : null;
|
||||
const nickname = (profile && profile.nickname) ? profile.nickname : '';
|
||||
|
||||
const message = JSON.stringify({
|
||||
type,
|
||||
data,
|
||||
playerId: this._playerId,
|
||||
nickname,
|
||||
roomId: this._roomId,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
@@ -514,6 +572,12 @@ class NetworkManager {
|
||||
return this._playerId;
|
||||
}
|
||||
|
||||
/** Player display nickname (may be empty until profile is fetched). */
|
||||
get nickname() {
|
||||
const profile = (typeof GameGlobal !== 'undefined') ? GameGlobal.playerProfile : null;
|
||||
return (profile && profile.nickname) ? profile.nickname : '';
|
||||
}
|
||||
|
||||
/** Current latency in ms. */
|
||||
get latency() {
|
||||
return this._latency;
|
||||
|
||||
Reference in New Issue
Block a user