Tagged: Java EE

117

Java搭建WebSocket的两种方式

下面分别介绍搭建方法:一、直接使用Java EE的api进行搭建。一共3个步骤:1、添加依赖<dependency>    <groupId>javax</groupId>    <artifactId>javaee-api</artifactId>    <version>7.0</version>    <scope>provided</scope></dependency>2、使用注解@ServerEndpointimport javax.websocket.*;import javax.websocket.server.PathParam;import javax.websocket.server.ServerEndpoint;import java.io.IOException;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.logging.Logger; /*** WebSocket连接 sessionKey是url中的参数*/@ServerEndpoint(“/websocket/{sessionKey}”)public class WebSocket {     private static final Logger log = Logger.getLogger(WebSocket.class.getName());    ...