当前位置: 首页 > 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/201064.html

相关文章:

  • 服务器搭建网站视频教程武汉seo认可搜点网络
  • wordpress 前台刷新济南做seo排名
  • 如何做1个手机网站b站暴躁姐
  • 网站开发项目流程图网址域名注册信息查询
  • dw做网站图片运用温州高端网站建设
  • 连云港网站制作公司哪家好网络培训学校
  • 制作网站网页域名的公司爱站网
  • 祖传做网站企业培训心得
  • 网站顶部广告图片电商代运营十大公司排名
  • 我想要个网站自己想开个网站怎么弄
  • 网站建设公司不赚钱市场调研分析
  • 网站开发功能简介制作公司网站
  • 网络开发公司济南优化seo公司
  • 网站 做 专家问答产品怎么进行推广
  • 自己怎么建网站卖东西百度app客服人工在线咨询
  • 做网站用什么语言编写北京网站建设开发公司
  • 有免费做理化试验的网站吗最近的新闻热点时事
  • 网站创建需要什么淘宝推广平台
  • 可以做试卷网站数学试卷小学六营销策划经典案例
  • 算命网站开发电话宜昌网站seo收费
  • 厦门网站建设系统seo优化包括哪些内容
  • php网站开发框架搭建广告公司
  • 上什么网站做会计教育淄博网站制作
  • 网站设计大概多少钱百度登录入口百度
  • 工信部网站bbs备案以网络营销为主题的论文
  • 建设工程公司 网站百度升级最新版本
  • 手机网站制作推广定制百度资源站长平台
  • 网络规划设计师一本通seo网站内容优化有哪些
  • h5手机网站发展趋势厦门最好的seo公司
  • 怎么做博彩网站推广镇江抖音seo