Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
6d2aeb46
S
SpringBoot-kwan
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringBoot-kwan
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot-kwan
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
6d2aeb46
编写于
8月 27, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:添加雪花算法
上级
39954353
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
89 addition
and
84 deletion
+89
-84
READ.md
READ.md
+2
-4
pom.xml
pom.xml
+5
-0
src/main/java/com/kwan/springbootkwan/config/WebSocketConfig.java
.../java/com/kwan/springbootkwan/config/WebSocketConfig.java
+2
-1
src/main/java/com/kwan/springbootkwan/utils/SnowflakeUtil.java
...ain/java/com/kwan/springbootkwan/utils/SnowflakeUtil.java
+54
-0
src/main/resources/templates/socket1.html
src/main/resources/templates/socket1.html
+0
-74
src/test/java/com/kwan/springbootkwan/SensitiveTest.java
src/test/java/com/kwan/springbootkwan/SensitiveTest.java
+1
-1
src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java
...est/java/com/kwan/springbootkwan/UserServiceImplTest.java
+1
-1
src/test/java/com/kwan/springbootkwan/controller/ChatbotControllerTest.java
...kwan/springbootkwan/controller/ChatbotControllerTest.java
+1
-1
src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java
...st/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java
+1
-1
src/test/java/com/kwan/springbootkwan/utils/SnowflakeUtilTest.java
...java/com/kwan/springbootkwan/utils/SnowflakeUtilTest.java
+21
-0
src/test/java/com/kwan/springbootkwan/utils/StringEncryptorUtil.java
...va/com/kwan/springbootkwan/utils/StringEncryptorUtil.java
+1
-1
未找到文件。
READ.md
浏览文件 @
6d2aeb46
...
@@ -18,7 +18,7 @@
...
@@ -18,7 +18,7 @@
-
devtools热部署
-
devtools热部署
-
mapstruct
dto-vo转换
-
mapstruct dto-vo转换
-
test测试模块
-
test测试模块
...
@@ -35,7 +35,6 @@
...
@@ -35,7 +35,6 @@
-
批处理
-
批处理
-
redis 全局唯一id
-
redis 全局唯一id
## 三.链接
## 三.链接
...
@@ -47,5 +46,4 @@ http://localhost:8761/user/all
...
@@ -47,5 +46,4 @@ http://localhost:8761/user/all
http://localhost:8761/swagger-ui.html
http://localhost:8761/swagger-ui.html
index.html 这个页面是进行websocket连接的
pom.xml
浏览文件 @
6d2aeb46
...
@@ -216,6 +216,11 @@
...
@@ -216,6 +216,11 @@
<artifactId>
jasypt-spring-boot-starter
</artifactId>
<artifactId>
jasypt-spring-boot-starter
</artifactId>
<version>
2.1.0
</version>
<version>
2.1.0
</version>
</dependency>
</dependency>
<dependency>
<groupId>
javax.websocket
</groupId>
<artifactId>
javax.websocket-api
</artifactId>
<version>
1.1
</version>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-websocket
</artifactId>
<artifactId>
spring-boot-starter-websocket
</artifactId>
...
...
src/main/java/com/kwan/springbootkwan/config/WebSocketConfig.java
浏览文件 @
6d2aeb46
...
@@ -2,6 +2,7 @@ package com.kwan.springbootkwan.config;
...
@@ -2,6 +2,7 @@ package com.kwan.springbootkwan.config;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.socket.config.annotation.EnableWebSocket
;
import
org.springframework.web.socket.server.standard.ServerEndpointExporter
;
import
org.springframework.web.socket.server.standard.ServerEndpointExporter
;
/**
/**
...
@@ -11,7 +12,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter;
...
@@ -11,7 +12,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter;
* @version : 2.2.0
* @version : 2.2.0
* @date : 2023/8/26 16:45
* @date : 2023/8/26 16:45
*/
*/
@EnableWebSocket
@Configuration
@Configuration
public
class
WebSocketConfig
{
public
class
WebSocketConfig
{
@Bean
@Bean
...
...
src/main/java/com/kwan/springbootkwan/utils/SnowflakeUtil.java
0 → 100644
浏览文件 @
6d2aeb46
package
com.kwan.springbootkwan.utils
;
public
class
SnowflakeUtil
{
private
static
final
long
EPOCH
=
1630444800000L
;
// 设置一个起始时间戳,例如:2021-09-01 00:00:00
private
static
final
long
MACHINE_ID_BITS
=
5
;
private
static
final
long
DATACENTER_ID_BITS
=
5
;
private
static
final
long
SEQUENCE_BITS
=
12
;
private
static
final
long
MAX_MACHINE_ID
=
(
1L
<<
MACHINE_ID_BITS
)
-
1
;
private
static
final
long
MAX_DATACENTER_ID
=
(
1L
<<
DATACENTER_ID_BITS
)
-
1
;
private
static
final
long
MAX_SEQUENCE
=
(
1L
<<
SEQUENCE_BITS
)
-
1
;
private
final
long
machineId
;
private
final
long
datacenterId
;
private
long
lastTimestamp
=
-
1L
;
private
long
sequence
=
0L
;
public
SnowflakeUtil
(
long
machineId
,
long
datacenterId
)
{
if
(
machineId
>
MAX_MACHINE_ID
||
machineId
<
0
)
{
throw
new
IllegalArgumentException
(
"Invalid machine ID"
);
}
if
(
datacenterId
>
MAX_DATACENTER_ID
||
datacenterId
<
0
)
{
throw
new
IllegalArgumentException
(
"Invalid datacenter ID"
);
}
this
.
machineId
=
machineId
;
this
.
datacenterId
=
datacenterId
;
}
public
synchronized
long
nextId
()
{
long
timestamp
=
System
.
currentTimeMillis
();
if
(
timestamp
<
lastTimestamp
)
{
throw
new
RuntimeException
(
"Clock moved backwards. Refusing to generate ID."
);
}
if
(
timestamp
==
lastTimestamp
)
{
sequence
=
(
sequence
+
1
)
&
MAX_SEQUENCE
;
if
(
sequence
==
0
)
{
timestamp
=
tilNextMillis
(
lastTimestamp
);
}
}
else
{
sequence
=
0
;
}
lastTimestamp
=
timestamp
;
return
((
timestamp
-
EPOCH
)
<<
(
MACHINE_ID_BITS
+
DATACENTER_ID_BITS
+
SEQUENCE_BITS
))
|
(
datacenterId
<<
(
MACHINE_ID_BITS
+
SEQUENCE_BITS
))
|
(
machineId
<<
SEQUENCE_BITS
)
|
sequence
;
}
private
long
tilNextMillis
(
long
lastTimestamp
)
{
long
timestamp
=
System
.
currentTimeMillis
();
while
(
timestamp
<=
lastTimestamp
)
{
timestamp
=
System
.
currentTimeMillis
();
}
return
timestamp
;
}
}
\ No newline at end of file
src/main/resources/templates/socket1.html
已删除
100644 → 0
浏览文件 @
39954353
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"utf-8"
>
<title>
Java后端WebSocket的Tomcat实现
</title>
<script
type=
"text/javascript"
src=
"js/jquery.min.js"
></script>
</head>
<body>
<div
id=
"main"
style=
"width: 1200px;height:800px;"
></div>
Welcome
<br/><input
id=
"text"
type=
"text"
/>
<button
onclick=
"send()"
>
发送消息
</button>
<hr/>
<button
onclick=
"closeWebSocket()"
>
关闭WebSocket连接
</button>
<hr/>
<div
id=
"message"
></div>
</body>
<script
type=
"text/javascript"
>
var
websocket
=
null
;
//判断当前浏览器是否支持WebSocket
if
(
'
WebSocket
'
in
window
)
{
//改成你的地址
websocket
=
new
WebSocket
(
"
ws://localhost:8888/api/websocket/100
"
);
}
else
{
alert
(
'
当前浏览器 Not support websocket
'
)
}
//连接发生错误的回调方法
websocket
.
onerror
=
function
()
{
setMessageInnerHTML
(
"
WebSocket连接发生错误
"
);
};
//连接成功建立的回调方法
websocket
.
onopen
=
function
()
{
setMessageInnerHTML
(
"
WebSocket连接成功
"
);
}
var
U01data
,
Uidata
,
Usdata
//接收到消息的回调方法
websocket
.
onmessage
=
function
(
event
)
{
console
.
log
(
event
);
setMessageInnerHTML
(
event
);
setechart
()
}
//连接关闭的回调方法
websocket
.
onclose
=
function
()
{
setMessageInnerHTML
(
"
WebSocket连接关闭
"
);
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window
.
onbeforeunload
=
function
()
{
closeWebSocket
();
}
//将消息显示在网页上
function
setMessageInnerHTML
(
innerHTML
)
{
document
.
getElementById
(
'
message
'
).
innerHTML
+=
innerHTML
+
'
<br/>
'
;
}
//关闭WebSocket连接
function
closeWebSocket
()
{
websocket
.
close
();
}
//发送消息
function
send
()
{
var
message
=
document
.
getElementById
(
'
text
'
).
value
;
websocket
.
send
(
'
{"msg":"
'
+
message
+
'
"}
'
);
setMessageInnerHTML
(
message
+
"
"
);
}
</script>
</html>
\ No newline at end of file
src/test/java/com/kwan/springbootkwan/SensitiveTest.java
浏览文件 @
6d2aeb46
...
@@ -9,7 +9,7 @@ import org.springframework.test.context.ContextConfiguration;
...
@@ -9,7 +9,7 @@ import org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
public
class
SensitiveTest
{
public
class
SensitiveTest
{
...
...
src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java
浏览文件 @
6d2aeb46
...
@@ -17,7 +17,7 @@ import java.util.concurrent.ExecutorService;
...
@@ -17,7 +17,7 @@ import java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Executors
;
@SpringBootTest
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
public
class
UserServiceImplTest
{
public
class
UserServiceImplTest
{
@Autowired
@Autowired
...
...
src/test/java/com/kwan/springbootkwan/controller/ChatbotControllerTest.java
浏览文件 @
6d2aeb46
...
@@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringRunner;
...
@@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith
(
SpringRunner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
class
ChatbotControllerTest
{
class
ChatbotControllerTest
{
...
...
src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java
浏览文件 @
6d2aeb46
...
@@ -8,7 +8,7 @@ import org.springframework.test.context.ContextConfiguration;
...
@@ -8,7 +8,7 @@ import org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
public
class
PinyinUtilTest
{
public
class
PinyinUtilTest
{
...
...
src/test/java/com/kwan/springbootkwan/utils/SnowflakeUtilTest.java
0 → 100644
浏览文件 @
6d2aeb46
package
com.kwan.springbootkwan.utils
;
import
com.kwan.springbootkwan.SpringBootKwanApplication
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
public
class
SnowflakeUtilTest
{
@Test
public
void
testName
()
{
// 传入机器ID和数据中心ID
SnowflakeUtil
snowflake
=
new
SnowflakeUtil
(
1
,
1
);
long
id
=
snowflake
.
nextId
();
System
.
out
.
println
(
"Generated ID: "
+
id
);
}
}
\ No newline at end of file
src/test/java/com/kwan/springbootkwan/utils/StringEncryptorUtil.java
浏览文件 @
6d2aeb46
...
@@ -19,7 +19,7 @@ import org.springframework.test.context.junit4.SpringRunner;
...
@@ -19,7 +19,7 @@ import org.springframework.test.context.junit4.SpringRunner;
* @date : 2023/8/26 15:49
* @date : 2023/8/26 15:49
*/
*/
@RunWith
(
SpringRunner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
public
class
StringEncryptorUtil
{
public
class
StringEncryptorUtil
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录