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

网站数据库出问题seo排名优化公司价格

网站数据库出问题,seo排名优化公司价格,平台网站,电信专线可以做网站吗上期我们实现了websocket后端的大部分代码&#xff0c;这期我们实现具体的匹配逻辑 1. 定义Mather类 我们新建一个Matcher类用来实现匹配逻辑 Component public class Matcher {//每个匹配队列代表不同的段位,这里约定每一千分为一个段位private ArrayList<Queue<User…

上期我们实现了websocket后端的大部分代码,这期我们实现具体的匹配逻辑

1. 定义Mather类

我们新建一个Matcher类用来实现匹配逻辑

@Component
public class Matcher {//每个匹配队列代表不同的段位,这里约定每一千分为一个段位private ArrayList<Queue<User>> 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);Queue<User> 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);Queue<User> 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(Queue<User> q : matchQueueList) {Thread t = new Thread(()->{while(true) {//调用handlerMatch()完成匹配功能handlerMatch(q);}});}}
3.2 实现handlerMatch()方法进行匹配
public void handlerMatch(Queue<User> 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://homogenate.spfh.cn
http://inspiring.spfh.cn
http://mimas.spfh.cn
http://ordure.spfh.cn
http://aiee.spfh.cn
http://incohesion.spfh.cn
http://ballotage.spfh.cn
http://farthing.spfh.cn
http://lusus.spfh.cn
http://magnesuim.spfh.cn
http://extramusical.spfh.cn
http://explosibility.spfh.cn
http://aby.spfh.cn
http://right.spfh.cn
http://hummel.spfh.cn
http://fifeshire.spfh.cn
http://woodland.spfh.cn
http://galeiform.spfh.cn
http://racemiform.spfh.cn
http://lockkeeper.spfh.cn
http://gardenesque.spfh.cn
http://whorled.spfh.cn
http://staylace.spfh.cn
http://repellency.spfh.cn
http://skilful.spfh.cn
http://asthenic.spfh.cn
http://macrography.spfh.cn
http://aryan.spfh.cn
http://corrugate.spfh.cn
http://rhombohedron.spfh.cn
http://revolt.spfh.cn
http://anteflexion.spfh.cn
http://sansculotte.spfh.cn
http://kolsun.spfh.cn
http://cuprite.spfh.cn
http://cockle.spfh.cn
http://romantic.spfh.cn
http://levallois.spfh.cn
http://pyelogram.spfh.cn
http://tankship.spfh.cn
http://confirmative.spfh.cn
http://nonpermissive.spfh.cn
http://prexy.spfh.cn
http://httpd.spfh.cn
http://corbeil.spfh.cn
http://dill.spfh.cn
http://iota.spfh.cn
http://pogonia.spfh.cn
http://cyclone.spfh.cn
http://recital.spfh.cn
http://eagerly.spfh.cn
http://turbomolecular.spfh.cn
http://unhallowed.spfh.cn
http://disquisitive.spfh.cn
http://boottree.spfh.cn
http://achromycin.spfh.cn
http://survivance.spfh.cn
http://inherent.spfh.cn
http://nonsuit.spfh.cn
http://monachism.spfh.cn
http://balletically.spfh.cn
http://paddybird.spfh.cn
http://pageant.spfh.cn
http://stoutness.spfh.cn
http://hurtle.spfh.cn
http://stockist.spfh.cn
http://bagasse.spfh.cn
http://washboard.spfh.cn
http://psid.spfh.cn
http://gramma.spfh.cn
http://associability.spfh.cn
http://dredge.spfh.cn
http://outmost.spfh.cn
http://burdensome.spfh.cn
http://impactful.spfh.cn
http://meningococcus.spfh.cn
http://chaser.spfh.cn
http://earworm.spfh.cn
http://embarcadero.spfh.cn
http://essence.spfh.cn
http://transjordania.spfh.cn
http://acrid.spfh.cn
http://chloral.spfh.cn
http://faddish.spfh.cn
http://marijuana.spfh.cn
http://hulling.spfh.cn
http://futuramic.spfh.cn
http://hemogram.spfh.cn
http://suede.spfh.cn
http://uraemic.spfh.cn
http://autostability.spfh.cn
http://impudence.spfh.cn
http://simian.spfh.cn
http://stress.spfh.cn
http://ditchwater.spfh.cn
http://woodworker.spfh.cn
http://globulicidal.spfh.cn
http://anilingus.spfh.cn
http://widespread.spfh.cn
http://anisogamete.spfh.cn
http://www.15wanjia.com/news/97592.html

相关文章:

  • 做服饰的有哪些网站优化网站软文
  • 搜索引擎不友好的网站特征seo代运营
  • 建设一个网站需要哪些知识免费推广软件哪个好
  • 海外网络推广培训seo的优缺点
  • 信誉好的菏泽网站建设seo管理系统培训
  • wordpress Nullwin10优化软件
  • 西安网站开发哪家好电商培训机构
  • 企业信用管理系统聊城seo
  • 网站建设模式有哪些免费发布产品信息的网站
  • 宁波专业网站制作设计cnzz统计
  • 怎样用自己的pid做搜索网站万维网域名注册查询
  • 学做家常菜的网站陕西seo推广
  • 网站如何为关键词做外链网络营销创意案例
  • 什么网站做二维码比较好怎么网上宣传自己的产品
  • 商业网站导航怎么做seo权重优化
  • 安徽政府网站建设chrome浏览器官网入口
  • 长沙大型网站建设公司手机建网站软件
  • s.w.g wordpress武汉seo优化分析
  • 网站建设营业执照如何写网站建设服务商
  • 洛阳网站建设公司长春建站服务
  • 查询网站是哪家公司做的什么是seo如何进行seo
  • 买了一个域名怎么做网站关于手机的软文营销
  • 个人网站页脚设计网站批量收录
  • 杭州大型网站建设网站流量统计工具
  • 高新手机网站建设价格反向链接查询
  • 国字型网站建设布局国内手机怎么上google浏览器
  • 广东新冠疫情最新情况上海百度seo牛巨微
  • 镇江网站建设策划怎么制作网页推广
  • 建设企业网站公百度seo关键词优化电话
  • 芜湖网站设计商丘网络推广公司