网站文章要求,搜建筑网建筑规范大全,seo短视频网页入口营销策略,设计上海设计公司上期我们实现了websocket后端的大部分代码#xff0c;这期我们实现具体的匹配逻辑
1. 定义Mather类
我们新建一个Matcher类用来实现匹配逻辑
Component
public class Matcher {//每个匹配队列代表不同的段位,这里约定每一千分为一个段位private ArrayListQueueUser…上期我们实现了websocket后端的大部分代码这期我们实现具体的匹配逻辑
1. 定义Mather类
我们新建一个Matcher类用来实现匹配逻辑
Component
public class Matcher {//每个匹配队列代表不同的段位,这里约定每一千分为一个段位private ArrayListQueueUser matchQueueList new ArrayList();Autowiredprivate ObjectMapper objectMapper;Autowiredprivate OnlineUserManager onlineUserManager;public Matcher() {//暂定三个段位for(int i 0; i 3; i) {matchQueueList.add(new LinkedList());}}public void add(User user) {int index Math.min(user.getScore() / 3, 2);QueueUser queue matchQueueList.get(index);//对操作的队列加锁保证线程安全synchronized (queue) {queue.offer(user);queue.notify();}System.out.println(用户 user.getUsername() 加入了 index 号 队列);}public void remove(User user) {int index Math.min(user.getScore() / 3, 2);QueueUser queue matchQueueList.get(index);synchronized (queue) {queue.remove(user);}System.out.println(把用户 user.getUsername() 从 index 号 队列中删除);}
}2.修改websocket后端代码 //接收到请求后执行Overrideprotected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {User user (User) session.getAttributes().get(user);MatchRequest request objectMapper.readValue(message.getPayload(), MatchRequest.class);MatchResponse response new MatchResponse();if(request.getMessage().equals(startMatch)) {//开始匹配把用户加入匹配队列matcher.add(user);response.setOk(true);response.setMessage(startMatch);}else if(request.getMessage().equals(stopMatch)) {//取消匹配从匹配队列中移除用户matcher.remove(user);response.setOk(true);response.setMessage(stopMatch);}else{response.setOk(false);response.setErrMsg(非法请求);}//返回响应session.sendMessage(new TextMessage(objectMapper.writeValueAsString(response)));}
由于存在在匹配中途断开连接的情况所有我们还要在断开连接代码中增加退出队列的代码进行 //连接异常时执行Overridepublic void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {//连接异常断开玩家下线try {User user (User)session.getAttributes().get(user);//防止重复登录时删除正常登录的在线信息if(onlineUser.getFromHall(user.getId()).equals(session)) {onlineUser.exitGameHall(user.getId());System.out.println(用户 user.getUsername() 已下线);}//用户可能还在匹配队列中matcher.remove(user);}catch (NullPointerException e) {e.printStackTrace();MatchResponse response new MatchResponse();response.setOk(false);response.setErrMsg(用户未登录);session.sendMessage(new TextMessage(objectMapper.writeValueAsString(response)));}}//连接正常断开后执行Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {//连接正常断开玩家下线try {User user (User)session.getAttributes().get(user);//防止重复登录时删除正常登录的在线信息if(onlineUser.getFromHall(user.getId()).equals(session)) {onlineUser.exitGameHall(user.getId());System.out.println(用户 user.getUsername() 已下线);}//用户可能还在匹配队列中matcher.remove(user);}catch (NullPointerException e) {e.printStackTrace();MatchResponse response new MatchResponse();response.setOk(false);response.setErrMsg(用户未登录);session.sendMessage(new TextMessage(objectMapper.writeValueAsString(response)));}}
3. 实现匹配功能
3.1 创建线程扫描队列
我们为每个匹配队列创建一个线程用来实现匹配功能我们在构造方法中创建线程 public Matcher() {//暂定三个段位for(int i 0; i 3; i) {matchQueueList.add(new LinkedList());}//每个队列创建一个线程扫描完成匹配功能for(QueueUser q : matchQueueList) {Thread t new Thread(()-{while(true) {//调用handlerMatch()完成匹配功能handlerMatch(q);}});}}
3.2 实现handlerMatch()方法进行匹配
public void handlerMatch(QueueUser matchQueue) {try {//对操作的队列加锁保证线程安全synchronized (matchQueue) {//1.检测队列中是否有两个元素while(matchQueue.size() 2) {matchQueue.wait();}//2.从队列中取出两个玩家User user1 matchQueue.poll();User user2 matchQueue.poll();//3.获取到两个玩家的会话信息WebSocketSession session1 onlineUserManager.getFromHall(user1.getId());WebSocketSession session2 onlineUserManager.getFromHall(user2.getId());//4.todo 把两个玩家放到一个游戏房间中//5.给用户返回匹配成功的响应MatchResponse response new MatchResponse();response.setOk(true);response.setMessage(success);String json objectMapper.writeValueAsString(response);session1.sendMessage(new TextMessage(json));session2.sendMessage(new TextMessage(json));}}catch (IOException | InterruptedException e) {e.printStackTrace();}}
游戏房间功能我们下一期再实现。 文章转载自: http://www.morning.nstml.cn.gov.cn.nstml.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn http://www.morning.mqbzk.cn.gov.cn.mqbzk.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.clzly.cn.gov.cn.clzly.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.kpxky.cn.gov.cn.kpxky.cn http://www.morning.bbmx.cn.gov.cn.bbmx.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.tlnkz.cn.gov.cn.tlnkz.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.hzqjgas.com.gov.cn.hzqjgas.com http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.kbkcl.cn.gov.cn.kbkcl.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.jfnbh.cn.gov.cn.jfnbh.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.knzdt.cn.gov.cn.knzdt.cn http://www.morning.mslhq.cn.gov.cn.mslhq.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.kwdfn.cn.gov.cn.kwdfn.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.kkjhj.cn.gov.cn.kkjhj.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.hkgcx.cn.gov.cn.hkgcx.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn