Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/chat page #234

Merged
merged 3 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# dump
dump.*
dump.*
package-lock.json
node_modules
2 changes: 1 addition & 1 deletion chat-server/src/common/schemas/chat.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Chat {
@Prop()
content: string;

@Prop({ type: Date })
@Prop({ default: new Date() })
createdAt: Date;
}

Expand Down
16 changes: 11 additions & 5 deletions chat-server/src/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { SocketService } from './socket.service';
@WebSocketGateway({
namespace: 'chat',
cors: {
origin: ['http://localhost:3000'],
origin: '*',
credential: true,
},
})
export class SocketGateway implements OnGatewayDisconnect {
Expand All @@ -27,9 +28,9 @@ export class SocketGateway implements OnGatewayDisconnect {
@ConnectedSocket() socket: Socket,
): Promise<void> {
const { recruitId } = data;
socket.join(recruitId); // room에 입장
socket.join(recruitId.toString()); // room에 입장
await this.socketService.setCacheData(socket.id, data);
const recentMsg = await this.socketService.getRecentMessage();
const recentMsg = await this.socketService.getRecentMessage(recruitId);
socket.emit('server_sent_recent', recentMsg);
}

Expand All @@ -46,11 +47,16 @@ export class SocketGateway implements OnGatewayDisconnect {
chat.sender = userId;
chat.recruitId = recruitId;
chat.content = content;
await this.socketService.saveRecentMessage(chat);
this.server.emit('server_sent', chat);
chat.createdAt = new Date();
this.server
.in(recruitId.toString())
.emit('server_sent', await this.socketService.saveRecentMessage(chat));
}

async handleDisconnect(@ConnectedSocket() socket: Socket): Promise<void> {
await this.socketService.delCacheData(socket.id);
}
async handleConnection(@ConnectedSocket() socket: Socket): Promise<void> {
await this.socketService.delCacheData(socket.id);
}
}
18 changes: 13 additions & 5 deletions chat-server/src/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ export class SocketService {
return this.cacheManager.del(`id:${socketId}`);
}

async getRecentMessage() {
async getRecentMessage(recruitId: number) {
const response = await this.chatModel
.find()
.sort({ createdAt: -1 })
.limit(10);
.find({ recruitId })
.sort({ createdAt: -1 });
// .limit(10);
return response.reverse();
}

async saveRecentMessage(chatEntity: Chat) {
this.chatModel.create(chatEntity);
return this.chatModel.create(chatEntity);
}

async getLatestMessage(recruitId: number) {
const response = await this.chatModel
.find({ recruitId })
.sort({ createdAt: -1 })
.limit(1);
return response[0];
}
}
135 changes: 135 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"react-slick": "^0.29.0",
"react-spinners": "^0.13.7",
"recoil": "^0.7.6",
"socket.io-client": "^4.5.4",
"styled-components": "^5.3.6",
"web-vitals": "^2.1.4"
},
Expand Down
43 changes: 20 additions & 23 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import { Route, Routes } from "react-router-dom";
import useRefreshQuery from "#hooks/queries/useRefreshQuery";
import Layout from "#components/Layout/Layout";
import { Suspense } from "react";
import Loading from "#components/commons/Loading/Loading";
import * as P from "#pages/index";

function App() {
useRefreshQuery();
return (
<Layout>
<Suspense fallback={<Loading />}>
<Routes>
<Route path="/" element={<P.MainPage />} />
<Route path="me" element={<P.MyPage />} />
<Route path="signup" element={<P.SignUp />} />
<Route path="login" element={<P.Login />} />
<Route path="courses" element={<P.Courses />} />
<Route path="recruits" element={<P.Recruits />} />
<Route path="course">
<Route path="new" element={<P.NewCourse />} />
<Route path=":id" element={<P.CourseDetail />} />
</Route>
<Route path="recruit">
<Route path=":id" element={<P.RecruitDetail />} />
</Route>
<Route path="mock">
<Route path="courses" element={<P.MockCourses />} />
<Route path="recruits" element={<P.MockRecruits />} />
</Route>
</Routes>
</Suspense>
<Routes>
<Route path="/" element={<P.MainPage />} />
<Route path="me" element={<P.MyPage />} />
<Route path="signup" element={<P.SignUp />} />
<Route path="login" element={<P.Login />} />
<Route path="courses" element={<P.Courses />} />
<Route path="recruits" element={<P.Recruits />} />
<Route path="course">
<Route path="new" element={<P.NewCourse />} />
<Route path=":id" element={<P.CourseDetail />} />
</Route>
<Route path="recruit">
<Route path=":id" element={<P.RecruitDetail />} />
<Route path=":id/chat" element={<P.ChatPage />} />
</Route>
<Route path="mock">
<Route path="courses" element={<P.MockCourses />} />
<Route path="recruits" element={<P.MockRecruits />} />
</Route>
</Routes>
</Layout>
);
}
Expand Down
3 changes: 3 additions & 0 deletions client/src/assets/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import CLOCK_ICON from "./clock_icon.svg";
import START_ICON from "./start_icon.svg";
import ARRIVE_ICON from "./arrive_icon.svg";
import MY_POSITION_ICON from "./my_position_icon.svg";
import SEND_ICON from "./send_icon.svg";

export {
USER_CIRCLE_ICON,
ARROW_LEFT_ICON,
Expand All @@ -39,4 +41,5 @@ export {
START_ICON,
ARRIVE_ICON,
MY_POSITION_ICON,
SEND_ICON,
};
9 changes: 9 additions & 0 deletions client/src/assets/icons/send_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading