WebSocket Java Programming入门-2(Programmatic)
来源:互联网 发布:杜兰特去勇士知乎 编辑:程序博客网 时间:2024/06/05 09:08
在http://blog.csdn.net/wangwenjun69/article/details/45967849 中我们介绍了如何采用注解的方式来实现一个简单的WebSocket基本应用,在本篇中我们介绍一下编程的方式实现上一篇的例子
为了完整期间,我还是将所有用到的配置以及代码都贴出来,读者不要认为我很繁琐即可
1、Maven POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>websocket</artifactId> <groupId>websocket</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>websocket-programmatic</artifactId> <packaging>war</packaging> <name>websocket-programmatic Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>websocket-programmatic</finalName> </build></project>
其实最主要是想用websocket api的包用于参与编码,在运行期间,web容器早已自带这个包了。
2、服务端代码
服务端的ServerEndpoint不仅要继承Endpoint接口还要编写一个ServerApplicationConfig,同样web容器会自动探测到相关的ServerEndpoint
2.1、ProgrammaticHelloWorldServer类
package com.wangwenjun.websocket.programmatic;import javax.websocket.Endpoint;import javax.websocket.EndpointConfig;import javax.websocket.MessageHandler;import javax.websocket.Session;import java.io.IOException;public class ProgrammaticHelloWorldServer extends Endpoint{ @Override public void onOpen(Session session, EndpointConfig endpointConfig) { final Session mySession = session; mySession.addMessageHandler(new MessageHandler.Whole<String>(){ @Override public void onMessage(String s) { try { mySession.getBasicRemote().sendText("I got this ("+s+") from you,so i am sending it back!"); } catch (IOException e) { e.printStackTrace(); } } }); }}
好的,这样我们就实现了一个编程式ServerEndpoint类,但是此时支持Websocket的web容器还是不知道该如何去寻找我们的ServerEndpoint,需要我们配置进ServerApplicationConfig才可以
2.2、ServerApplicationConfig的配置
package com.wangwenjun.websocket.programmatic;import javax.websocket.Endpoint;import javax.websocket.server.ServerApplicationConfig;import javax.websocket.server.ServerEndpointConfig;import java.util.HashSet;import java.util.Set;public class ProgrammaticConfig implements ServerApplicationConfig { @Override public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> classes) { Set<ServerEndpointConfig> sets = new HashSet<ServerEndpointConfig>(); ServerEndpointConfig config = ServerEndpointConfig.Builder.create(ProgrammaticHelloWorldServer.class, "/hello").build(); sets.add(config); return sets; } @Override public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanner) { return scanner; }}
3、发布运行
同样我们需要运行mvn clean package将web项目打成一个war包,并且拷贝到tomcat中的webapps下,然后启动tomcat,你会看到发布成功,然后访问地址http://localhost:8080/websocket-programmatic/ 这样就可以和服务器进行交互了。
4、《JAVA WEBSOCKET PROGRAMMING》中的一个问题
其实参照这么书很多东西都是没问题的,但是在编程式方式中,本书中描述的是实现ServerApplicationConfiguration接口,但是该接口在websocket API中不存在,可能是我引入的版本比较老,或者是作者使用的版本比较老,总之这一部分是有问题的,我在代码中作了修改,只要照着我的代码肯定没有任何问题的。
5、后续文章
好了,关于WebSocket的基本应用我们基本上了解的差不多了,虽然暂时只是一个比较浅层次的理解,但是不要过于担心,在接下来我们会继续深入的学习,并且经过一些很有代表性的例子来去学习Websocket,比如会包括如下的内容:
- websocket的生命周期
- websocket的message详细介绍
- websocket的Session以及config
- websocket的高级Message
- websocket的path mapping
- websocket的加密通讯
- websocket在其他J2EE容器下的使用
- websocket的例子,聊天室,在线查看后台服务的日志等
- 介绍基于websocket的网页版本的WebSSH(python和websocket的创建的一个非常牛鼻的SSH工具)
0 0
- WebSocket Java Programming入门-2(Programmatic)
- WebSocket Java Programming入门-1(annotated)
- Programmatic programming, Declarative programming and Generative programming
- webSocket入门java实现
- Java websocket入门
- WebSocket 框架快速入门(2)
- programming challenge 1(基础入门2)
- express入门(12)- websocket
- websocket入门
- WebSocket入门
- 入门 websocket
- WebSocket入门
- webSocket入门
- WebSocket入门
- WebSocket入门
- WebSocket学习总结(2)——WebSocket与java消息推送框架
- WebSocket(2)--为什么引入WebSocket协议
- WebSocket(2)--为什么引入WebSocket协议
- spark学习逻辑回归
- oracle 添加用户
- <C++ primer>习题16.24
- java socket编程
- LR11+WIN7+64位+IE8启动不了浏览器
- WebSocket Java Programming入门-2(Programmatic)
- Visual Studio中Image Watch的使用
- 发送报文出现java.io.IOException:Invalid keystore format ...
- Java Android SSL 双向认证代码
- MySQL Cluster 3台机器搭建集群环境
- object-c中的assign,retain,copy,atomic,nonatomic,readonly,readwrite以及strong,weak
- FineReport7.0 + Tomcat + PostgreSQL集成
- 25个Git进阶技巧
- Linux进程管理(2)