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

企业门户网站实现江苏短视频seo搜索

企业门户网站实现,江苏短视频seo搜索,用ip访问没有备案的网站,wordpress的菜单和页面本文章完全参考了JSR 356, Java API for WebSocket。 JSR 356 JSR 356是Java的WebSocket实现API,是Java EE7的一部分。JSR 356包括WebSocket客户端API和服务端API,一般包括下面组件: 介于我们一般使用Java做服务端开发,这里我们…

本文章完全参考了JSR 356, Java API for WebSocket。

JSR 356

JSR 356是Java的WebSocket实现API,是Java EE7的一部分。JSR 356包括WebSocket客户端API和服务端API,一般包括下面组件:
Java WebSocket组件
介于我们一般使用Java做服务端开发,这里我们只实现服务端代码。

编程模型

JSR 356支持两种编程模型:

  • 基于注解。在POJO上使用注解。
  • 基于接口。实现类必须implements接口Endpoint

为了方便开发,我在springboot环境中编写代码。

基于注解的方式

  1. 定义业务控制器。
    在业务控制器类上使用@ServerEndpoint修饰,标识这是一个websocket服务,@ServerEndpointvalue属性表示请求的path。
@Component
@ServerEndpoint("/app/hello")
public class AnnotationGreetingEndpoint {}
  1. 接收消息
    在业务控制器类方法上使用@OnMessage修饰,用来接收客户端发送的消息。
    @OnMessagepublic void greeting(String text) throws IOException {System.out.println("AnnotationGreetingEndpoint<=" + text);}
  1. 发送消息
    发送消息需要使用WebSocket的Session对象。我们在业务控制器属性中定义Session session成员变量,在websocket链接建立的时候设置session值,使用@OnOpen注解修饰处理websocket链接建立逻辑的方法。
    private Session session;@OnOpenpublic void init(Session session) {this.session = session;}

发送一个同步消息可以使用session.getBasicRemote().sendText方法。

    @OnMessagepublic void greeting(String text) throws IOException {System.out.println("AnnotationGreetingEndpoint<=" + text);this.session.getBasicRemote().sendText("AnnotationGreetingEndpoint=>hello," + text);}

AnnotationGreetingEndpoint 类全部代码:

@Component
@ServerEndpoint("/app/hello")
public class AnnotationGreetingEndpoint {private Session session;@OnOpenpublic void init(Session session) {this.session = session;}@OnMessagepublic void greeting(String text) throws IOException {System.out.println("AnnotationGreetingEndpoint<=" + text);this.session.getBasicRemote().sendText("AnnotationGreetingEndpoint=>hello," + text);}@OnClosepublic void destroy() {this.session =  null;}
}
  1. 把业务类部署到websocket的web容器中。

使用Spring的ServerEndpointExporter自动把带有@ServerEndpoint注解的类添加的WebSocket容器中。

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

ServerEndpointExporter作为Spring的bean,自动注入ServerContainer属性。

public class ServerEndpointExporter extends WebApplicationObjectSupportimplements InitializingBean, SmartInitializingSingleton {@Nullableprivate ServerContainer serverContainer;@Overrideprotected void initServletContext(ServletContext servletContext) {if (this.serverContainer == null) {this.serverContainer =(ServerContainer) servletContext.getAttribute("javax.websocket.server.ServerContainer");}}@Overridepublic void afterPropertiesSet() {Assert.state(getServerContainer() != null, "javax.websocket.server.ServerContainer not available");}
}

并自动扫描Spring容器带有ServerEndpoint注解的bean,添加到ServerContainer中。

public class ServerEndpointExporter extends WebApplicationObjectSupportimplements InitializingBean, SmartInitializingSingleton {@Overridepublic void afterSingletonsInstantiated() {registerEndpoints();}protected void registerEndpoints() {Set<Class<?>> endpointClasses = new LinkedHashSet<>();ApplicationContext context = getApplicationContext();if (context != null) {String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class);for (String beanName : endpointBeanNames) {endpointClasses.add(context.getType(beanName));}}for (Class<?> endpointClass : endpointClasses) {registerEndpoint(endpointClass);}}private void registerEndpoint(Class<?> endpointClass) {ServerContainer serverContainer = getServerContainer();try {serverContainer.addEndpoint(endpointClass);}catch (DeploymentException ex) {throw new IllegalStateException("Failed to register @ServerEndpoint class: " + endpointClass, ex);}}
}
  1. 客户端调用

为了方便,我们直接打开浏览器窗口的控制台执行js客户端代码。

  1. 创建WebScoket对象。
ws =new WebSocket("ws://localhost:8090/app/hello");```
控制台返回:
```shell
·> WebSocket {url: 'ws://localhost:8090/app/hello___', readyState: 0, bufferedAmount: 0, onopen: null, onerror: null,}

2.添加消息回调方法。

// 直接把收到的消息打印在控制台
ws.onmessage=console.log

3.发送消息

ws.send("aodi");

控制台会打印onmessage回调的消息参数:

> MessageEvent {isTrusted: true, data: 'AnnotationGreetingEndpoint=>hello,aodi', origin: 'ws://localhost:8090', lastEventId: '', source: null, …}

在这里插入图片描述

基于接口的方式

  1. 定义业务控制。
    业务控制器类需要实现Endpoint接口。与基于注解的实现方式不同,在于Endpoint接口没有OnMessage方法,需要向Session中添加消息处理器来处理消息。这里直接给出实现AnnotationGreetingEndpoint相同功能的代码。
public class InterfaceGreetingEndpoint extends Endpoint {@Overridepublic void onOpen(Session session, EndpointConfig config) {session.addMessageHandler(new EchoMessageHandler(session));}public static class EchoMessageHandler implements MessageHandler.Whole<String> {private final Session session;public EchoMessageHandler(Session session) {this.session = session;}@Overridepublic void onMessage(String message) {System.out.println("InterfaceGreetingEndpoint<=" + message);try {session.getBasicRemote().sendText("InterfaceGreetingEndpoint=>hello," + message);} catch (IOException e) {//}}}
}
  1. 把业务类部署到websocket的web容器中。

通过ServerEndpointConfig把业务类控制器注入到Spring容器中。

@Configuration
public class WebSocketConfig {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}@Beanpublic ServerEndpointConfig serverEndpointConfig() {return ServerEndpointConfig.Builder.create(InterfaceGreetingEndpoint.class, "/app/hello_").build();}
}

搭配ServerEndpointExporter添加到websocket容器中。

public class ServerEndpointExporter extends WebApplicationObjectSupportimplements InitializingBean, SmartInitializingSingleton {@Overridepublic void afterSingletonsInstantiated() {registerEndpoints();}/*** Actually register the endpoints. Called by {@link #afterSingletonsInstantiated()}.*/protected void registerEndpoints() {ApplicationContext context = getApplicationContext();if (context != null) {Map<String, ServerEndpointConfig> endpointConfigMap = context.getBeansOfType(ServerEndpointConfig.class);for (ServerEndpointConfig endpointConfig : endpointConfigMap.values()) {registerEndpoint(endpointConfig);}}}private void registerEndpoint(ServerEndpointConfig endpointConfig) {ServerContainer serverContainer = getServerContainer();try {serverContainer.addEndpoint(endpointConfig);}catch (DeploymentException ex) {throw new IllegalStateException("Failed to register ServerEndpointConfig: " + endpointConfig, ex);}}
}
  1. 客户端调用。
    在这里插入图片描述
http://www.dtcms.com/wzjs/210004.html

相关文章:

  • 哪里找做网站的公司百度下载安装2019
  • 衙门口网站建设整合营销传播的六种方法
  • 丰宁县网站建设郑州网站关键词排名
  • 关于网站建设的书谷歌浏览器手机版
  • 亚圣信息科技做网站怎么样360手机助手
  • 云服务器做的网站需要备案国内seo公司哪家最好
  • 露天做愛偷拍网站sem优化是什么
  • 中型网站开发语言网站seo在线诊断分析
  • 南昌定制网站开发公司广告开户
  • 个人网站电商怎么做女教师网课入侵录屏冫
  • 专门做兼职的网站合肥网站seo推广
  • 商城网站有哪些功能公司网站营销
  • 农村电子商务网站建设方案关键词优化推广公司
  • cpa广告网站怎么做品牌软文范文
  • 网站优化提升排名seo准
  • 网站制作潍坊区域福州关键词快速排名
  • 网站建设公司 深圳信科nba最新交易新闻
  • 谁会建设网站清博舆情系统
  • 企业做网站优点营销排名seo
  • 江苏建设工程招投标网站市场调研报告怎么写范文
  • vue做门户网站用什么uiseo就业哪家好
  • 合工大网站建设试卷灰色关键词排名代发
  • 如何做 行业社交类网站千锋教育课程
  • 济南建站优化百度账号个人中心
  • 免费模板app下载seo关键词排名优化哪家好
  • seo短视频网页入口引流网站推荐成都seo学徒
  • 建网站用什么服务器好优化大师优化项目有
  • 郑州富士康招聘网官网成都百度提升优化
  • 网站主页跳转indexseo网络营销推广排名
  • 做游戏的php网站有哪些app接入广告变现