gameClient/assets/scripts/login/loginPanel.ts

70 lines
2.3 KiB
TypeScript

import { _decorator, Button, Component, EditBox, Label, Node } from 'cc'
import { WsClient } from '../network/wsclient'
import { WsConfig } from '../network/wsconfig'
import * as pb from '../network/pbExport'
import { MessageDispatcher } from '../network/messageDispatcher'
const { ccclass, property } = _decorator
@ccclass('loginPanel')
export class loginPanel extends Component {
@property(EditBox)
private usernameInput: EditBox = null // 用户名输入框
@property(EditBox)
private passwordInput: EditBox = null // 密码输入框
@property(Label)
private statusLable: Label = null // 状态提示标签
@property(Button)
private loginButton: Button = null // 登录按钮
// 组件初始化完成后调用
start() {
this.loginButton.node.on(Button.EventType.CLICK, this.onLoginButtonClick, this)
// 监听websocket消息事件
MessageDispatcher.Instance.On(pb.MsgId.RspUserLoginId, this.onLogin.bind(this))
}
protected onDestroy(): void {
MessageDispatcher.Instance.Off(pb.MsgId.RspUserLoginId, this.onLogin.bind(this))
}
// 登录按钮点击处理函数
private async onLoginButtonClick() {
const username = this.usernameInput.string.trim()
if (!username) {
this.statusLable.string = '请输入用户名'
return
}
const pwd = this.passwordInput.string.trim()
if (!pwd) {
this.statusLable.string = '请输入密码'
return
}
const ws = WsClient.Instance
console.log(typeof ws)
if (!WsClient.Instance.IsConnected) {
await WsClient.Instance.ConnectAsync(WsConfig.Address[1].Address[0])
if (!WsClient.Instance.IsConnected) {
this.statusLable.string = '连接网络失败'
return
}
}
// console.log('登陆帐号:', username, '密码:', pwd)
this.reqLogin(username, pwd)
}
private reqLogin(username, password: string) {
const req = new pb.ReqUserLogin()
req.username = username
req.password = password
req.version = '20250601123030'
const buffer = pb.ReqUserLogin.encode(req).finish()
WsClient.Instance.Send(pb.ServiceTypeId.STI_Login, pb.MsgId.ReqUserLoginId, buffer)
}
private onLogin(rsp: typeof pb.RspUserLogin.prototype) {
console.log('收到返回消息:', rsp)
}
update(deltaTime: number) {}
}