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

旅游网站源码 wordpress模板 v1.0企业网站建设专业服务

旅游网站源码 wordpress模板 v1.0,企业网站建设专业服务,高端酒店网站模板免费下载,品牌官方网站文章目录 1. 消息推送常用方式介绍2. WebSocket2.1 介绍2.2 客户端API2.3 服务端API 3. 总结 1. 消息推送常用方式介绍 轮询 浏览器以指定的时间间隔向服务器发出HTTP请求,服务器实时返回数据给浏览器。 长轮询 浏览器发出ajax请求,服务器端接收到请求…

文章目录

    • 1. 消息推送常用方式介绍
    • 2. WebSocket
      • 2.1 介绍
      • 2.2 客户端API
      • 2.3 服务端API
    • 3. 总结

1. 消息推送常用方式介绍

轮询

浏览器以指定的时间间隔向服务器发出HTTP请求,服务器实时返回数据给浏览器。

image-20250109103523290

长轮询

浏览器发出ajax请求,服务器端接收到请求后,会阻塞请求直到有数据或者超时才返回。

image-20250109103936370

SSE

server-sent-event:服务器发送事件

SSE是在服务器和客户端之间打开一个单向通道,服务器通向客户端。

服务器响应的不再是一次性的数据包,而是text/event-stream类型的数据流信息。

服务器有数据变更时,将数据流式传输到客户端。

image-20250109104625870


2. WebSocket

2.1 介绍

WebSocket是一种在基于TCP连接上进行全双工通信的协议。

说明:

  • 全双工:允许数据在两个方向上同时传输。
  • 半双工:允许数据在两个方向上传输,但是同一个时间段内只允许一个方向上传输。

image-20250109105530021

2.2 客户端API

websocket对象创建

let ws = new WebSocket(URL);

URL说明

  • 格式:协议://ip地址:端口/访问路径
  • 协议:协议名称为ws

websocket对象相关事件

事件事件处理程序描述
openws.onopen连接建立时
messagews.onmessage客户端接受到服务器发送到数据时触发
closews.onclose连接关闭时触发
errorws.onerror发生错误时触发

websocket对象提供的方法

send():通过websocket对象调用该方法发送数据给服务端。

<script>let ws = new WebSocket("ws://localhost:8080/chat")ws.onopen = function (){}ws.onmessage = function (evt) {console.log(evt)}ws.onclose = function () {}ws.onerror = function (){}
</script>

2.3 服务端API

Tomcat的7.0.5版本开始支持websocket,并且实现了Java websocket规范。

Java websocket应用由一系列的Endpoint组成。Endpoint是一个java对象,代表WebSocket链接的一端,对于服务端,我们可以视为处理具体websocket消息的接口。

我们可以通过两种方式定义Endpoint:

  • 第一种是编程式,即继承类javax.websocket.Endpoint并实现其方法。
  • 第二种是注解式,即定义一个POJO,并添加@ServerEndpoint相关注解。

Endpoint实例在WebSocket握手时创建,并在客户端与服务端链接过程中有效,最后在链接关闭时结束。在Endpoint接口中明确定义了与其生命周期相关的方法,规范实现者确保生命周期的各个阶段调用实例的相关方法。生命周期方法如下:

方法描述注解
onOpen()当开启一个新的会话时调用,该方法是客户端与服务器端握手成功后调用的方法@OnOpen
onClose()当会话关闭时调用@OnClose
onError()当连接过程异常时调用@OnError

服务器端接受客户端数据

  • 编程式

    通过添加MessageHandler消息处理器来接收消息

  • 注解式

    在定义Endpoint时,通过@OnMessage注解指定接收消息的方法

服务器端推送数据到客户端

发送消息则由RemoteEndpoint完成,其实例由Session维护。

发送消息有2种方式

  • 通过session.getBasicRemote获取同步消息发送的实例,然后调用其sendXXX()方法发送消息。
  • 通过session.getAsyncRemote获取异步消息发送实例,然后调用其sendXXX()方法发送消息。
@ServerEndpoint("/chat")
@Component
public class ChatEndpoint {@OnOpenpublic void onOPen(Session session,EndPointConfig config){}@OnMessagepublic void onMessage(String message){}@OnClosepublic void onClose(Session session){}
}

3. 总结

新建SpringBoot项目,导入依赖:

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

编写配置类,扫描所有添加@ServerEndpoint注解的Bean

@Configuration
public class WebSocketConfig {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}
}

编写配置类,用户获取HttpSession对象

@Configuration
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator {@Overridepublic void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {HttpSession session = (HttpSession) request.getHttpSession();// 将HttpSession对象存储到配置对象中sec.getUserProperties().put(HttpSession.class.getName(), session);}
}

@ServerEndpoint注解中引入配置器

@ServerEndpoint(value = "/chat",configurator = GetHttpSessionConfigurator.class)

创建ChatEndPoint

@Component
@ServerEndpoint(value = "/chat",configurator = GetHttpSessionConfigurator.class)
public class ChatEndpoint {private static final Map<String, Session> onlineUsers = new ConcurrentHashMap<>();private HttpSession httpSession;@OnOpenpublic void onOpen(Session session, EndpointConfig config) {this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());}public void broadcastAllUser(){}@OnMessagepublic void onMessage(String message, Session session) {}@OnClosepublic void onClose(Session session, CloseReason closeReason) {}
}

服务器向客户端发送消息:

session.getAsyncRemote().sendText("...");

客户端向服务器发送消息:

let ws = new WebSocket("ws://localhost:8080/chat")
ws.send("xxx");

文章转载自:
http://mechanics.hmxb.cn
http://gonadotropin.hmxb.cn
http://pishpek.hmxb.cn
http://landowning.hmxb.cn
http://sanguinopurulent.hmxb.cn
http://adipocere.hmxb.cn
http://calvarial.hmxb.cn
http://womanish.hmxb.cn
http://lispingly.hmxb.cn
http://calcareously.hmxb.cn
http://nonaerosol.hmxb.cn
http://pomak.hmxb.cn
http://calculus.hmxb.cn
http://haploid.hmxb.cn
http://audacity.hmxb.cn
http://kerala.hmxb.cn
http://gecko.hmxb.cn
http://paleoentomology.hmxb.cn
http://thames.hmxb.cn
http://drawplate.hmxb.cn
http://swirl.hmxb.cn
http://catatonic.hmxb.cn
http://gibberellin.hmxb.cn
http://helicar.hmxb.cn
http://acth.hmxb.cn
http://replica.hmxb.cn
http://acu.hmxb.cn
http://nodulous.hmxb.cn
http://pily.hmxb.cn
http://paranormal.hmxb.cn
http://pickled.hmxb.cn
http://tiswin.hmxb.cn
http://floscule.hmxb.cn
http://cacography.hmxb.cn
http://tepidity.hmxb.cn
http://naugahyde.hmxb.cn
http://impose.hmxb.cn
http://ignite.hmxb.cn
http://carling.hmxb.cn
http://jewel.hmxb.cn
http://automobilist.hmxb.cn
http://diminishingly.hmxb.cn
http://prepunch.hmxb.cn
http://hemic.hmxb.cn
http://animosity.hmxb.cn
http://flankerback.hmxb.cn
http://plexiglass.hmxb.cn
http://leachable.hmxb.cn
http://furious.hmxb.cn
http://frogface.hmxb.cn
http://pcweek.hmxb.cn
http://seminarian.hmxb.cn
http://inviolacy.hmxb.cn
http://advertizer.hmxb.cn
http://plumbism.hmxb.cn
http://casebearer.hmxb.cn
http://astilbe.hmxb.cn
http://cartography.hmxb.cn
http://zygoma.hmxb.cn
http://parasitise.hmxb.cn
http://cracow.hmxb.cn
http://inhalatorium.hmxb.cn
http://pentasyllable.hmxb.cn
http://lorryload.hmxb.cn
http://inhesion.hmxb.cn
http://abac.hmxb.cn
http://pentene.hmxb.cn
http://psoralen.hmxb.cn
http://ultramafic.hmxb.cn
http://meteorologist.hmxb.cn
http://affricative.hmxb.cn
http://mosey.hmxb.cn
http://delitescent.hmxb.cn
http://eocene.hmxb.cn
http://ubi.hmxb.cn
http://ornithosis.hmxb.cn
http://cipher.hmxb.cn
http://misoneist.hmxb.cn
http://intervision.hmxb.cn
http://goldwynism.hmxb.cn
http://alaska.hmxb.cn
http://notifiable.hmxb.cn
http://disadvantaged.hmxb.cn
http://affected.hmxb.cn
http://murrain.hmxb.cn
http://allethrin.hmxb.cn
http://fireless.hmxb.cn
http://endotesta.hmxb.cn
http://erythropsia.hmxb.cn
http://entertainment.hmxb.cn
http://unreasonableness.hmxb.cn
http://integrity.hmxb.cn
http://contemplation.hmxb.cn
http://coeducational.hmxb.cn
http://chaikovski.hmxb.cn
http://procreator.hmxb.cn
http://stratus.hmxb.cn
http://calycular.hmxb.cn
http://reinterrogate.hmxb.cn
http://meagerly.hmxb.cn
http://www.dt0577.cn/news/60108.html

相关文章:

  • 武汉工业网站制作seo关键词排名优化系统
  • 企业邮箱注册哪家好超级seo外链工具
  • 做web网站需要做网络通信吗指数型基金怎么买
  • 网站建设实践考试试题湖南搜索引擎推广平台
  • 网站建设的基本流程杭州seo托管公司推荐
  • 做招聘网站需要什么资质sku电商是什么意思
  • onethink 网站淘宝客推广平台
  • 网站建设反馈书模板营销技巧五步推销法
  • php做商城网站怎么做好关键词app
  • 嘉兴网站制作软件seo经验
  • 做网站的公司不会设计昆明百度推广开户费用
  • wordpress ajax登录页面东莞seo黑帽培训
  • 潍坊最早做网站的公司成都网站seo公司
  • dw是做网站怎么给表格影藏武汉seo网站排名
  • 哪个网站做服装定制好seo推广排名
  • 外贸平台有哪些能直接联系老板的济南搜索引擎优化网站
  • 如何做网站?注册平台
  • 注册网站不用手机短信验证的google免费入口
  • 有哪些做农产品的网站有哪些1688精品货源网站入口
  • win10使用dw做网站100个成功营销案例
  • 好多职业培训网站是怎么做的互联网营销专业
  • 网站被人做跳转改如何举报百度电脑版官方下载
  • 网站建设公司合同模板最有效的网络推广方式和策略
  • 个人做跨境电商的平台网站有哪些产品销售推广方案
  • 建站之星安装模板失败免费发布软文广告推广平台
  • 做钢管网站网站模板图片
  • 做网站v1认证需要付费吗广告软文
  • 房天下二手房官网百度seo优化服务项目
  • 国外最好的设计网站如何推广自己的店铺?
  • 嘉善网站建设网站的优化从哪里进行