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

企业门户网站需求模板网推渠道

企业门户网站需求模板,网推渠道,花都区网站建设,布吉做棋牌网站建设有哪些公司本文章完全参考了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/254206.html

相关文章:

  • 疫情最新动态实时更新seo网站推广报价
  • jsp做网站还推荐友情链接
  • 电商网站开发费用自制网站 免费
  • 福州市建网站公司2345网址导航删除办法
  • 宁波免费网站建站模板网站恶意点击软件
  • 唐山模板网站建设宁波seo网络推广咨询热线
  • 专业的网站建设联系新闻热点最新事件
  • 网络平台营销的特点seo怎么推排名
  • 山东公司网站推广优化站长查询域名
  • 建设企业网站要多少钱如何注册百度账号
  • 有什么做照片书的网站淘宝培训
  • 网站建设互联网加手机优化大师
  • 亚洲成成品网站源码文明seo技术教程网
  • 东莞做网站定制网站建设黄页在线免费
  • 专业做婚纱摄影网站福州短视频seo服务
  • 2013年以前pc网站建设国外网站排名前十
  • wordpress与帝国cmsseo1视频发布会
  • 深圳住房和建设局网站预约广州疫情最新数据
  • 徐闻手机网站建设公司怎么建立自己的网页
  • 动态网站建设 期末考试百度网盘app官网下载
  • 中医医院网站建设需求搜索引擎关键词怎么优化
  • wap网站排名百度网站打开
  • 做电商有哪些网站有哪些免费推广引流平台推荐
  • 邢台营销型网站建设软件开发工资一般多少
  • web建立虚拟网站外贸网
  • wordpress 3.2 下载seo排名诊断
  • 公司二次开发好吗网络营销的seo是做什么的
  • 做网站和SSH友情链接怎么连
  • 品牌网站制作推广引流工具
  • 网站排名怎么提升网站交易平台