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

山东省建设厅举报网站网站风格分类

山东省建设厅举报网站,网站风格分类,ppt免费模板大全,微博优惠券网站怎么做的这里写自定义目录标题 一、消息推送常见方式二、websocket 是什么#xff1f;三、websocket api的介绍1、客户端 #xff08;浏览器#xff09;2、服务端api 四、实现在线聊天室1、需求2、聊天室流程分析3、消息格式4、代码实现 一、消息推送常见方式 1、轮训方式 2、SSE… 这里写自定义目录标题 一、消息推送常见方式二、websocket 是什么三、websocket api的介绍1、客户端 浏览器2、服务端api 四、实现在线聊天室1、需求2、聊天室流程分析3、消息格式4、代码实现 一、消息推送常见方式 1、轮训方式 2、SSEserver-send event服务器发送事件 3、websocket 二、websocket 是什么 websocket 是一种基于TCP 连接上进行全双工通信的协议 三、websocket api的介绍 1、客户端 浏览器 websocket对象创建 websocket对象相关事件 websocket 对象提供的方法 2、服务端api Tomcat 的7.0.5版本开始支持 websocket , 并且实现了Java websocket 规范。 Java websocket 应用由一系列的 Endpoint组成。 Endpoint 是一个java对象代表websocket连接的一段。对于服务端我们可以视其为处理具体websocket消息的接口。 我们可以通过两种方式定义EndPoint 编程式继承类 javax.websocket.Endpoint 并实现其方法 注解式定义一个pojo并添加ServerEndPoint相关注解 服务端如何接收客户端发过来的数据呢 编程式 通过添加MessageHandler消息处理器来接收消息 注解式 在定义endpoint时通过OnMessage 注解指定接收消息的方法 服务器如何推送消息给客户端 发送消息由RemoteEndpoint完成其实例由Session维护。 四、实现在线聊天室 1、需求 通过websocket实现在线聊天室 2、聊天室流程分析 3、消息格式 4、代码实现 1 引入依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-websocket/artifactId/dependency2编写配置类 扫描所有添加 ServerEndpoint注解的Bean Configuration public class WebsocketConfig {Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();} }编写配置类用于获取 HttpSession 对象 public class GetHttpSessionConfig extends ServerEndpointConfig.Configurator {/*** param sec* param request 握手请求* param response*/Overridepublic void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {// 获取HttpSession对象HttpSession httpSession (HttpSession) request.getHttpSession();// 将 HttpSession对象保存起来sec.getUserProperties().put(HttpSession.class.getName(), httpSession);} } 在ServerEndpoint 注解中引入配置器 ServerEndpoint(value “/chat”, configurator GetHttpSessionConfig.class) ServerEndpoint(value /chat, configurator GetHttpSessionConfig.class) Component public class ChatEndpoint {/*** 使用static* ChatEndpoint是多例的多个实例共享一个Map对象*/private static final MapString, Session onlineUsers new ConcurrentHashMap();private HttpSession httpSession;OnOpenpublic void onOpen(Session session, EndpointConfig endpointConfig) {this.httpSession (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());String userName (String) httpSession.getAttribute(user);// 1、将我们的session进行保存onlineUsers.put(userName, session);// 2、广播消息将登录的用户推送给所有的用户String message MessageUtils.getMessage(true, null, userName 上线);boardcast(message);// 3、}/*** 广播消息*/private void boardcast(String message) {// 遍历 map 集合SetMap.EntryString, Session entries onlineUsers.entrySet();for (Map.EntryString, Session entry : entries) {Session session entry.getValue();try {session.getBasicRemote().sendText(message);} catch (IOException e) {// 记录日志}}}/*** 浏览器发送消息到服务端该方法会被调用*/OnMessagepublic void onMessage(String message, EndpointConfig endpointConfig) {try {this.httpSession (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());String fromName (String) httpSession.getAttribute(user);// 将消息推送给指定的用户 message : {toName:张三,message:你好}ClientMessage message1 JSON.parseObject(message, ClientMessage.class);String toName message1.getToName();Session session onlineUsers.get(toName);String message2 MessageUtils.getMessage(false, fromName, message1.getMessage());session.getBasicRemote().sendText(message2);} catch (IOException e) {throw new RuntimeException(e);}}/*** 当websocket连接断开时此方法会被处罚*/OnClosepublic void onClose(Session session, EndpointConfig endpointConfig) {// 从在线用户集合中剔除断开连接的用户this.httpSession (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());String userName (String) httpSession.getAttribute(user);onlineUsers.remove(userName);// 通知其他用户当前用户下线String message MessageUtils.getMessage(true, null, userName 上线);boardcast(message);} }
http://www.w-s-a.com/news/604351/

相关文章:

  • 海南响应式网站建设哪里好瑞安电影城网站建设
  • wordpress widgetkit济南优化网站厂家
  • 麦片网站建设佛山短视频推广渠道
  • 免费自助建网站销售的网络建设
  • 传媒大气的网站网站怎么做分类聚合
  • 网站可以自己备案吗crm系统架构图
  • 罗湖网站建设58做网站的公司盐城
  • 网站开发答辩想要去网站做友情链接怎么发邮件
  • 网站名称填写什么广告网络推广怎么做
  • 做网站架构需要注意什么百度竞价排名推广
  • 网站接口设置地税局内网网站建设
  • 谷歌提交网站入口wordpress前台自动登录
  • 规模以上工业企业的标准是什么洛阳霞光seo网络公司
  • 怎样用文本建一个网站做美容美发学校网站公司
  • 南宁企业网站建设制作芜湖网站建设推广
  • 泉州市建设局网站公示深圳建站公司好坏
  • 如何搭建网站教程一个人制作网站
  • 网站开发专业都有哪些课程广州安全教育平台账号找回
  • 网站调整方案适合平面设计师的网站
  • 免费服务器建立网站用html5做的旅游网站代码
  • 学校英语网站栏目名称WordPress禁用邮件注册
  • 手机qq网页版网站沧州手机网站开发
  • 深圳罗湖网站设计公司建设的网站属于无形资产吗
  • 网站开发python西安网站建站品牌
  • 网站开发商标属于哪一类做网站还有钱赚吗
  • 做设计的搜素材上什么网站好设计公司画册设计哪家好
  • 视频网站开发需要什么语言做ui设计一年后年薪多少
  • 网站服务器维护费用统一企业官方网站
  • 网站如何调用手机淘宝做淘宝客呼和浩特网站运营公司
  • 做推广可以上那些网站网页游戏排行榜2014前十名