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

网站内容建设和管理现在推广一般都用什么软件

网站内容建设和管理,现在推广一般都用什么软件,企业备案 网站服务内容,怎么做电影网站这里写自定义目录标题 一、消息推送常见方式二、websocket 是什么?三、websocket api的介绍1、客户端 (浏览器)2、服务端api 四、实现在线聊天室1、需求2、聊天室流程分析3、消息格式4、代码实现 一、消息推送常见方式 1、轮训方式 2、SSE…

这里写自定义目录标题

  • 一、消息推送常见方式
  • 二、websocket 是什么?
  • 三、websocket api的介绍
    • 1、客户端 (浏览器)
    • 2、服务端api
  • 四、实现在线聊天室
    • 1、需求
    • 2、聊天室流程分析
    • 3、消息格式
    • 4、代码实现

一、消息推送常见方式

1、轮训方式
在这里插入图片描述

2、SSE(server-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) 引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency>

2)编写配置类

扫描所有添加 @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 Map<String, 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 集合Set<Map.Entry<String, Session>> entries = onlineUsers.entrySet();for (Map.Entry<String, 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.hkea.cn/news/413896/

相关文章:

  • 网站建设小企业案例漯河网络推广哪家好
  • wordpress 清空回收站合肥网站优化软件
  • 电站建设招聘网站智推教育seo课程
  • 做静态网站选用什么服务器站长素材网站
  • 网站建设先做前台还是后台百度认证是什么
  • 广州专业做crm系统的供应商seo网站培训班
  • 景安建网站企业网站seo方案案例
  • 山东滕州疫情最新消息今天i长沙官网seo
  • 公司做网站买域名之后做什么百度一下你就知道手机版
  • 北京婚恋网站哪家最好企业推广宣传方式
  • 国发网站建设西安做网站公司
  • 网站推广服务合同简述网络营销的主要方法
  • 信息门户网站是什么成人计算机培训机构哪个最好
  • 网站建设公司 中企动力公司东莞商城网站建设
  • b2c的电子商务网站自己想做个网站怎么做
  • 京东pc网站用什么做的如何注册网站怎么注册
  • 长沙商城网站制作seo线下培训课程
  • web网站开发公司网站制作优化排名
  • 这么做3d网站企业邮箱网页版
  • 瑞安网站建设公司关键词排名网络推广
  • 南京学做网站友情链接检查工具
  • 参考文献网站开发百度重庆营销中心
  • 如何做微信ppt模板下载网站企业网页设计公司
  • 做b2b网站百度点击快速排名
  • 网站怎么做移动图片不显示不出来吗芭嘞seo
  • 旅游网站建设服务器ip域名解析
  • 企业网站建设三个原则百度指数资讯指数是指什么
  • 房地产集团网站建设方案软文文案案例
  • 阜蒙县建设学校网站是什么北京seo编辑
  • 珠海建设局网站十大经典事件营销案例分析