${data.username} 加入聊天室
` 194 | // $('.chat-con').append(html); 195 | this.playground.domCube.chat.setGuestNumber(data.userCount); 196 | this.playground.domCube.chat.refreshGuestList(data?.users); 197 | }) 198 | 199 | //離開成功 200 | this.socket.on('leaveSuccess', () => { 201 | this.checkOut() 202 | }) 203 | 204 | //退出提示 205 | this.socket.on('leave', (data) => { 206 | if (data.username != null) { 207 | this.playground.domCube.chat.refreshGuestList(data?.users) 208 | let html = `${data.username} 退出聊天室
`; 209 | // $('.chat-con').append(html); 210 | this.playground.domCube.chat.setGuestNumber(data.userCount) 211 | } 212 | }) 213 | 214 | //收到訊息 215 | this.socket.on('receiveMessage', (data) => { 216 | 217 | this.showMessage(data) 218 | }) 219 | } 220 | private checkIn(data: any) { 221 | const loginWrapper = this.chatBlock.querySelector('#login'); 222 | const userNameEle = this.chatBlock.querySelector('#my-name'); 223 | userNameEle.innerHTML = data.username; 224 | loginWrapper.classList.add('login--logined'); 225 | this.setRotationLockFromUI(true); 226 | this.setLoginStatus(true); 227 | this.playground.showChat(); 228 | } 229 | 230 | private checkOut() { 231 | const loginWrapper = this.chatBlock.querySelector('#login'); 232 | loginWrapper.classList.remove('login--logined'); 233 | this.playground.domCube.chat.showScreen('loginGuide'); 234 | this.chatBlock.querySelector('#chat-main').innerHTML = ''; 235 | this.setLoginStatus(false); 236 | } 237 | 238 | private sendMessage() { 239 | const inputEle = this.chatBlock.querySelector('#message-input'); 240 | const message = (inputEle as HTMLInputElement).value; 241 | (inputEle as HTMLInputElement).value = '' 242 | if (message) { 243 | /*觸發 sendMessage 事件*/ 244 | this.socket.emit('sendMessage', { username: this.myName, message: message }); 245 | } 246 | } 247 | 248 | private showMessage(data: any) { 249 | let html; 250 | if (data.username === this.myName) { 251 | html = `