gameClient/assets/scripts/login/loginPanel.ts

89 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-06-29 15:30:43 +08:00
import { _decorator, Button, Component, EditBox, Label, Node } from 'cc'
2025-07-01 22:52:45 +08:00
import { WsClient } from '../network/wsclient'
2025-07-05 16:16:50 +08:00
import { WsConfig } from '../network/wsconfig'
import { PbHelper } from '../network/pbHelper'
import pb from '../pb/login.js'
import pb1 from '../pb/client.js'
import pb2 from '../pb/msgId.js'
import pb3 from '../pb/service.js'
// import * as pb from '../pb/login.d'
2025-06-29 15:30:43 +08:00
const { ccclass, property } = _decorator
@ccclass('loginPanel')
export class loginPanel extends Component {
@property(EditBox)
private usernameInput: EditBox = null // 用户名输入框
2025-07-05 16:16:50 +08:00
@property(EditBox)
private passwordInput: EditBox = null // 密码输入框
2025-06-29 15:30:43 +08:00
@property(Label)
private statusLable: Label = null // 状态提示标签
@property(Button)
private loginButton: Button = null // 登录按钮
// 组件初始化完成后调用
start() {
2025-07-01 22:52:45 +08:00
this.loginButton.node.on(Button.EventType.CLICK, this.onLoginButtonClick, this)
2025-06-29 15:30:43 +08:00
// 监听websocket消息事件
window.addEventListener('ws-message', this.onMessage.bind(this))
}
protected onDestroy(): void {
window.removeEventListener('ws-message', this.onMessage.bind(this))
}
// 登录按钮点击处理函数
2025-07-05 16:16:50 +08:00
private async onLoginButtonClick() {
2025-06-29 15:30:43 +08:00
const username = this.usernameInput.string.trim()
if (!username) {
this.statusLable.string = '请输入用户名'
return
}
2025-07-05 16:16:50 +08:00
const pwd = this.passwordInput.string.trim()
if (!pwd) {
this.statusLable.string = '请输入密码'
return
}
const ws = WsClient.Instance()
console.log(typeof ws)
if (!ws.IsConnected) {
await ws.ConnectAsync(WsConfig.Address[0].Address[0])
if (!ws.IsConnected) {
this.statusLable.string = '连接网络失败'
return
2025-06-29 15:30:43 +08:00
}
2025-07-05 16:16:50 +08:00
}
console.log('登陆帐号:', username, '密码:', pwd)
console.log('ReqUserLogin:', pb.ReqUserLogin)
const req = new pb.ReqUserLogin()
req.username = username
req.password = pwd
req.version = '20250601123030'
// const req = pb.ReqUserLogin.create({ username: username, password: pwd, ip: '', deviceId: '', version: '' })
const buffer = pb.ReqUserLogin.encode(req).finish()
const clinetMsg = new pb1.ClientMsg()
clinetMsg.msgId = pb2.MsgId.ReqUserLoginId
clinetMsg.data = buffer
clinetMsg.serviceTid = pb3.ServiceTypeId.STI_Login
const buffer1 = pb1.ClientMsg.encode(clinetMsg).finish()
ws.Send(buffer1)
2025-06-29 15:30:43 +08:00
}
private onMessage(event: CustomEvent) {
try {
const message = JSON.parse(event.detail) // 解析消息内容
if (message.type === 'loginSuccess') {
this.statusLable.string = '登录成功'
this.node.active = false
} else if (message.type === 'loginError') {
this.statusLable.string = '登录失败:' + message.reason
}
} catch (error) {
console.log('收到非json消息:', event.detail)
}
}
update(deltaTime: number) {}
}