增加h5websocket样例
This commit is contained in:
parent
190c8ef703
commit
0e89e429b5
56
h5websocket/pom.xml
Normal file
56
h5websocket/pom.xml
Normal file
@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.fxb</groupId>
|
||||
<artifactId>h5websocket</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>h5websocket</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.4.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.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-websocket</artifactId>
|
||||
<version>1.5.3.RELEASE</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,15 @@
|
||||
package com.fxb.h5websocket;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
public class H5websocketApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(H5websocketApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.fxb.h5websocket;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
* Description:
|
||||
* User: ${fxb}
|
||||
* Email: fanxb.tl@gmail.com
|
||||
* Date: 2018-08-01
|
||||
*/
|
||||
@RestController
|
||||
public class HomeController {
|
||||
|
||||
@GetMapping("/broadcast")
|
||||
public void broadcast(){
|
||||
MyWebSocket.broadcast();
|
||||
}
|
||||
}
|
100
h5websocket/src/main/java/com/fxb/h5websocket/MyWebSocket.java
Normal file
100
h5websocket/src/main/java/com/fxb/h5websocket/MyWebSocket.java
Normal file
@ -0,0 +1,100 @@
|
||||
package com.fxb.h5websocket;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
* Description:
|
||||
* User: ${fxb}
|
||||
* Email: fanxb.tl@gmail.com
|
||||
* Date: 2018-08-01
|
||||
*/
|
||||
@ServerEndpoint(value = "/websocket") //接受websocket请求路径
|
||||
@Component
|
||||
public class MyWebSocket {
|
||||
|
||||
|
||||
//保存所有在线socket连接
|
||||
private static Map<String,MyWebSocket> webSocketMap = new LinkedHashMap<>();
|
||||
|
||||
//记录当前在线数目
|
||||
private static int count=0;
|
||||
|
||||
//当前连接(每个websocket连入都会创建一个MyWebSocket实例
|
||||
private Session session;
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
//处理连接建立
|
||||
@OnOpen
|
||||
public void onOpen(Session session){
|
||||
this.session=session;
|
||||
webSocketMap.put(session.getId(),this);
|
||||
addCount();
|
||||
log.info("新的连接加入:{}",session.getId());
|
||||
}
|
||||
|
||||
//接受消息
|
||||
@OnMessage
|
||||
public void onMessage(String message,Session session){
|
||||
log.info("收到客户端{}消息:{}",session.getId(),message);
|
||||
try{
|
||||
this.sendMessage("收到消息:"+message);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//处理错误
|
||||
@OnError
|
||||
public void onError(Throwable error,Session session){
|
||||
log.info("发生错误{},{}",session.getId(),error.getMessage());
|
||||
}
|
||||
|
||||
//处理连接关闭
|
||||
@OnClose
|
||||
public void onClose(){
|
||||
webSocketMap.remove(this.session.getId());
|
||||
reduceCount();
|
||||
log.info("连接关闭:{}",this.session.getId());
|
||||
}
|
||||
|
||||
//群发消息
|
||||
|
||||
//发送消息
|
||||
public void sendMessage(String message) throws IOException {
|
||||
this.session.getBasicRemote().sendText(message);
|
||||
}
|
||||
|
||||
//广播消息
|
||||
public static void broadcast(){
|
||||
MyWebSocket.webSocketMap.forEach((k,v)->{
|
||||
try{
|
||||
v.sendMessage("这是一条测试广播");
|
||||
}catch (Exception e){
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//获取在线连接数目
|
||||
public static int getCount(){
|
||||
return count;
|
||||
}
|
||||
|
||||
//操作count,使用synchronized确保线程安全
|
||||
public static synchronized void addCount(){
|
||||
MyWebSocket.count++;
|
||||
}
|
||||
|
||||
public static synchronized void reduceCount(){
|
||||
MyWebSocket.count--;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.fxb.h5websocket.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
* Description:
|
||||
* User: ${fxb}
|
||||
* Email: fanxb.tl@gmail.com
|
||||
* Date: 2018-08-01
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter(){
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.fxb.h5websocket;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class H5websocketApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
34
h5websocket/test.html
Normal file
34
h5websocket/test.html
Normal file
@ -0,0 +1,34 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>websocket测试</title>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<button onclick="sendMessage()">测试</button>
|
||||
<script>
|
||||
let socket = new WebSocket("ws://localhost:8080/websocket");
|
||||
socket.onerror = err => {
|
||||
console.log(err);
|
||||
};
|
||||
socket.onopen = event => {
|
||||
console.log(event);
|
||||
};
|
||||
socket.onmessage = mess => {
|
||||
console.log(mess);
|
||||
};
|
||||
socket.onclose = () => {
|
||||
console.log("连接关闭");
|
||||
};
|
||||
|
||||
function sendMessage() {
|
||||
if (socket.readyState === 1)
|
||||
socket.send("这是一个测试数据");
|
||||
else
|
||||
alert("尚未建立websocket连接");
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user