()
| 123 | */ |
| 124 | private closeFn: unknown = null; // 全局loading关闭函数 |
| 125 | setupSocketStatusTip() { |
| 126 | const socket = this.socket; |
| 127 | |
| 128 | const showConnecting = () => { |
| 129 | if (this.closeFn) { |
| 130 | return; |
| 131 | } |
| 132 | this.closeFn = showGlobalLoading(t('正在重新链接')); |
| 133 | }; |
| 134 | |
| 135 | const closeConnecting = () => { |
| 136 | if (this.closeFn && typeof this.closeFn === 'function') { |
| 137 | this.closeFn(); |
| 138 | this.closeFn = null; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | // 网络状态管理 |
| 143 | socket.on('connect', () => { |
| 144 | console.log('连接成功'); |
| 145 | closeConnecting(); |
| 146 | |
| 147 | sharedEvent.emit('updateNetworkStatus', 'connected'); |
| 148 | }); |
| 149 | socket.on('connecting', (data) => { |
| 150 | console.log('正在连接'); |
| 151 | |
| 152 | showConnecting(); |
| 153 | |
| 154 | sharedEvent.emit('updateNetworkStatus', 'reconnecting'); |
| 155 | }); |
| 156 | socket.on('disconnect', (data) => { |
| 157 | console.log('与服务器的链接已断开'); |
| 158 | showErrorToasts(t('与服务器的链接已断开')); |
| 159 | closeConnecting(); |
| 160 | sharedEvent.emit('updateNetworkStatus', 'disconnected'); |
| 161 | }); |
| 162 | socket.on('connect_error', (data) => { |
| 163 | console.log('连接失败'); |
| 164 | showErrorToasts(t('连接失败')); |
| 165 | closeConnecting(); |
| 166 | sharedEvent.emit('updateNetworkStatus', 'disconnected'); |
| 167 | }); |
| 168 | |
| 169 | socket.io.on('reconnect', (data) => { |
| 170 | console.log('重连成功'); |
| 171 | |
| 172 | closeConnecting(); |
| 173 | sharedEvent.emit('updateNetworkStatus', 'connected'); |
| 174 | }); |
| 175 | socket.io.on('reconnect_attempt', (data) => { |
| 176 | console.log('重连中...'); |
| 177 | showConnecting(); |
| 178 | sharedEvent.emit('updateNetworkStatus', 'reconnecting'); |
| 179 | }); |
| 180 | socket.io.on('reconnect_error', (error) => { |
| 181 | console.error('重连尝试失败...', error); |
| 182 | showConnecting(); |
no test coverage detected