| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { _decorator, Component, assetManager, game } from "cc"
- import { GameCtr } from "../gameCtr"
- import { processCtr } from "./processCtr"
- const { ccclass, property } = _decorator
- @ccclass("SocketClient")
- export class SocketClient extends Component {
- @property({ type: processCtr })
- public processCtr: processCtr | null = null
- private roomId: number = 1
- public gameId: number = 8
- private SERVER_URL: String = `wss://${location.hostname === 'localhost' ? 'airpg1.izouma.com' :location.hostname}`
- public socket:any ;
- // 你可以定义其他属性或者私有变量来存储游戏状态或与UI交互的节点
- onLoad() {
- // 加载 socket.io 脚本
- // this.scheduleOnce(() => { this.initSocketConnection() }, 10)
- }
- public initSocketConnection() {
- this.socket = io(this.SERVER_URL)
- this.socket.on("connect", () => {
- console.log("Connected to the server.")
- })
- this.socket.on(`${this.gameId}`, (data) => {
- console.log('接收到消息:'+data.type)
- if (typeof data === "string") {
- try {
- data = JSON.parse(data)
- } catch (e) {
- console.error("Failed to parse message:", data)
- return
- }
- }
- if (data && data.type) {
- console.log('加入处理队列:'+data.type)
- this.processCtr.messageQueue.push(data) // Push the message to the queue
- this.processCtr.processNextMessage() // Attempt to process the next message
- }
- })
- }
- }
- // private SERVER_URL:string = "wss://airpg.izouma.com/";
- // // 指令处理函数映射
- // private commandHandlers: { [key: string]: (data: any) => void } = {
- // 'command1': this.handleCommand1,
- // 'command2': this.handleCommand2,
- // // ... 其他指令处理函数
- // };
- // onLoad() {
- // this._socket = new WebSocket(this.SERVER_URL);
- // this._socket.onmessage = (event) => {
- // const data = JSON.parse(event.data);
- // const handler = this.commandHandlers[data.command];
- // if (handler) {
- // handler.call(this, data);
- // }
- // };
- // }
- // handleCommand1(data: any) {
- // // 处理指令1的逻辑
- // }
- // handleCommand2(data: any) {
- // // 处理指令2的逻辑
- // }
- // // ... 其他指令处理函数
- // }
|