// js/game.js 示例代码 const game = { ws: null, // WebSocket 对象 init: function() { // 1. 使用你在 config.js 里定义的函数获取地址 // 假设房间号是 'room1',玩家ID随机生成 const url = getWsUrl('room1', 'player_' + Math.floor(Math.random()*1000)); console.log("正在连接:", url); // 2. 建立连接 this.ws = new WebSocket(url); // 3. 监听连接成功 this.ws.onopen = () => { document.getElementById('info').innerText = "已连接到服务器!"; }; // 4. 监听服务器发来的消息(比如别人出牌了) this.ws.onmessage = (event) => { const data = JSON.parse(event.data); console.log("收到消息:", data); // 这里写具体的处理逻辑,比如刷新手牌 if(data.type === 'deal') { this.renderCards(data.cards); } }; }, // 点击“准备”按钮触发的动作 ready: function() { if(this.ws) { // 通过 WebSocket 发送数据给 Go 后端 this.ws.send(JSON.stringify({ action: 'ready' })); } }, // ... 其他函数如 callLandlord, playCards 等 }; // 页面加载完成后初始化 window.onload = function() { game.init(); };