-
Notifications
You must be signed in to change notification settings - Fork 3
#30 웹소켓을 이용한 1:1 채팅 구현 #32
base: feature/29
Are you sure you want to change the base?
Changes from 1 commit
f455586
5fabb1d
bf32376
4daebd0
c19f1ba
4e73acc
cfdde06
88e212c
18c4ad4
f56371c
fc9f045
bd8c3b2
5a4efeb
c5a76b7
6e23408
f4f1fad
8d887b5
5fb8d10
7d4b6ca
825152d
e4e31df
f1d226c
05eba04
fdb1bf9
9717040
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ | |
import com.market.server.service.ChattingService; | ||
import io.swagger.annotations.Api; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
|
@@ -40,14 +40,12 @@ public ModelAndView room() { | |
*/ | ||
@PostMapping("/rooms") | ||
@ResponseBody | ||
public List<RoomDTO> createRoom(RoomDTO roomDTO, Model model) { | ||
model.addAttribute("roomDTO", roomDTO); | ||
public List<RoomDTO> createRoom(RoomDTO roomDTO) { | ||
String roomName = roomDTO.getRoomName(); | ||
|
||
RoomDTO room = null; | ||
|
||
if (roomName != null && !roomName.trim().equals("")) { | ||
int roomNumber = 0; | ||
roomNumber = chattingService.getLastRoomNumber(); | ||
int roomNumber = chattingService.getLastRoomNumber(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이것도 동시성문제가 있어보이네요. 이 메소드는 호출할때마다 다른값을 반환하나요? 혹은 같은 값을 반환할수도 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RoomDTO가 정상적으로 등록되지 않았다면 같은값을 반환할 수도 있습니다. |
||
room = RoomDTO.builder() | ||
.roomNumber(++roomNumber) | ||
.roomName(roomName) | ||
|
@@ -76,8 +74,7 @@ public List<RoomDTO> getRoom(@RequestBody RoomDTO roomDTO) { | |
* @return | ||
*/ | ||
@GetMapping("/chatting") | ||
public ModelAndView moveRoom(RoomDTO roomDTO, Model model) { | ||
model.addAttribute("roomDTO", roomDTO); | ||
public ModelAndView moveRoom(RoomDTO roomDTO) { | ||
ModelAndView mv = new ModelAndView(); | ||
int roomNumber = roomDTO.getRoomNumber(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
public class SocketHandler extends TextWebSocketHandler { | ||
|
||
private List<HashMap<String, Object>> rls = new ArrayList<>(); //웹소켓 세션을 담아둘 리스트 ---roomListSessions | ||
private static final int DEFAULT_FILE_UPLOAD_INDEX = 0; | ||
private static final String FILE_DIRECTORY_NAME = System.getProperty("user.dir") + File.separator + "attachFile" + File.separator; | ||
|
||
@Override | ||
public void handleTextMessage(WebSocketSession session, TextMessage message) { | ||
|
@@ -65,31 +67,31 @@ public void handleTextMessage(WebSocketSession session, TextMessage message) { | |
@SneakyThrows | ||
@Override | ||
public void handleBinaryMessage(WebSocketSession session, BinaryMessage message) { | ||
int fileUploadIdx = 0; | ||
//바이너리 메시지 발송 | ||
ByteBuffer byteBuffer = message.getPayload(); | ||
String fileName = DateUtil.getNowTimeToyyyyMMddHHmm(new Date(), ".jpg"); | ||
String fileDirectoryName = System.getProperty("user.dir") + File.separator + "attachFile" + File.separator; | ||
File file = new File(fileDirectoryName, fileName); | ||
File file = new File(FILE_DIRECTORY_NAME, fileName); | ||
File dir = new File(FILE_DIRECTORY_NAME); | ||
|
||
File dir = new File(fileDirectoryName); | ||
if (!dir.exists()) { | ||
dir.mkdirs(); | ||
} | ||
|
||
try (FileOutputStream out = new FileOutputStream(file, true); //생성을 위해 OutputStream을 연다. | ||
FileChannel outChannel = out.getChannel(); //채널을 열고) | ||
try ( | ||
FileOutputStream out = new FileOutputStream(file, true); //생성을 위해 OutputStream을 연다. | ||
FileChannel outChannel = out.getChannel(); //채널을 열고 | ||
) { | ||
byteBuffer.flip(); //byteBuffer를 읽기 위해 세팅 | ||
byteBuffer.compact(); //파일을 복사한다. | ||
outChannel.write(byteBuffer); //파일을 쓴다. | ||
} catch (Exception e) { | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 쓰면 안돼는 메소드를 쓰셨네요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
e.getMessage(), e.toString() 를 사용해도 될까요?? |
||
log.error("파일 전송 실패", e); | ||
} | ||
|
||
byteBuffer.position(0); //파일을 저장하면서 position값이 변경되었으므로 0으로 초기화한다. | ||
//파일쓰기가 끝나면 이미지를 발송한다. | ||
HashMap<String, Object> temp = rls.get(fileUploadIdx); | ||
HashMap<String, Object> temp = rls.get(DEFAULT_FILE_UPLOAD_INDEX); | ||
for (String k : temp.keySet()) { | ||
if (k.equals("roomNumber")) { | ||
continue; | ||
|
@@ -98,6 +100,7 @@ public void handleBinaryMessage(WebSocketSession session, BinaryMessage message) | |
try { | ||
wss.sendMessage(new BinaryMessage(byteBuffer)); //초기화된 버퍼를 발송한다. | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
log.error("sendMessage 실패", e); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package com.market.server.utils; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.text.ParseException; | ||
|
||
@Log4j2 | ||
public class DateUtil { | ||
|
||
/** | ||
|
@@ -13,8 +16,14 @@ public class DateUtil { | |
* @return 처리 시간의 문자열 202009040159.jpg | ||
* @author topojs8 | ||
*/ | ||
public static String getNowTimeToyyyyMMddHHmm(Date date, String fileType) { | ||
return new SimpleDateFormat("yyyyMMddHHmm").format(date) + fileType; | ||
private static final ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<SimpleDateFormat>(); | ||
public static final String getNowTimeToyyyyMMddHHmm(Date date, String fileType) { | ||
SimpleDateFormat sdf = tl.get(); | ||
if(sdf == null) { | ||
sdf = new SimpleDateFormat("yyyyMMddHHmm"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
이거는 thread safe 한가요? 그리고 스레드로컬 사용법에 대해 잘못 이해하고 계신듯 하네요~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로컬변수여서 thread safe한 줄 알고 있는데 아닐까요?? |
||
tl.set(sdf); | ||
} | ||
return sdf.format(date) + fileType; | ||
} | ||
|
||
/** | ||
|
@@ -28,9 +37,10 @@ private static void parseDate(String dateStr) { | |
try { | ||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmm"); | ||
Date date = simpleDateFormat.parse(dateStr); | ||
log.info("Successfully Parsed Date " + date); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그 포맷팅을 사용해주세요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변경했습니다. |
||
System.out.println("Successfully Parsed Date " + date); | ||
} catch (ParseException e) { | ||
System.out.println("ParseError " + e.getMessage()); | ||
log.error("ParseError", e); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 마찬가지입니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변경했습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변경이 안되어있네요 |
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ market.server.redis.port=6379 | |
market.server.redis.password= | ||
|
||
# expire | ||
# millisecond 600������ �� 10�ð� | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 뭔가 글자가 깨진것 같네요 |
||
expire.defaultTime=36288000 | ||
|
||
#JSP, HTML ModelAndView Path Setting | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
roomName
이 null이면 NPE가 날 것 같네요