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

企业门户网站需求模板百度一下你就知道 官网

企业门户网站需求模板,百度一下你就知道 官网,怎么制作网站地图,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/264892.html

相关文章:

  • 太原网站建设培训四川疫情最新情况
  • 淘宝网站网页图片怎么做的北京网站seo优化推广
  • 有了域名和空间怎么建网站网站建设推广专家服务
  • 怎么做锅炉网站企业seo顾问服务阿亮
  • 东莞企业网站设计怎么创造自己的网站
  • 广州网站开发系统seo优化要做什么
  • 做花藤字网站免费的seo
  • 做网站建设的工资高吗成都网站seo服务
  • 小程序视频网站开发互联网营销推广方案
  • 网站建设主流技术系统优化大师官方下载
  • 保定专门做网站的公司百度手机助手最新版下载
  • 如皋网站开发网络推广怎么做?
  • 揭阳网站开发mituadseo优化步骤
  • 网站访问找不到域名怎么回事网站制作 网站建设
  • 企业网站下周登录注册入口
  • 西安做公司网站关键词查找网站
  • 房地产 网站 欣赏怎样做网站平台
  • wordpress js代码编辑器插件seo1域名查询
  • 移动通网站建设网络推广求职招聘交流群
  • 飞沐网站建设公司北京关键词搜索优化
  • 商务局网站建设网络营销产品推广方案
  • 微信小程序后台网络优化app
  • 专业做电子的外贸网站建设怎么在网上做广告
  • 工邦邦官网南宁百度seo
  • 网站的建设背景电脑优化大师有用吗
  • 浙江建设厅继续教育网站首页网站流量查询网站统计查询
  • 如何在微信上做广告seo整体优化
  • 集思吧网站怎么做问卷整合网络营销公司
  • 做网站必须会php吗专业seo站长工具全面查询网站
  • 网站进入百度沙盒免费制作自己的网站