Tomcat实现Web Socket
原文链接:https://www.dubby.cn/detail.html?id=9103
1、依赖
本文使用的是Tomcat9
项目结构也是最基本的servlet的项目结构:
代码地址:github.com/dubby1994/t…
其实啥依赖都不需要,但是需要几个api,这些在Tomcat里都已经提供了,但是代码里还是需要提供一下,不然编译报错:
<dependency><groupId>javax.websocket</groupId><artifactId>javax.websocket-api</artifactId><version>1.0</version><scope>provided</scope>
</dependency>
复制代码
2、配置
ExamplesConfig.java
package cn.dubby.tomcat.study.config;
import cn.dubby.tomcat.study.ws.EchoEndpoint;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpointConfig;
import java.util.HashSet;
import java.util.Set;public class ExamplesConfig implements ServerApplicationConfig {@Overridepublic Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scanned) {Set<ServerEndpointConfig> result = new HashSet<>();if (scanned.contains(EchoEndpoint.class)) {result.add(ServerEndpointConfig.Builder.create(EchoEndpoint.class, "/websocket").build());}return result;}@Overridepublic Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {Set<Class<?>> results = new HashSet<>();for (Class<?> clazz : scanned) {if (clazz.getPackage().getName().contains("ws")) {results.add(clazz);}}return results;}
}
复制代码
其中getEndpointConfigs
是配置所有继承Endpoint
的类,而getAnnotatedEndpointClasses
是配置所有被@ServerEndpoint
修饰的类。
3、继承Endpoint
public class EchoEndpoint extends Endpoint {private static final AtomicLong count = new AtomicLong();@Overridepublic void onOpen(Session session, EndpointConfig endpointConfig) {System.out.println("在线人数:" + count.incrementAndGet());session.addMessageHandler(new EchoMessageHandlerText(session));}@Overridepublic void onClose(Session session, CloseReason closeReason) {System.out.println("在线人数:" + count.decrementAndGet());}private static class EchoMessageHandlerText implements MessageHandler.Partial<String> {private final Session session;private EchoMessageHandlerText(Session session) {this.session = session;}@Overridepublic void onMessage(String message, boolean last) {if (session == null)return;System.out.println(session.getId() + "\t" + message);try {session.getBasicRemote().sendText(message, last);} catch (IOException e) {e.printStackTrace();}}}
}
复制代码
4、使用注解
@ServerEndpoint("/websocket2")
public class EchoEndpoint2 {private static final AtomicLong count = new AtomicLong();@OnOpenpublic void open(Session session) {System.out.println("在线人数:" + count.incrementAndGet());try {session.getBasicRemote().sendText("welcome");} catch (IOException e) {e.printStackTrace();}}@OnClosepublic void close(Session session) {System.out.println("在线人数:" + count.decrementAndGet());}@OnMessagepublic void echoTextMessage(Session session, String message) {System.out.println(session.getId() + "\t" + message);try {if (session.isOpen()) {session.getBasicRemote().sendText(message);}} catch (IOException e) {e.printStackTrace();}}@OnMessagepublic void echoBinaryMessage(Session session, ByteBuffer bb) {System.out.println(session.getId() + "\t" + bb.toString());try {if (session.isOpen()) {session.getBasicRemote().sendBinary(bb);}} catch (IOException e) {e.printStackTrace();}}@OnMessagepublic void echoPongMessage(PongMessage pm) {//pass}
}
复制代码
例子就不给了,和昨天的几乎是一样的,你可以直接用昨天的那个html来测试今天的。
转载于:https://juejin.im/post/5bc6d5996fb9a05ce7518e89
Tomcat实现Web Socket相关推荐
- 基于Spring 4.0 的 Web Socket 聊天室/游戏服务端简单架构
跟webservice来相比,Web Socket可以做到保持长连接,或者说强连接,一直握手存在两端可以互相发送消息互相收到消息,而webservice是一次性的,你要我响应就必须要请求我一次(黄盖: ...
- Tomcat是什么:Tomcat与Java技、Tomcat与Web应用以及Tomcat基本框架及相关配置
1.Tomcat是什么 Apache Tomcat是由Apache Software Foundation(ASF)开发的一个开源Java WEB应用服务器. 类似功能的还有:Jetty ...
- Tomcat6.0.13下配置Tomcat Administration Web Application
Tomcat 5.5 以后的binary 核心安装版不再集成Tomcat Administration Web Application,需要独立下载安装.而Tomcat 6.0.13的Administ ...
- apr提高tomcat的web性能
apr提高tomcat的Web性能 Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术.APR(Apache Portable Runtime)是一个高可移植库,它是Apa ...
- web socket 心跳包的实现方案
web socket 心跳包的实现方案05/30/2010 现在网络环境错综复杂,socket心跳包是获得健康强壮的连接的有效解决方案,今天,我们就在web socket中实现心跳包方案,是的,尽管我 ...
- linux eclipse web插件,使用Tomcat插件开发WEB应用
在Eclipse中,可以安装Tomcat插件,实现WEB应用的开发调试工作,Tomcat插件还可以支持WEB应用的热部署. 一.安装配置Tomcat插件 可以通过拷贝安装和Links方式安装Tomca ...
- Tomcat 配置WEB虚拟映射 及 配置虚拟主机
Tomcat 配置WEB虚拟映射 及 配置虚拟主机 配置WEB虚拟映射文件夹有三种方法例如以下: 第一(要重新启动server的): 打开路径 Tomcat 6.0\conf 下的 server.x ...
- eclipse集成tomcat运行web时提示引入jar包的类找不到的解决办法
在eclipse集成tomcat开发web时,java类引入的jar包,编译通过,但启动tomcat运行web时提示找不到jar包内的类,需要作如下配置,将jar包在部署到集成的tomcat环境中. ...
- 源码解析 使用tomcat作为web容器时,用到的外观模式
源码解析 使用tomcat作为web容器时,接收浏览器发送过来的请求, tomcat会将请求信息封装成ServletRequest对象, 如下图①处对象. 但是大家想想ServletRequest是一 ...
- ubuntu下搭建eclipse+tomcat的web开发环境
之前是打算给eclipse装上插件变成eclipse for javaEE 的,后来一看过程太麻烦了,直接下载eclipse for java EE 算了.搭建eclipse+tomcat的web开发 ...
最新文章
- ASP.net 资源请求漏洞利用工具PadBuster
- 关于计算机英语素材,计算机专业英语相关素材.doc
- Codeforces 1491 D. Zookeeper and The Infinite Zoo (二进制处理)
- ajax异步通讯 遮罩滚动栏,防止并发及误操作
- javaI/O之PushbackInputStream
- minishell的实现及IO接口的调用
- Electron的学习笔记
- 信用评分卡(python)
- 京东自营厂直考试答案
- IOL2020中国初选第一题题解
- 华为交换机如何清除console口密码
- vue父子传值,slot插槽的使用
- 疯狂android讲义目录
- 基于springboot+jsp的服装穿搭信息管理系统
- ref使用之react / react hook
- QPS和TPS是什么
- Oauth2 中 access_token和refresh_token的过期时间
- Hbase的表查询操作
- 集群收益管理需要计算机能力,收益管理理论基础问题及发展研究.doc
- soot代码分析框架的基础知识(一)
热门文章
- Atitit图片复制父目录给你设计的实现 基于win 图片浏览器
- atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性
- Atitit.获取swing ui 按钮控件的id 与名字 与JPDA 调试体系
- paip.eclipse忽然启动报错的解决
- 远端异步调用事件结果
- rust: linker-link-exe-not-found
- 贝莱德COO:作为全球最大资产管理公司,为什么说我们还是成长中的科技公司?
- (转)FOF、MOM投资模式与金融科技应用展望
- 阿里云眼中的“云网络3.0”:构建应用、云、边一体网络
- php退款申请源码,PHP实现微信申请退款功能