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

网站建设杭州哪家好高端网站建设优化

网站建设杭州哪家好,高端网站建设优化,wordpress 演示站,中迅做网站是模板站吗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/561178.html

相关文章:

  • 郑州百度网站推广做国外衣服的网站有哪些
  • 政务网站建设 紧急通知关键词优化的策略
  • 做网站虚拟主机价格网站开发毕设开题报告
  • 网站建设优化方案楼盘推荐排行榜
  • 网站备案免费吗济南互联网选号网站
  • 网站开发投标文件南京好的网站制作公司
  • 竹子建站公司在线logo设计免费生成器
  • 响应式网站建站价格广州十大软件公司排名
  • 廉溪区建设局网站怎么免费创建一个网站
  • 自适应网站建站价格横店建设网站
  • 农业企业网站建设大连虚拟主机
  • 网站排名优化软件有哪些外包公司工伤找谁赔偿
  • 自己做网站系统首选平台上海市城乡住房建设部网站
  • 电脑公司网站设计镇江网站建设多少钱
  • 四川省建设厅官方网站电话怎么样做英文网站
  • js代码能用在wordpress吗东莞网站制作十年乐云seo
  • 河北省建设厅网站重新安装零基础网站建设教程
  • 荥阳网站优化公司手机建设网站
  • apache 建立网站做电影网站犯罪吗
  • 网站集群建设通知设计制作活动
  • 东莞长安网站制作做英文网站费用多少
  • 辽宁建网站wordpress连接代码
  • 网站建设方面的书籍盗版网站是如何做的
  • 旅游网站建设模板下载公司宣传册页面设计模板
  • 微网站门户深圳创业补贴怎么申请
  • 宝安网站设计项目线下推广的渠道和方法
  • 淄博微网站网站版块建设
  • 企业网站建设需要哪些设备网络营销推广方法认定大将军21
  • 郑州制作网站费用网站图片上传功能怎么做的
  • 介绍国外的网站有什么不同免费网站怎么赚钱