Websocket
websocket主要可以分为服务器端和客户端两部分,其中服务器端又可以分为配置和服务
需要的依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!--以下两项需要如果不配置,解析themleaft 会有问题--> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>runtime</scope> </dependency> <!--添加对websocket的支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <!--添加对html的支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>1.5.6.RELEASE</version> <optional>true</optional> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <imageName>suyu/${project.artifactId}:${project.version}</imageName> <baseImage>java:8</baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
后端配置
1 import org.springframework.context.annotation.Bean; 2 import org.springframework.context.annotation.Configuration; 3 import org.springframework.web.socket.server.standard.ServerEndpointExporter; 4 5 @Configuration 6 public class WebSocketConfig { 7 @Bean 8 public ServerEndpointExporter serverEndpointExporter(){ 9 return new ServerEndpointExporter(); 10 } 11 }
后端服务
1 import org.springframework.stereotype.Component; 2 3 import javax.websocket.OnClose; 4 import javax.websocket.OnMessage; 5 import javax.websocket.OnOpen; 6 import javax.websocket.Session; 7 import javax.websocket.server.ServerEndpoint; 8 import java.util.concurrent.CopyOnWriteArraySet; 9 10 @Component 11 @ServerEndpoint("/webSocket") 12 //@Slf4j 13 public class WebSocket { 14 private Session session; 15 private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>(); 16 17 @OnOpen 18 public void onOpen(Session session) { 19 this.session = session; 20 webSocketSet.add(this); 21 System.out.println("【websocket消息】:打开连接" + webSocketSet.size()); 22 } 23 24 @OnClose 25 public void OnClose() { 26 webSocketSet.remove(this); 27 System.out.println("【websocket消息】:关闭连接" + webSocketSet.size()); 28 } 29 30 @OnMessage 31 public void OnMessage(String message) { 32 System.out.println("【websocket消息】:接收到的消息" + message); 33 } 34 35 public void sendMessage(String message) { 36 for (WebSocket webSocket : webSocketSet) { 37 System.out.println("【websocket消息】:广播的消息" + message); 38 try { 39 webSocket.session.getBasicRemote().sendText(message); 40 } catch (Exception e) { 41 e.printStackTrace(); 42 } 43 } 44 } 45 46 }
前端界面
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>WebSocket测试</title> 5 </head> 6 <body> 7 <h1>WebSocket测试</h1> 8 </body> 9 <script type="text/javascript"> 10 var websocket = null; 11 if (!!window.WebSocket && window.WebSocket.prototype.send) { 12 console.log("当前浏览器支持websocket"); 13 websocket=new WebSocket('ws://127.0.0.1:8086/webSocket'); 14 }else { 15 console.log("当前浏览器不支持websocket"); 16 } 17 websocket.onopen=function (evnet) { 18 console.log("建立连接"); 19 } 20 websocket.onclose=function (event) { 21 console.log("关闭连接"); 22 } 23 websocket.onmessage=function (event) { 24 console.log("收到消息:"+event.data); 25 } 26 websocket.onerror=function (event){ 27 console.log("连接错误"); 28 } 29 30 </script> 31 </html>
定时任务
使用springboot添加定时任务向前端发送信息
1 import com.zhang.websocket.server.WebSocket; 2 import org.springframework.scheduling.annotation.Scheduled; 3 import org.springframework.stereotype.Component; 4 5 6 @Component 7 public class scheduled { 8 WebSocket webSocket=new WebSocket(); 9 @Scheduled(fixedRate = 1000) 10 public void pushDataScheduled(){ 11 webSocket.sendMessage("你好啊"); 12 } 13 }
注:添加定时任务需要在spring启动类前加入注解
@EnableScheduling//定时任务
效果图