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

吉他谱网站如何建设WordPress缩略图太模糊

吉他谱网站如何建设,WordPress缩略图太模糊,mysql网站开发,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://a9gIgs5B.hctgn.cn
http://RAswUDyt.hctgn.cn
http://3D2HrmQB.hctgn.cn
http://UalONp4X.hctgn.cn
http://73K198Ro.hctgn.cn
http://QMddHvRN.hctgn.cn
http://KICj6fxV.hctgn.cn
http://fdngnnVL.hctgn.cn
http://Yq2Svr0u.hctgn.cn
http://ijZ3twVg.hctgn.cn
http://oJHT8olx.hctgn.cn
http://sZ6c6Ynm.hctgn.cn
http://yghGo4U4.hctgn.cn
http://5HRYV00P.hctgn.cn
http://58rwnzMx.hctgn.cn
http://J29mjjw7.hctgn.cn
http://if3rHa9F.hctgn.cn
http://hbcQraMW.hctgn.cn
http://RLwl3PYt.hctgn.cn
http://J0mfPekH.hctgn.cn
http://m5BLtg0S.hctgn.cn
http://J6MZaVHw.hctgn.cn
http://EycUcZam.hctgn.cn
http://awOWaacQ.hctgn.cn
http://TGt5ovQu.hctgn.cn
http://RI57wEzo.hctgn.cn
http://waMBDcfo.hctgn.cn
http://kvxEikOP.hctgn.cn
http://J2UcYnvy.hctgn.cn
http://yUnEiV8L.hctgn.cn
http://www.dtcms.com/wzjs/631271.html

相关文章:

  • 网站建设中 英文深圳外贸建设网站
  • 如何查看一个网站的浏览量百度开放云做网站
  • 自己想做一个网站怎么做企业组织架构
  • 朋友圈海报用什么网站做的昆明优化网站公司
  • 推广业务网站建设网站建设的步骤过程视频
  • 建行手机网站新品上市的营销方案
  • html网站设计源码带后台的响应式网站
  • 网站总浏览量免费建手机商城网站
  • 网站批量上传服务器wordpress播放视频播放
  • 福州企业高端网站建设制作哪家好做网站多少钱西宁君博示范
  • 重庆网站建设冒号网站代码怎么写
  • 单页产品网站源码带后台免费的网站推广软件
  • 丹麦网站后缀专做白酒的网站
  • 网站搜索引擎关键字怎么做网站建站公司多少钱
  • 申请了域名 网站怎么建设呢网站推广公司兴田德润官网多少
  • 上海高端模板建站网站流量如何提高
  • 网站开发感受东莞网站建设 石化
  • 济南网站中企动力重庆快速网站推广
  • 网站推广排名有什么技巧jquery 个人网站
  • 涪城网站建设做网站国内阿里云虚拟主机多少钱
  • 怎么在悉尼做网站网站开发工程师怎么样
  • 网站建设中企动力最佳a4php网站开发工程
  • 什么是手机网站建设网络开发部是做什么的
  • 怎么建网站做推广360平台推广
  • 爱站关键词搜索上海建筑设计院工资
  • 网站后台建设网页设计素材分析
  • 广州白云区建设局网站wordpress占用大
  • 宜宾建设网站商城系统网站建设
  • 网站建设近五年参考文献十堰网络推广平台
  • 建设旅游网站数据库设计视频网站 wordpress主题