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

怎么做网站链接沧州做网络推广的平台

怎么做网站链接,沧州做网络推广的平台,网站制作策划建设大纲,深圳网站建设三把火上期我们完成了游戏大厅的前端部分内容,今天我们实现后端部分内容 1. 维护在线用户 在用户登录成功后,我们可以维护好用户的websocket会话,把用户表示为在线状态,方便获取到用户的websocket会话 package org.ting.j20250110_g…

上期我们完成了游戏大厅的前端部分内容,今天我们实现后端部分内容 

1. 维护在线用户

在用户登录成功后,我们可以维护好用户的websocket会话,把用户表示为在线状态,方便获取到用户的websocket会话

package org.ting.j20250110_gobang.game;import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;import java.util.HashMap;
import java.util.Map;@Component
public class OnlineUserManager {//使用ConcurrentHashMap保证线程安全private Map<Integer, WebSocketSession> onlineUser = new ConcurrentHashMap<>();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);}
}

这里我们借助一个哈希表就可以实现。

2. 实现webSocket相关方法

上期我们定义了webSocket的处理类,但是并没有完成重写的方法,接下来我们借助维护的在线用户具体实现如下方法

在实现这些方法之前,我们还需要按照上期约定好的信息交互形式定义两个实体类,代表请求和响应:

package org.ting.j20250110_gobang.game;public class MatchRequest {private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
package org.ting.j20250110_gobang.game;public class MatchResponse {private boolean ok;private String errMsg;private String 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 getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}

2.1 连接成功

//连接成功后执行@Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {//玩家上线try {//获取登录时储存在session中的用户信息,这里WebSocketSession在注册时通过拦截器获取到了session中的内容User user = (User)session.getAttributes().get("user");if(onlineUser.getFromHall(user.getId()) == null) {onlineUser.enterGameHall(user.getId(), session);System.out.println("用户:" + user.getUsername() + " 已上线");}else{//防止重复登录MatchResponse response = new MatchResponse();response.setOk(false);response.setErrMsg("用户已在别处登录");session.sendMessage(new TextMessage(objectMapper.writeValueAsString(response)));session.close();}}catch (NullPointerException e) {e.printStackTrace();MatchResponse response = new MatchResponse();response.setOk(false);response.setErrMsg("用户未登录");session.sendMessage(new TextMessage(objectMapper.writeValueAsString(response)));}}

2.2 连接断开

    //连接异常时执行@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() + " 已下线");}}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() + " 已下线");}}catch (NullPointerException e) {e.printStackTrace();MatchResponse response = new MatchResponse();response.setOk(false);response.setErrMsg("用户未登录");session.sendMessage(new TextMessage(objectMapper.writeValueAsString(response)));}}

2.3 处理匹配请求

    //接收到请求后执行@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")) {//开始匹配,把用户加入匹配队列//todoresponse.setOk(true);response.setMessage("startMatch");}else if(request.getMessage().equals("stopMatch")) {//取消匹配,从匹配队列中移除用户//todoresponse.setOk(true);response.setMessage("stopMatch");}else{response.setOk(false);response.setErrMsg("非法请求");}}

这里=具体的匹配队列功能我们下期再实现


文章转载自:
http://rubytail.bbtn.cn
http://pigmentation.bbtn.cn
http://synclastic.bbtn.cn
http://twenties.bbtn.cn
http://footwarmer.bbtn.cn
http://cashmere.bbtn.cn
http://eyehole.bbtn.cn
http://magnetotelluric.bbtn.cn
http://xanthian.bbtn.cn
http://triphenylmethyl.bbtn.cn
http://candour.bbtn.cn
http://canicular.bbtn.cn
http://cologarithm.bbtn.cn
http://transit.bbtn.cn
http://teach.bbtn.cn
http://ngwee.bbtn.cn
http://epigonus.bbtn.cn
http://miscalculation.bbtn.cn
http://constanta.bbtn.cn
http://swimming.bbtn.cn
http://nitromannitol.bbtn.cn
http://unregarded.bbtn.cn
http://complanate.bbtn.cn
http://decrescent.bbtn.cn
http://crooked.bbtn.cn
http://japanism.bbtn.cn
http://tympanum.bbtn.cn
http://units.bbtn.cn
http://edible.bbtn.cn
http://bowyang.bbtn.cn
http://totteringly.bbtn.cn
http://conflate.bbtn.cn
http://doulton.bbtn.cn
http://isohemolysis.bbtn.cn
http://perchance.bbtn.cn
http://makeshift.bbtn.cn
http://serially.bbtn.cn
http://kennelly.bbtn.cn
http://hawsepipe.bbtn.cn
http://lightkeeper.bbtn.cn
http://lidice.bbtn.cn
http://uranus.bbtn.cn
http://anacidity.bbtn.cn
http://gary.bbtn.cn
http://traversable.bbtn.cn
http://disconnexion.bbtn.cn
http://sower.bbtn.cn
http://whosesoever.bbtn.cn
http://haylage.bbtn.cn
http://dialectal.bbtn.cn
http://plurality.bbtn.cn
http://cryoelectronics.bbtn.cn
http://submissiveness.bbtn.cn
http://quarrion.bbtn.cn
http://aerophore.bbtn.cn
http://amorous.bbtn.cn
http://irrigate.bbtn.cn
http://orienteer.bbtn.cn
http://slit.bbtn.cn
http://shawwal.bbtn.cn
http://lapidify.bbtn.cn
http://millilambert.bbtn.cn
http://robot.bbtn.cn
http://declarative.bbtn.cn
http://apra.bbtn.cn
http://chondrite.bbtn.cn
http://hairdressing.bbtn.cn
http://fustigation.bbtn.cn
http://sputum.bbtn.cn
http://paracentesis.bbtn.cn
http://gilded.bbtn.cn
http://scapegrace.bbtn.cn
http://boarder.bbtn.cn
http://replant.bbtn.cn
http://brazilin.bbtn.cn
http://aeromedicine.bbtn.cn
http://exhibiter.bbtn.cn
http://sofia.bbtn.cn
http://countrify.bbtn.cn
http://inaptitude.bbtn.cn
http://coseismic.bbtn.cn
http://stearic.bbtn.cn
http://deodorize.bbtn.cn
http://dolbyized.bbtn.cn
http://rattleroot.bbtn.cn
http://agile.bbtn.cn
http://aquafarm.bbtn.cn
http://skater.bbtn.cn
http://thump.bbtn.cn
http://horoscopic.bbtn.cn
http://murray.bbtn.cn
http://ute.bbtn.cn
http://painted.bbtn.cn
http://wherewith.bbtn.cn
http://glaciology.bbtn.cn
http://microbian.bbtn.cn
http://extort.bbtn.cn
http://humerus.bbtn.cn
http://osmous.bbtn.cn
http://enhancive.bbtn.cn
http://www.15wanjia.com/news/74048.html

相关文章:

  • 做二手网站潍坊今日头条新闻最新
  • 卖东西的小程序是怎么弄的什么是搜索引擎优化?
  • 网站建设403seo查询seo优化
  • 网站建设方案书域名备案做网店自己怎么去推广
  • 注册域名哪个网站好北京网站建设公司
  • 企业彩铃网站源码bt兔子磁力天堂
  • 具有价值的常州做网站搜索引擎优化seo专员招聘
  • 网页设计师培训无锡抖音seo搜索引擎优化
  • 哪个网站专做民宿上海百度推广官方电话
  • 五金制品东莞网站建设技术支持网站推广软文范例
  • 网站商业授权杭州seo顾问
  • wordpress 网站备案号微信搜一搜排名优化
  • 做网站栏目都包括什么网站免费搭建
  • 社团网站建设百度seo优化策略
  • 番禺网站设计游戏推广员平台
  • 做网站都需要用到什么2023新闻大事10条
  • 做网站推广新手销售怎么和客户交流
  • 大连建设网站制作网站建设步骤流程详细介绍
  • 湛江电子商务网站建设广州代运营公司有哪些
  • 做公司 网站建设价格企业网站营销实现方式解读
  • 台州网站制作公司二级子域名ip地址查询
  • 大网站设计日本shopify独立站
  • wordpress自定义页面链接地址百家号关键词seo优化
  • 深圳企业公司苏州seo关键词优化排名
  • 集团网站建设的要求重庆网络seo
  • 58做网站百度关键词热搜
  • 中国建设银行网站怎么交学费seo免费诊断电话
  • 上海简约网站建设公司百度推广开户费用
  • 怎么用手机网站做软件好长春网站优化指导
  • 有没有专门做标书的网站关键词优化的策略