81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
import { _decorator, Button, Component, EditBox, Label, Node } from 'cc'
|
|
import { WsClient } from '../network/wsclient'
|
|
import { WsConfig } from '../network/wsconfig'
|
|
import { PbHelper } from '../network/pbHelper'
|
|
import { ReqUserLogin, ServiceTypeId, MsgId, RspUserLogin } from '../network/pbExport'
|
|
import { EventType } from '../constant'
|
|
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消息事件
|
|
window.addEventListener(EventType.Ws, this.onMessage.bind(this))
|
|
}
|
|
|
|
protected onDestroy(): void {
|
|
window.removeEventListener(EventType.Ws, this.onMessage.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 (!ws.IsConnected) {
|
|
await ws.ConnectAsync(WsConfig.Address[1].Address[0])
|
|
if (!ws.IsConnected) {
|
|
this.statusLable.string = '连接网络失败'
|
|
return
|
|
}
|
|
}
|
|
|
|
console.log('登陆帐号:', username, '密码:', pwd)
|
|
|
|
const req = new ReqUserLogin()
|
|
req.username = username
|
|
req.password = pwd
|
|
req.version = '20250601123030'
|
|
const buffer = ReqUserLogin.encode(req).finish()
|
|
ws.Send(ServiceTypeId.STI_Login, MsgId.ReqUserLoginId, buffer)
|
|
}
|
|
|
|
private onMessage(event: CustomEvent) {
|
|
try {
|
|
const [msgid, buffer] = PbHelper.DecodeClient(event.detail)
|
|
switch (msgid) {
|
|
case MsgId.RspUserLoginId:
|
|
const rsp = PbHelper.Decode(RspUserLogin, buffer)
|
|
console.log('收到返回消息:', rsp)
|
|
default:
|
|
// console.log('收到非json消息:', event.detail)
|
|
}
|
|
} catch (error) {
|
|
// console.log('解析错误失败:', error)
|
|
console.log(PbHelper.Uint8ToHex(event.detail))
|
|
}
|
|
}
|
|
|
|
update(deltaTime: number) {}
|
|
}
|