一、项目介绍
一个基于SSM+Websocket的实时在线聊天室项目,主要功能如下
1. 用户注册登录:用户可以通过注册账号并登录系统,以便参与在线聊天。
2. 实时聊天:用户可以在房间中实时发送文本消息,与当前在线的其他用户进行实时聊天。
3. 聊天记录:系统会自动记录聊天内容,用户可以查看聊天历史记录。
3. 发送图片和文件:用户可以发送图片和文件给其他用户,进行分享和交流。
4. 在线用户列表:用户可以查看当前房间中的在线用户列表,方便与其他用户进行交流。
5. 个人资料设置:用户可以编辑个人资料,包括昵称、头像等。
6. 消息提醒:系统会提醒用户有新消息到达,保证用户及时获取聊天信息。
二、项目技术
编程语言:Java
数据库:MySQL
前端技术:JSP、Websocket
后端技术:Spring、SpringMVC、MyBatis
三、运行环境
操作系统:Windows或者macOS
JDK版本:最好是JDK1.8,其他版本理论上也可以
开发工具:IDEA、Ecplise、Myecplise都可以
数据库: MySQL5.5/5.7/8.0版本都可以
Tomcat:7.0及以上版本都可以
Maven:查看源码根目录中是否包含pom.xml文件,若包含,则为maven项目,需要配置Maven,否则为非maven项目,不用配置Maven
四、运行截图
五、主要功能代码实现
1. 用户注册登录
用户可以注册并登录系统,系统验证用户身份。
代码:
import java.io.*;
import java.net.*;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ChatServer {
private static final int PORT = 12345;
private static Set<String> registeredUsers = new HashSet<>();
private static Set<ClientHandler> clients = new HashSet<>();
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Chat server started...");
while (true) {
Socket clientSocket = serverSocket.accept();
new ClientHandler(clientSocket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class ClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private String userName;
public ClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
// User registration and login
String command = in.readLine();
if (command.startsWith("REGISTER")) {
String[] parts = command.split(" ");
String newUser = parts[1];
registeredUsers.add(newUser);
out.println("Registration successful.");
} else if (command.startsWith("LOGIN")) {
String[] parts = command.split(" ");
userName = parts[1];
if (registeredUsers.contains(userName)) {
synchronized (clients) {
clients.add(this);
}
out.println("Login successful.");
broadcastMessage(userName + " has joined the chat.");
} else {
out.println("User not registered.");
return;
}
}
String message;
while ((message = in.readLine()) != null) {
if (message.equalsIgnoreCase("exit")) {
break;
}
broadcastMessage(userName + ": " + message);
}
broadcastMessage(userName + " has left the chat.");
} catch (IOException e) {
e.printStackTrace();
} finally {
synchronized (clients) {
clients.remove(this);
}
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void broadcastMessage(String message) {
synchronized (clients) {
for (ClientHandler client : clients) {
client.out.println(message);
}
}
}
}
}
2. 实时聊天
用户可以实时发送和接收文本消息。
代码:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ChatClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 12345;
public static void main(String[] args) {
try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter command (REGISTER <username> / LOGIN <username>): ");
String command = scanner.nextLine();
out.println(command);
String response = in.readLine();
System.out.println(response);
if (response.contains("successful")) {
Thread messageReceiver = new Thread(() -> {
try {
String serverMessage;
while ((serverMessage = in.readLine()) != null) {
System.out.println(serverMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
});
messageReceiver.start();
System.out.println("You can start chatting now:");
while (scanner.hasNextLine()) {
String userMessage = scanner.nextLine();
out.println(userMessage);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 聊天记录
系统记录聊天内容并允许用户查看历史记录。
代码:
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
private static final int PORT = 12345;
private static Set<ClientHandler> clients = new HashSet<>();
private static List<String> chatHistory = new ArrayList<>();
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Chat server started...");
while (true) {
Socket clientSocket = serverSocket.accept();
new ClientHandler(clientSocket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class ClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private String userName;
public ClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
String command = in.readLine();
String[] parts = command.split(" ");
userName = parts[1];
synchronized (clients) {
clients.add(this);
}
out.println("Chat history:");
for (String history : chatHistory) {
out.println(history);
}
broadcastMessage(userName + " has joined the chat.");
String message;
while ((message = in.readLine()) != null) {
if (message.equalsIgnoreCase("exit")) {
break;
}
chatHistory.add(userName + ": " + message);
broadcastMessage(userName + ": " + message);
}
broadcastMessage(userName + " has left the chat.");
} catch (IOException e) {
e.printStackTrace();
} finally {
synchronized (clients) {
clients.remove(this);
}
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void broadcastMessage(String message) {
synchronized (clients) {
for (ClientHandler client : clients) {
client.out.println(message);
}
}
}
}
}
4. 发送图片和文件
用户可以发送图片和文件给其他用户进行分享和交流。
代码:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ChatClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 12345;
public static void main(String[] args) {
try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter command (REGISTER <username> / LOGIN <username>): ");
String command = scanner.nextLine();
out.println(command);
String response = in.readLine();
System.out.println(response);
if (response.contains("successful")) {
Thread messageReceiver = new Thread(() -> {
try {
String serverMessage;
while ((serverMessage = in.readLine()) != null) {
System.out.println(serverMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
});
messageReceiver.start();
System.out.println("You can start chatting now:");
while (scanner.hasNextLine()) {
String userMessage = scanner.nextLine();
if (userMessage.startsWith("sendfile")) {
String[] parts = userMessage.split(" ");
String fileName = parts[1];
sendFile(fileName, socket);
} else {
out.println(userMessage);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void sendFile(String fileName, Socket socket) {
try (FileInputStream fileInputStream = new FileInputStream(fileName);
OutputStream outputStream = socket.getOutputStream()) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
System.out.println("File sent.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
5. 在线用户列表
用户可以查看当前房间中的在线用户列表。
代码:
import java.io.*;
import java.net.*;
import java.util.HashSet;
import java.util.Set;
public class ChatServer {
private static final int PORT = 12345;
private static Set<ClientHandler> clients = new HashSet<>();
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Chat server started...");
while (true) {
Socket clientSocket = serverSocket.accept();
new ClientHandler(clientSocket).start();
}