Skip to content

Commit 4980332

Browse files
committed
刷题吧程序
0 parents  commit 4980332

File tree

746 files changed

+63027
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

746 files changed

+63027
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
*.iml
3+
target
4+
/数据/

anonymous-group-chat/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.iml
3+
target

anonymous-group-chat/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 刷题吧匿名群聊
2+
3+
此应用是我在学习`WebSocket`的时候写出来的一个`匿名群聊`,头像和用户名都是`随机分配`,头像素材`来源于网络`
4+
5+
+ 请使用`谷歌浏览器`,如果遇到收不到消息的情况,`刷新`浏览器即可
6+
7+
+ 尽量不要使用`手机`访问,未做手机兼容
8+
9+
+ 每一个浏览器会`随机`分配一个`匿名身份``头像`,使用`Cookie`保存,如果想要换一个名字,清理一下`Cookie`即可
10+
11+
+ 当前只可以发送`文字`,不考虑加入其它功能
12+
13+
+ 聊天不会被`记录`
14+
15+
## 界面预览
16+
17+
![界面预览](http://p.qlogo.cn/qqmail_head/Q3auHgzwzM4g2cLj1J8wBePWc7IpPAic1rjFtR5gfwDLqQGpLicFibGuzLNCXIImDEypicB0a2Wch63wQp3dwbkbCpcKkfqFbSVtBzKdXh2jkW4/0)
18+
19+
20+
21+
> 2019-01-03 更新了不同用户链接WebSocket自动获取正确的链接

anonymous-group-chat/pom.xml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>question</artifactId>
7+
<groupId>com.cy</groupId>
8+
<version>1.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>anonymous-group-chat</artifactId>
13+
14+
<dependencies>
15+
<!--WebSocket支持-->
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-websocket</artifactId>
19+
</dependency>
20+
<!-- FastJSON依赖包 -->
21+
<dependency>
22+
<groupId>com.alibaba</groupId>
23+
<artifactId>fastjson</artifactId>
24+
<version>1.2.53</version>
25+
</dependency>
26+
<!--排除Tomcat依赖-->
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-tomcat</artifactId>
30+
<!--<scope>provided</scope>-->
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<finalName>GroupChat</finalName>
36+
<plugins>
37+
<!--可以使用Maven命令spring-boot:run来启动SpringBoot-->
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.cy;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.builder.SpringApplicationBuilder;
6+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7+
8+
/**
9+
* By CY
10+
* Date 2018/11/24 12:10
11+
*/
12+
@SpringBootApplication
13+
public class ChatApplication extends SpringBootServletInitializer {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(ChatApplication.class, args);
17+
}
18+
19+
/**
20+
* 部署到Tomcat需要这个方法
21+
*/
22+
@Override
23+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
24+
return application.sources(ChatApplication.class);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.cy.conf;
2+
3+
import org.springframework.boot.CommandLineRunner;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.core.annotation.Order;
6+
7+
/**
8+
* 这个类的作用是项目启动后自动打开浏览器访问项目
9+
* By CY
10+
* Date 2018/11/26 12:50
11+
*/
12+
@Configuration
13+
public class ApplicationInit implements CommandLineRunner {
14+
15+
/**
16+
* 运行项目后要进行的操作【已测试2018年11月27日23:03:49】
17+
*
18+
* @param args 参数
19+
* @throws Exception 异常
20+
*/
21+
@Order(0) // 运行的顺序,数字越小优先级越高
22+
@Override
23+
public void run(String... args) throws Exception {
24+
// 自动打开默认浏览器访问项目,当前只有Windows平台可用
25+
Runtime.getRuntime().exec("cmd /c start http://127.0.0.1:8080/");
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.cy.conf;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
6+
7+
@Configuration
8+
public class WebSocketConfig {
9+
10+
@Bean
11+
public ServerEndpointExporter serverEndpointExporter() {
12+
return new ServerEndpointExporter();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.cy.controller;
2+
3+
import com.cy.data.OnlineCount;
4+
import com.cy.service.RandomUserInfo;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import javax.servlet.http.HttpServletRequest;
10+
11+
/**
12+
* 得到用户信息的Controller
13+
*/
14+
@RestController
15+
@RequestMapping("chat")
16+
public class InfoController {
17+
18+
/**
19+
* 获取一个随机的用户名
20+
*
21+
* @param request IP
22+
* @return 用户的名 + IP
23+
* Todo 这种方式不恰当,因为前台可以通过修改Cookie的形式来进行IP的修改
24+
* 还可以自定义用户名,这里的设计思路需要改变
25+
*/
26+
@PostMapping(value = "username")
27+
public String getRandomUsername(HttpServletRequest request) {
28+
return RandomUserInfo.getRandomUserName() + " (" + request.getRemoteAddr() + ")";
29+
}
30+
31+
/**
32+
* 得到随机头像
33+
*
34+
* @return 随机头像的地址
35+
*/
36+
@PostMapping(value = "avatar")
37+
public String getRandomAvatar() {
38+
return RandomUserInfo.getRandomAvatar();
39+
}
40+
41+
/**
42+
* 得到在线的人数
43+
*
44+
* @return 在线的人数
45+
*/
46+
@PostMapping(value = "getOnlineCount")
47+
public synchronized int getOnlineCount() {
48+
return OnlineCount.getOnlineCount();
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.cy.data;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class OnlineCount {
7+
8+
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
9+
private static int onlineCount;
10+
11+
/**
12+
* 增加在线人数
13+
*/
14+
public static synchronized void addOnlineCount() {
15+
onlineCount++;
16+
}
17+
18+
/**
19+
* 减少在线人数
20+
*/
21+
public static synchronized void subOnlineCount() {
22+
onlineCount--;
23+
}
24+
25+
/**
26+
* 得到在线人数
27+
*
28+
* @return 在线人数
29+
*/
30+
public static synchronized int getOnlineCount() {
31+
return onlineCount;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.cy.service;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Date;
6+
import java.util.Properties;
7+
import java.util.Random;
8+
9+
/**
10+
* 得到随机的匿名用户信息
11+
* By CY
12+
* Date 2018/11/24 14:05
13+
*/
14+
public class RandomUserInfo {
15+
16+
/**
17+
* 头像属性文件的路径地址
18+
*/
19+
private static final String RANDOM_AVATAR_FILE_PATH = "properties/randomAvatar.properties";
20+
21+
/**
22+
* 随机用户名文件的路径地址
23+
*/
24+
private static final String RANDOM_USERNAME_FILE_PATH = "properties/randomUsername.properties";
25+
26+
/**
27+
* 得到随机的头像
28+
*/
29+
public static String getRandomAvatar() {
30+
// 读取匿名头像属性文件
31+
InputStream resourceAsStream = RandomUserInfo.class.getClassLoader().getResourceAsStream(RANDOM_AVATAR_FILE_PATH);
32+
return getRandomValue(resourceAsStream);
33+
}
34+
35+
/**
36+
* 得到随机的用户名
37+
*/
38+
public static String getRandomUserName() {
39+
// 读取匿名头像属性文件
40+
InputStream resourceAsStream = RandomUserInfo.class.getClassLoader().getResourceAsStream(RANDOM_USERNAME_FILE_PATH);
41+
return getRandomValue(resourceAsStream);
42+
}
43+
44+
/**
45+
* 得到随机值
46+
*
47+
* @param resourceAsStream 输入流
48+
* @return 返回一个随机值
49+
*/
50+
private static String getRandomValue(InputStream resourceAsStream) {
51+
Properties properties = new Properties();
52+
try {
53+
properties.load(resourceAsStream);
54+
resourceAsStream.close();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
int i = new Random(new Date().getTime()).nextInt(properties.size());
59+
return (String) properties.get(Integer.toString(i));
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.cy.websocket;
2+
3+
import com.cy.data.OnlineCount;
4+
import org.springframework.stereotype.Component;
5+
6+
import javax.websocket.*;
7+
import javax.websocket.server.ServerEndpoint;
8+
import java.io.IOException;
9+
import java.util.concurrent.CopyOnWriteArraySet;
10+
11+
/**
12+
* ServerEndpoint注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端,
13+
* 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端
14+
*/
15+
@Component
16+
@ServerEndpoint("/chat")
17+
public class ChatWebSocket {
18+
19+
/**
20+
* concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
21+
*/
22+
private static CopyOnWriteArraySet<ChatWebSocket> webSocketSet = new CopyOnWriteArraySet<>();
23+
24+
/**
25+
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
26+
*/
27+
private Session session;
28+
29+
/**
30+
* 连接建立成功调用的方法
31+
*
32+
* @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
33+
*/
34+
@OnOpen
35+
public void onOpen(Session session) {
36+
this.session = session;
37+
webSocketSet.add(this); //加入set中
38+
OnlineCount.addOnlineCount(); //在线数加1
39+
}
40+
41+
/**
42+
* 连接关闭调用的方法
43+
*/
44+
@OnClose
45+
public void onClose() {
46+
webSocketSet.remove(this); //从set中删除
47+
OnlineCount.subOnlineCount(); //在线数减1
48+
}
49+
50+
/**
51+
* 收到客户端消息后调用的方法
52+
*
53+
* @param message 客户端发送过来的消息
54+
*/
55+
@OnMessage
56+
public void onMessage(String message) {
57+
//群发消息
58+
for (ChatWebSocket item : webSocketSet) {
59+
try {
60+
if (item == this) {
61+
continue;
62+
}
63+
item.session.getBasicRemote().sendText(dealMassage(message));
64+
} catch (IOException e) {
65+
e.printStackTrace();
66+
}
67+
}
68+
}
69+
70+
/**
71+
* 发生错误时调用
72+
*/
73+
@OnError
74+
public void onError(Throwable error) {
75+
error.printStackTrace();
76+
}
77+
78+
/**
79+
* 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
80+
* 处理消息
81+
*/
82+
private String dealMassage(String message) {
83+
return message.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 设置日志级别
2+
logging.level.root=all
3+
# 端口号
4+
server.port=8080

0 commit comments

Comments
 (0)