Skip to content

Commit 7db9f11

Browse files
committed
添加代码示例springboot-socketio
1 parent 74757c1 commit 7db9f11

File tree

21 files changed

+5864
-2
lines changed

21 files changed

+5864
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Spring Boot 现在已经成为Java 开发领域的一颗璀璨明珠,它本身
1313

1414
## 子项目列表
1515

16-
每个项目一篇博客文章的详细讲解,点击链接可以查看文章
16+
每个项目一篇博客文章的详细讲解,点击链接可以查看文章 :point_right:
1717

1818
项目名称 | 文章地址
1919
----------------------------|------------------------------------------------------------------------------------------
@@ -40,7 +40,7 @@ springboot-batch | [批处理](https://www.xncoding.com/2017/08/01/sp
4040
springboot-rabbitmq | [集成消息队列RabbitMQ](https://www.xncoding.com/2017/08/06/spring/sb-rabbitmq.html)
4141
springboot-echarts | [集成Echarts导出图片](https://www.xncoding.com/2017/08/19/spring/sb-echarts.html)
4242
app-manage | [一个完整的Web后台管理系统,组合使用了多种技术]
43-
app-manage-api | [同时实现了需要认证授权访问的RESTful API接口和WebSocket接口]
43+
app-manage-api | [实现需要认证授权访问的RESTful API接口和WebSocket接口]
4444

4545
## 环境
4646

springboot-socketio/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 此为注释– 将被Git 忽略
2+
# /结尾表示是目录,忽略目录和目录下的所有件
3+
# /开头表示根目录,否则是.gitignore的相对目录
4+
# !开头表示反选
5+
.idea/
6+
target/
7+
*.iml
8+
*.ipr
9+
*.iws
10+
*.log
11+
.svn/
12+
.project
13+
rebel.xml
14+
.rebel-remote.xml.*
15+
swagger.json
16+
swagger.adoc
17+

springboot-socketio/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Xiong Neng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

springboot-socketio/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
## 简介
3+
4+
在SpringBoot中有两种实现WebSocket实时通信的方式:
5+
6+
1. 一个是使用WebSocket的一个子协议stomp
7+
2. 另外一个是使用Socket.IO协议实现。
8+
9+
本项目演示如何通过Socket.IO协议实现
10+
11+
## WeSocket长连接
12+
13+
Socket.IO服务器端和客户端,使用Java语言实现,用于和浏览器或Android客户端进行长连接的WebSocket通信。
14+
服务器端可以实时将消息推送给客户端,以此来取代客户端轮询机制。
15+
16+
Sockt.io官网:<https://socket.io/>
17+
18+
服务器端使用 [netty-socketio](https://github.com/mrniko/netty-socketio)
19+
20+
客户端使用 [socket.io-client-java](https://github.com/socketio/socket.io-client-java)
21+
22+
### WebSocket js客户端测试
23+
24+
```
25+
/client/html/index.html
26+
```
27+
28+
### WebSocket Java客户端测试
29+
30+
```
31+
com.xncoding.pos.socket.client.SocketClient
32+
```
33+
34+
## 许可证
35+
36+
Copyright (c) 2018 Xiong Neng
37+
38+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>
39+

springboot-socketio/pom.xml

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xncoding</groupId>
8+
<artifactId>springboot-socketio</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-socketio</name>
13+
<description>集成SocketIO实时通信</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>1.5.9.RELEASE</version>
19+
<relativePath/>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
<netty.version>4.1.19.Final</netty.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter</artifactId>
33+
</dependency>
34+
<!-- netty-socketio-->
35+
<dependency>
36+
<groupId>com.corundumstudio.socketio</groupId>
37+
<artifactId>netty-socketio</artifactId>
38+
<version>1.7.13</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>io.netty</groupId>
42+
<artifactId>netty-buffer</artifactId>
43+
<version>${netty.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.netty</groupId>
47+
<artifactId>netty-codec</artifactId>
48+
<version>${netty.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.netty</groupId>
52+
<artifactId>netty-codec-http</artifactId>
53+
<version>${netty.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.netty</groupId>
57+
<artifactId>netty-common</artifactId>
58+
<version>${netty.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.netty</groupId>
62+
<artifactId>netty-handler</artifactId>
63+
<version>${netty.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.netty</groupId>
67+
<artifactId>netty-resolver</artifactId>
68+
<version>${netty.version}</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>io.netty</groupId>
72+
<artifactId>netty-transport</artifactId>
73+
<version>${netty.version}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>io.netty</groupId>
77+
<artifactId>netty-transport-native-epoll</artifactId>
78+
<version>${netty.version}</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>io.netty</groupId>
82+
<artifactId>netty-transport-native-unix-common</artifactId>
83+
<version>${netty.version}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>io.socket</groupId>
87+
<artifactId>socket.io-client</artifactId>
88+
<version>1.0.0</version>
89+
</dependency>
90+
</dependencies>
91+
92+
<build>
93+
<plugins>
94+
<plugin>
95+
<groupId>org.apache.maven.plugins</groupId>
96+
<artifactId>maven-compiler-plugin</artifactId>
97+
<version>3.6.1</version>
98+
<configuration>
99+
<!--<proc>none</proc>-->
100+
<source>1.8</source>
101+
<target>1.8</target>
102+
</configuration>
103+
</plugin>
104+
<plugin>
105+
<groupId>org.apache.maven.plugins</groupId>
106+
<artifactId>maven-surefire-plugin</artifactId>
107+
<version>2.20</version>
108+
<configuration>
109+
<systemPropertyVariables>
110+
<swaggerOutputDir>${project.basedir}/src/main/resources/swagger</swaggerOutputDir>
111+
<asciiDocOutputDir>${project.basedir}/src/main/resources/swagger/swagger</asciiDocOutputDir>
112+
</systemPropertyVariables>
113+
<skip>true</skip>
114+
</configuration>
115+
</plugin>
116+
<plugin>
117+
<groupId>org.springframework.boot</groupId>
118+
<artifactId>spring-boot-maven-plugin</artifactId>
119+
<executions>
120+
</executions>
121+
</plugin>
122+
</plugins>
123+
124+
<resources>
125+
<resource>
126+
<directory>src/main/resources</directory>
127+
</resource>
128+
<resource>
129+
<directory>src/main/java</directory>
130+
<includes>
131+
<include>**/*.xml</include>
132+
</includes>
133+
</resource>
134+
</resources>
135+
</build>
136+
137+
</project>

springboot-socketio/run.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# 项目自动更新脚本
3+
# 先clone相应的分支下来:
4+
# git clone ssh://[email protected]:7999/xxx.git
5+
# 远程调试启动:
6+
# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
7+
8+
function start {
9+
profile="$1"
10+
echo "启动环境profile=${profile}"
11+
jarfile=$(ls target/*.jar)
12+
if [[ "$?" == "0" ]]; then
13+
stop $profile $jarfile
14+
fi
15+
branch=$(git branch |awk '{print $2}')
16+
git pull origin ${branch}
17+
echo "更新完代码开始重新打包"
18+
mvn clean && mvn clean && mvn package -DskipTests=true
19+
if [[ "$?" != "0" ]]; then
20+
echo "编译出错,退出!"
21+
exit 1
22+
fi
23+
echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &"
24+
nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
25+
echo "启动应用中,请查看日志文件..."
26+
}
27+
28+
function stop {
29+
profile="$1"
30+
jarfile="$2"
31+
ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null
32+
if [[ "$?" == "0" ]]; then
33+
echo "该应用还在跑,我先停了它"
34+
pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}')
35+
if [[ "$pid" != "" ]]; then
36+
kill -9 $pid
37+
fi
38+
echo "停止应用成功..."
39+
fi
40+
}
41+
42+
if [[ "$1" == "start" ]]; then
43+
if [[ "$#" < 2 ]]; then
44+
echo "请输入正确参数:./epay.sh start {profile}"
45+
exit 1
46+
fi
47+
profile="$2"
48+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
49+
echo "参数错误,请输入正确的profile参数,使用方法:"
50+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
51+
exit 1
52+
fi
53+
start "${profile}"
54+
elif [[ "$1" == "stop" ]]; then
55+
if [[ "$#" < 2 ]]; then
56+
echo "请输入正确参数:./epay.sh stop {profile}"
57+
exit 1
58+
fi
59+
profile="$2"
60+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
61+
echo "参数错误,请输入正确的profile参数,使用方法:"
62+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
63+
exit 1
64+
fi
65+
jarfile=$(ls target/*.jar)
66+
stop $profile $jarfile
67+
else
68+
echo "参数错误,使用方法:{}参数是必填的,[]参数可选"
69+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
70+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
71+
exit 1
72+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xncoding.jwt;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
12+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.xncoding.jwt.common;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
9+
import java.io.IOException;
10+
11+
/**
12+
* 对象和Json转换器
13+
*
14+
* @author XiongNeng
15+
* @version 1.0
16+
* @since 2018/1/18
17+
*/
18+
public class JsonConverter {
19+
public static JSONObject objectToJSONObject(Object object) {
20+
try {
21+
String jsonString = new ObjectMapper().writeValueAsString(object);
22+
return new JSONObject(jsonString);
23+
} catch (JSONException | JsonProcessingException e) {
24+
e.printStackTrace();
25+
return null;
26+
}
27+
}
28+
29+
public static JSONArray objectToJSONArray(Object object) {
30+
try {
31+
String jsonString = new ObjectMapper().writeValueAsString(object);
32+
return new JSONArray(jsonString);
33+
} catch (JSONException | JsonProcessingException e) {
34+
e.printStackTrace();
35+
return null;
36+
}
37+
}
38+
39+
public static <T> T jsonObjectToObject(Object jsonObject, Class<T> clazz) {
40+
try {
41+
// List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>(){});
42+
return new ObjectMapper().readValue(jsonObject.toString(), clazz);
43+
} catch (IOException e) {
44+
return null;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)