Websocket
来源:互联网 发布:软件环境是什么 编辑:程序博客网 时间:2024/06/11 20:22
今天尝试使用websocket
(1)添加依赖
<dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version></dependency>
(2)编写一个类,用serverendpoint注解
@ServerEndpoint(value = "/echo")public class EchoEndpointAnnotated { @OnMessage public String onMessage(String message, Session session) { return message; }}
(3)加另一部分依赖
To use Tyrus in standalone mode it is necessary to depend on correct Tyrus artifacts. The following artifacts need to be added to your pom to use Tyrus:
<dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-server</artifactId> <version>1.12</version></dependency><dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-grizzly-server</artifactId> <version>1.12</version></dependency>
(4)启动server,把写好的类加进去启动
Let's use the very same example like for Java API for WebSocket and deploy the EchoEndpointAnnotated
on the standalone Tyrus server on the hostname "localhost", port 8025 and path "/websockets", so the endpoint will be available at address "ws://localhost:8025/websockets/echo".
public void runServer() { Server server = new Server("localhost", 8025, "/websockets", null, EchoEndpoint.class); try { server.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Please press a key to stop the server."); reader.readLine(); } catch (Exception e) { e.printStackTrace(); } finally { server.stop(); }}
阅读全文
0 0
- WebSocket
- WebSocket
- WebSocket
- websocket
- WebSocket
- WebSocket
- WebSocket
- WebSocket
- websocket
- websocket
- WebSocket
- WebSocket
- WebSocket
- WebSocket
- websocket
- websocket
- websocket
- websocket
- flask-cache 之缓存cache实现原理
- 程序员面试宝典随笔记(一)-----malloc和new分析
- 参数为数组类型时参数为空取全部数据写法
- 在Linux中如何开启VNC服务
- 运行PL/SQL时只输出anonymous block completed
- Websocket
- 佳能照片打印软件(My Image Garden) v3.6.1 官方版下载
- 解锁Retrofit -- 浅析Retrofit源码
- 树莓派下代码移植问题记录1 gcc failes to compile operator definition with prefixed namespace
- 数据结构实验之链表八:Farey序列
- 26 函数适配器binder2nd(学自Boolean)
- 错位排列-信封问题
- kylin从入门到实战:实际案例
- 四种子块匹配数字稳像算法比较