当前位置: 首页 > wzjs >正文

做家教的正规网站微信推广广告在哪里做

做家教的正规网站,微信推广广告在哪里做,重庆观音桥旅游攻略,上海公司章程在哪里下载打印1. webSocket交互约定 上期我们完成了游戏房间的实体类以及前端界面,这期我们实现玩家对战功能,这里我们同样通过webSocket实现玩家和玩家之间的交互,连接建立成功时,约定服务器发送一些游戏的初始信息给客户端: {me…

1. webSocket交互约定

上期我们完成了游戏房间的实体类以及前端界面,这期我们实现玩家对战功能,这里我们同样通过webSocket实现玩家和玩家之间的交互,连接建立成功时,约定服务器发送一些游戏的初始信息给客户端:

{message: 'gameReady', //游戏消息类别,游戏就绪ok: true, errMsg: '',//错误信息roomId: '', //游戏房间iduserId1: 1, //当前客户端玩家userId2: 2, //对手玩家isBlack: true //当前玩家是否拿黑子(先手)
}

约定玩家落子的请求和响应:

落子请求:

{message: 'putChess', userId: ,row: , //落子坐标行col:  //落子坐标列
}

落子响应:

{message: 'putChess', userid: ,row: , //落子坐标行col:  //落子坐标列winnerId: //是否分出胜负,-1表示未分出胜负,否则表示获胜玩家id
}

2. 建立webSocket连接

2.1 客户端代码

我们在script.js文件中增加webSocket连接相关的代码


let webSocket = new WebSocket('ws://127.0.0.1:8080/game');
webSocket.onopen = function() {console.log("连接game成功");
}
webSocket.onclose = function() {console.log("连接关闭请重新登陆");location.href = "/login.html";
}
webSocket.onerror = function() {console.log("error");
}//页面关闭时释放webSocket
window.onbeforeunload = function() {webSocket.close();
}//处理服务器发送的消息
webSocket.onmessage = function(e) {//连接成功处理返回响应let resp = JSON.parse(e.data);if(resp.message != "gameReady" || !resp.ok) {alert("进入游戏房间失败: " + resp.errMsg);location.href = "/hall.html";return;}resp.roomId = e.roomId;resp.userId1 = e.userId1;resp.userId2 = e.userId2;resp.isBlack = e.isBlack;//初始化棋盘initGame();//提示先手玩家落子setScreenText(resp.isBlack);
}
2.2 服务器代码

 创建webSocket

package org.ting.j20250110_gobang.websocket;import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
public class GameWebSocket extends TextWebSocketHandler {@Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {}@Overrideprotected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {}@Overridepublic void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {}@Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {}
}

注册webSocket:

package org.ting.j20250110_gobang.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import org.ting.j20250110_gobang.websocket.GameWebSocket;
import org.ting.j20250110_gobang.websocket.MatchWebSocket;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {@AutowiredMatchWebSocket matchWebSocket;@AutowiredGameWebSocket gameWebSocket;@Overridepublic void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {registry.addHandler(matchWebSocket, "/findMatch") //注意路径和前端对应//添加拦截器获取到session,方便获取session中的用户信息.addInterceptors(new HttpSessionHandshakeInterceptor());registry.addHandler(gameWebSocket, "/game")//添加拦截器获取到session,方便获取session中的用户信息.addInterceptors(new HttpSessionHandshakeInterceptor());}
}

3. 创建数据交互实体类

package org.ting.j20250110_gobang.game;public class GameReadyResponse {private String message; //游戏消息类别,游戏就绪private boolean ok;private String errMsg;//错误信息private String roomId; //游戏房间idprivate int userId1; //当前客户端玩家的idprivate int userId2;//对手idprivate boolean isBlack; //当前玩家是否拿黑子(先手)public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public boolean isOk() {return ok;}public void setOk(boolean ok) {this.ok = ok;}public String getErrMsg() {return errMsg;}public void setErrMsg(String errMsg) {this.errMsg = errMsg;}public String getRoomId() {return roomId;}public void setRoomId(String roomId) {this.roomId = roomId;}public Integer getUserId1() {return userId1;}public void setUserId1(Integer userId1) {this.userId1 = userId1;}public Integer getUserId2() {return userId2;}public void setUserId2(Integer userId2) {this.userId2 = userId2;}public boolean isBlack() {return isBlack;}public void setBlack(boolean black) {isBlack = black;}
}
package org.ting.j20250110_gobang.game;public class GameRequest {private int userId;public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}public int getRow() {return row;}public void setRow(int row) {this.row = row;}public int getCol() {return col;}public void setCol(int col) {this.col = col;}private int row; //落子坐标行private int col;  //落子坐标列
}
package org.ting.j20250110_gobang.game;public class GameResponse {private String message;private int userid;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public int getUserid() {return userid;}public void setUserid(int userid) {this.userid = userid;}public int getRow() {return row;}public void setRow(int row) {this.row = row;}public int getCol() {return col;}public void setCol(int col) {this.col = col;}public int getWinnerId() {return winnerId;}public void setWinnerId(int winnerId) {this.winnerId = winnerId;}private int row; //落子坐标行private int col;  //落子坐标列private int winnerId; //是否分出胜负,-1表示未分出胜负,否则表示获胜玩家id}

4. 维护用户在线状态

前面我们在OnlineUserManager中使用了一个哈希表来维护用户的在线状态,但是当用户匹配成功后会进入游戏房间页面,从游戏大厅页面进入游戏房间页面,这个操作会使得我们在游戏大厅页面的webSocket连接断开,连接断开时,根据我们之前的代码来看,是会把玩家从哈希表中移除的,也就是把玩家下线,那么这个时候如果有人使用这个账号是可以在另一个地方登录成功的,就会造成两个账号同时在线的情况,为了避免这种情况我们在OnlineUserManager中再添加另一个哈希表用来维护在游戏房间中的用户:

package org.ting.j20250110_gobang.game;import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;@Component
public class OnlineUserManager {//使用ConcurrentHashMap保证线程安全//维护大厅中在线用户private Map<Integer, WebSocketSession> onlineUser = new ConcurrentHashMap<>();//维护游戏房间中的用户private Map<Integer, WebSocketSession> inRoomUser = new ConcurrentHashMap<>();public void enterGameRoom(int userId, WebSocketSession session) {//用户进入游戏房间inRoomUser.put(userId, session);}public void exitGameRoom(int userId) {//用户退出游戏房间inRoomUser.remove(userId);}public WebSocketSession getFromRoom(int userId) {//获取用户的websocket会话return inRoomUser.get(userId);}public void enterGameHall(int userId, WebSocketSession session) {//用户上线onlineUser.put(userId, session);}public void exitGameHall(int userId) {//用户下线onlineUser.remove(userId);}public WebSocketSession getFromHall(int userId) {//获取用户的websocket会话return onlineUser.get(userId);}
}

注意要更改之前玩家上线时代码的判断逻辑,还需要检测用户是否在游戏房间中:

http://www.dtcms.com/wzjs/44615.html

相关文章:

  • 网站建设书籍济宁seo推广
  • 长沙景点门票价格表关键词优化排名
  • 芜湖公司企业排名seo教程排名第一
  • 网站建设的市场策划首页排名seo
  • 互助平台网站建设费用专业地推团队
  • 南宁排名推广山东自助seo建站
  • 深圳市龙岗区做网站的公司seo行业网
  • 石家庄网站建设蓝点广告推广平台赚取佣金
  • sqlite 做网站深圳华强北新闻最新消息今天
  • wordpress虚拟储存安卓优化大师官方版本下载
  • 深圳哪里网站制作网页制作三大软件
  • 给我做网站的人老是给我留点尾巴灯塔seo
  • wordpress 数据导出广州谷歌seo
  • 网站建设维护委托合同黑帽seo论坛
  • 台州市城乡建设规划局网站seo01网站
  • 花瓣按照哪个网站做的seo是什么缩写
  • fireworks学习网站网站排行
  • 网站制作哪家专业5000人朋友圈推广多少钱
  • 土巴兔装修靠谱吗百度关键词优化查询
  • 在哪个网站做整形南昌seo搜索优化
  • 网站备案就是合法的引流客户的最快方法是什么
  • pc官方网站做网站公司
  • 淘宝网站开发框架山东一级造价师
  • 深圳网站建设制作网络公司什么是搜索推广
  • 汕头老城区图片seo研究中心好客站
  • 网站开发费用摊销吗提交网址给百度
  • 福州企业免费建站百度seo关键词优化电话
  • 网站的开发设计的技术合肥seo公司
  • 网站开发 合作协议旅游景点推广软文
  • 搭建一个网站需要多久南京百度推广