Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

#30 웹소켓을 이용한 1:1 채팅 구현 #32

Open
wants to merge 25 commits into
base: feature/29
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f455586
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 24, 2020
5fabb1d
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 24, 2020
bf32376
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 25, 2020
4daebd0
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 25, 2020
c19f1ba
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 25, 2020
4e73acc
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 27, 2020
cfdde06
Merge branch 'develop' into feature/30
junshock5 Aug 27, 2020
88e212c
Merge branch 'feature/29' into feature/30
junshock5 Aug 27, 2020
18c4ad4
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 28, 2020
f56371c
Merge branch 'feature/30' of https://github.com/f-lab-edu/used-market…
junshock5 Aug 28, 2020
fc9f045
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 31, 2020
bd8c3b2
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Aug 31, 2020
5a4efeb
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Sep 1, 2020
c5a76b7
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Sep 1, 2020
6e23408
attachFile 잘못올라간거 삭제
junshock5 Sep 1, 2020
f4f1fad
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Sep 3, 2020
8d887b5
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Sep 3, 2020
5fb8d10
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Sep 3, 2020
7d4b6ca
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Sep 6, 2020
825152d
Merge branch 'feature/29' into feature/30
junshock5 Sep 8, 2020
e4e31df
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Sep 12, 2020
f1d226c
Merge branch 'feature/30' of https://github.com/f-lab-edu/used-market…
junshock5 Sep 12, 2020
05eba04
캐싱 만료시간 추가 , DateUtil 필요없는 로그 삭제
junshock5 Sep 12, 2020
fdb1bf9
Merge branch 'feature/29' into feature/30
junshock5 Sep 12, 2020
9717040
#30 웹소켓을 이용한 1:1 채팅 구현
junshock5 Sep 15, 2020
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
40 changes: 7 additions & 33 deletions src/main/java/com/market/server/controller/ChattingController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package com.market.server.controller;

import com.market.server.dto.RoomDTO;
import com.market.server.service.ChattingService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -22,8 +15,6 @@
@Log4j2
public class ChattingController {

static int roomNumber = 0;
private ChattingResponse chattingResponse;
private final ChattingService chattingService;

@Autowired
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자주입시에 이 어노테이션이 필요할까요?

Copy link
Collaborator Author

@junshock5 junshock5 Sep 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자 주입시 단일 생성자(생성자가 2개 미만)인 경우에는 @Autowired 어노테이션 붙이지 않아도 주입됩니다.

Expand Down Expand Up @@ -51,10 +42,11 @@ public ModelAndView room() {
*/
@PostMapping("/rooms")
public @ResponseBody
List<RoomDTO> createRoom(@RequestParam HashMap<Object, Object> params, ChattingRequest chattingRequest) {
List<RoomDTO> createRoom(@RequestParam HashMap<String, String> params) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

유지보수성 측면에서 Map을 쓰는게 좋을지 고민해주세요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

유지보수성 측면에서 코드 가 명시적으로 보이기 위해
파라미터를 DTO형으로 변경하였습니다.

String roomName = (String) params.get("roomName");
RoomDTO roomDTO = null;
if (roomName != null && !roomName.trim().equals("")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roomName이 null이면 NPE가 날 것 같네요

int roomNumber = chattingService.getLastRoomNumber();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 동시성문제가 있어보이네요. 이 메소드는 호출할때마다 다른값을 반환하나요? 혹은 같은 값을 반환할수도 있나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoomDTO가 정상적으로 등록되지 않았다면 같은값을 반환할 수도 있습니다.

roomDTO = RoomDTO.builder()
.roomNumber(++roomNumber)
.roomName(roomName)
Expand All @@ -73,7 +65,7 @@ List<RoomDTO> createRoom(@RequestParam HashMap<Object, Object> params, ChattingR
*/
@GetMapping("/rooms")
public @ResponseBody
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ResponseBody는 보통 메소드 위에 선언합니다. 이것도 컨벤션을 지키지 않은 것 같네요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변경하겠습니다.

List<RoomDTO> getRoom(@RequestParam HashMap<Object, Object> params, @RequestBody RoomDTO roomDTO) {
List<RoomDTO> getRoom(@RequestBody RoomDTO roomDTO) {
return chattingService.getRooms(roomDTO);
}

Expand All @@ -83,37 +75,19 @@ List<RoomDTO> getRoom(@RequestParam HashMap<Object, Object> params, @RequestBody
* @return
*/
@GetMapping("/chatting")
public ModelAndView moveRoom(@RequestParam HashMap<Object, Object> params) {
public ModelAndView moveRoom(@RequestParam HashMap<String, String> params) {
ModelAndView mv = new ModelAndView();
int roomNumber = Integer.parseInt((String) params.get("roomNumber"));

List<RoomDTO> new_list = chattingService.getRooms(null).stream().filter(o->o.getRoomNumber()==roomNumber).collect(Collectors.toList());
if(new_list != null && new_list.size() > 0) {
List<RoomDTO> new_list = chattingService.getRooms(null).stream().filter(o -> o.getRoomNumber() == roomNumber).collect(Collectors.toList());
if (new_list != null && new_list.size() > 0) {
mv.addObject("roomName", params.get("roomName"));
mv.addObject("roomNumber", params.get("roomNumber"));
mv.setViewName("chat");
}else {
} else {
mv.setViewName("room");
}
return mv;
}

// -------------- response 객체 --------------

@Getter
@AllArgsConstructor
private static class ChattingResponse {
private List<RoomDTO> roomDTOList;
}

// -------------- request 객체 --------------

@Setter
@Getter
private static class ChattingRequest {
private int roomNumber;
private String roomName;
private RoomDTO.Status status;
private Date updatetime;
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/market/server/dto/RoomDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public enum SortStatus {
private Status status;
private Date createtime;
private Date updatetime;
private List<RoomDTO> roomDTOList;
private SortStatus sortStatus;
private int searchCount;
private int pagingStartOffset;
Expand All @@ -36,7 +35,6 @@ public RoomDTO(@NonNull int roomNumber, String roomName) {
this.roomNumber = roomNumber;
this.roomName = roomName;
this.createtime = new Date();
roomDTOList = new ArrayList<RoomDTO>();
SortStatus sortStatus = SortStatus.NEWEST;
searchCount = 10;
pagingStartOffset = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/market/server/mapper/ChattingMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@

public interface ChattingMapper {
public int register(RoomDTO roomDTO);
public List<RoomDTO> selectRooms(RoomDTO roomDTO);
public List<RoomDTO> selectRooms(String sortStatus, int searchCount, int pagingStartOffset);
public int getLastRoomNumber();
}
14 changes: 12 additions & 2 deletions src/main/java/com/market/server/service/ChattingService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.market.server.service;

import com.market.server.dto.RoomDTO;
import com.market.server.mapper.ChattingMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
Expand All @@ -21,12 +23,20 @@ public void register(RoomDTO roomDTO) {
throw new RuntimeException("register ERROR! 채팅방 등록 메서드를 확인해주세요\n" + "Params : " + roomDTO);
}
}

// 채팅방 목록 찾기
public List<RoomDTO> getRooms(RoomDTO roomDTO) {
List<RoomDTO> roomDTOList = chattingMapper.selectRooms(roomDTO);
List<RoomDTO> roomDTOList = null;
if (roomDTO == null)
roomDTOList = chattingMapper.selectRooms("NEWEST", 20, 0);
else
roomDTOList = chattingMapper.selectRooms(roomDTO.getSortStatus().toString(), roomDTO.getSearchCount(), roomDTO.getPagingStartOffset());
return roomDTOList;
}

public int getLastRoomNumber() {
return chattingMapper.getLastRoomNumber();
}

}

22 changes: 22 additions & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# mysql
spring.datasource.url=jdbc:mysql://localhost:3307/market?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul
spring.datasource.username=root
spring.datasource.password=wnstjr1026

# message
spring.messages.basename=i18n/exception
spring.messages.encoding=UTF-8

# redis
spring.cache.type=redis
spring.data.redis.repositories.enabled=true
market.server.redis.host=localhost
market.server.redis.port=6379
market.server.redis.password=

# expire
expire.defaultTime=36288000

#JSP, HTML ModelAndView Path Setting
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
18 changes: 18 additions & 0 deletions src/main/resources/application-release.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# mysql
spring.datasource.url=jdbc:mysql://localhost:3307/market?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul
spring.datasource.username=root
spring.datasource.password=wnstjr1026

# message
spring.messages.basename=i18n/exception
spring.messages.encoding=UTF-8

# redis
spring.cache.type=redis
spring.data.redis.repositories.enabled=true
market.server.redis.host=localhost
market.server.redis.port=6379
market.server.redis.password=

# expire
expire.defaultTime=36288000
29 changes: 6 additions & 23 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
# mysql
spring.datasource.url=jdbc:mysql://localhost:3307/market?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul
spring.datasource.username=root
spring.datasource.password=wnstjr1026

# message
spring.messages.basename=i18n/exception
spring.messages.encoding=UTF-8

# redis
spring.cache.type=redis
spring.data.redis.repositories.enabled=true
market.server.redis.host=localhost
market.server.redis.port=6379
market.server.redis.password=

# expire
expire.defaultTime=36288000

# tomcat
# Server
server.port=8888

#JSP, HTML ModelAndView Path Setting
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# profile name = local, dev, prod
spring.profiles.active=dev

# session
spring.session.store-type=redis
37 changes: 15 additions & 22 deletions src/main/resources/mybatis/mapper/chattingMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,22 @@
`roomName`,
`type`
FROM `room`
LIMIT 100 OFFSET 0
<choose>
<when test="sortStatus == 'NEWEST'">
ORDER BY createtime DESC
</when>
<when test="sortStatus == 'OLDEST'">
ORDER BY createtime ASC
</when>
</choose>
LIMIT #{searchCount, jdbcType=INTEGER} OFFSET #{pagingStartOffset, jdbcType=INTEGER}
</select>

<!-- <select id="selectRooms" resultType="com.market.server.dto.RoomDTO">-->
<!-- SELECT `id`,-->
<!-- `title`,-->
<!-- `type`,-->
<!-- FROM `room`-->
<!-- <choose>-->
<!-- <when test="sortStatus.toString() == 'NEWEST'">-->
<!-- ORDER BY createtime DESC-->
<!-- </when>-->
<!-- <when test="sortStatus.toString() == 'OLDEST'">-->
<!-- ORDER BY createtime ASC-->
<!-- </when>-->
<!-- <when test="sortStatus.toString() == 'GRADE'">-->
<!-- LEFT JOIN user-->
<!-- ON product.accountId = user.accountId-->
<!-- GROUP BY user.accountId-->
<!-- ORDER BY user.grade DESC-->
<!-- </when>-->
<!-- </choose>-->
<!-- LIMIT #{searchCount, jdbcType=INTEGER} OFFSET #{pagingStartOffset, jdbcType=INTEGER}-->
<!-- </select>-->
<select id="getLastRoomNumber" resultType="int">
SELECT roomNumber
FROM room
ORDER BY roomNumber
DESC LIMIT 1;
</select>

</mapper>