-
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 |
---|---|---|
|
@@ -36,3 +36,4 @@ build/ | |
/market | ||
/logs/ | ||
/usedMarketServer/attachFile/ | ||
/attachFile/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,27 +35,27 @@ public void handleTextMessage(WebSocketSession session, TextMessage message) { | |
String rN = (String) obj.get("roomNumber"); | ||
HashMap<String, Object> temp = new HashMap<String, Object>(); | ||
|
||
if(rls.size() > 0) { | ||
for(int i=0; i<rls.size(); i++) { | ||
if (rls.size() > 0) { | ||
for (int i = 0; i < rls.size(); i++) { | ||
String roomNumber = (String) rls.get(i).get("roomNumber"); //세션리스트의 저장된 방번호를 가져와서 | ||
if(roomNumber.equals(rN)) { //같은값의 방이 존재한다면 | ||
if (roomNumber.equals(rN)) { //같은값의 방이 존재한다면 | ||
temp = rls.get(i); //해당 방번호의 세션리스트의 존재하는 모든 object값을 가져온다. | ||
break; | ||
} | ||
} | ||
|
||
//해당 방의 세션들만 찾아서 메시지를 발송해준다. | ||
for(String k : temp.keySet()) { | ||
if(k.equals("roomNumber")) { //다만 방번호일 경우에는 건너뛴다. | ||
for (String k : temp.keySet()) { | ||
if (k.equals("roomNumber")) { //다만 방번호일 경우에는 건너뛴다. | ||
continue; | ||
} | ||
|
||
WebSocketSession wss = (WebSocketSession) temp.get(k); | ||
if(wss != null) { | ||
if (wss != null) { | ||
try { | ||
wss.sendMessage(new TextMessage(obj.toJSONString())); | ||
} catch (IOException e) { | ||
log.error("sendMessage 실패" , e); | ||
log.error("sendMessage 실패", e); | ||
} | ||
} | ||
} | ||
|
@@ -70,15 +70,14 @@ public void handleBinaryMessage(WebSocketSession session, BinaryMessage message) | |
ByteBuffer byteBuffer = message.getPayload(); | ||
|
||
String fileName = new SimpleDateFormat("yyyyMMddHHmm'.jpg'").format(new 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. Synchronization SimpleDateFormat 인스턴스는 필요한 경우 전역 상수로 선언 할 수 있지만 jdk 1.8 doc 참고 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. 그럼 더 효율좋은 코드로 바꿔주세요~ |
||
File file = new File(""); | ||
String fileDirectoryName = file.getAbsolutePath()+"\\usedMarketServer\\attachFile\\"; | ||
String fileDirectoryName = System.getProperty("user.dir") + File.separator + "attachFile" + File.separator; | ||
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. 로컬변수에서 상수로 변경하였습니다. |
||
|
||
File dir = new File(fileDirectoryName); | ||
if(!dir.exists()) { | ||
if (!dir.exists()) { | ||
dir.mkdirs(); | ||
} | ||
|
||
file = new File(fileDirectoryName, fileName); | ||
File file = new File(fileDirectoryName, fileName); | ||
FileOutputStream out = null; | ||
FileChannel outChannel = null; | ||
try { | ||
|
@@ -87,33 +86,33 @@ public void handleBinaryMessage(WebSocketSession session, BinaryMessage message) | |
outChannel = out.getChannel(); //채널을 열고 | ||
byteBuffer.compact(); //파일을 복사한다. | ||
outChannel.write(byteBuffer); //파일을 쓴다. | ||
}catch(Exception e) { | ||
log.error("파일 write 실패" , e); | ||
}finally { | ||
} catch (Exception e) { | ||
log.error("파일 write 실패", e); | ||
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. IOException printStackTrace(); 추가했습니다. |
||
} finally { | ||
try { | ||
if(out != null) { | ||
if (out != null) { | ||
out.close(); | ||
} | ||
if(outChannel != null) { | ||
if (outChannel != null) { | ||
outChannel.close(); | ||
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. try-with-resource 를 쓰면 어떨까요? 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. 변경했습니다. |
||
} | ||
}catch (IOException e) { | ||
log.error("파일 전송 실패" , e); | ||
} catch (IOException e) { | ||
log.error("파일 전송 실패", e); | ||
} | ||
} | ||
|
||
byteBuffer.position(0); //파일을 저장하면서 position값이 변경되었으므로 0으로 초기화한다. | ||
//파일쓰기가 끝나면 이미지를 발송한다. | ||
HashMap<String, Object> temp = rls.get(fileUploadIdx); | ||
for(String k : temp.keySet()) { | ||
if(k.equals("roomNumber")) { | ||
for (String k : temp.keySet()) { | ||
if (k.equals("roomNumber")) { | ||
continue; | ||
} | ||
WebSocketSession wss = (WebSocketSession) temp.get(k); | ||
try { | ||
wss.sendMessage(new BinaryMessage(byteBuffer)); //초기화된 버퍼를 발송한다. | ||
} catch (IOException e) { | ||
log.error("sendMessage 실패" , e); | ||
log.error("sendMessage 실패", e); | ||
} | ||
} | ||
} | ||
|
@@ -127,21 +126,21 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio | |
System.out.println(url); | ||
String roomNumber = url.split("/chating/")[1]; | ||
int idx = rls.size(); //방의 사이즈를 조사한다. | ||
if(rls.size() > 0) { | ||
for(int i=0; i<rls.size(); i++) { | ||
if (rls.size() > 0) { | ||
for (int i = 0; i < rls.size(); i++) { | ||
String rN = (String) rls.get(i).get("roomNumber"); | ||
if(rN.equals(roomNumber)) { | ||
if (rN.equals(roomNumber)) { | ||
flag = true; | ||
idx = i; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if(flag) { //존재하는 방이라면 세션만 추가한다. | ||
if (flag) { //존재하는 방이라면 세션만 추가한다. | ||
HashMap<String, Object> map = rls.get(idx); | ||
map.put(session.getId(), session); | ||
}else { //최초 생성하는 방이라면 방번호와 세션을 추가한다. | ||
} else { //최초 생성하는 방이라면 방번호와 세션을 추가한다. | ||
HashMap<String, Object> map = new HashMap<String, Object>(); | ||
map.put("roomNumber", roomNumber); | ||
map.put(session.getId(), session); | ||
|
@@ -158,8 +157,8 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio | |
@Override | ||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { | ||
//소켓 종료 | ||
if(rls.size() > 0) { //소켓이 종료되면 해당 세션값들을 찾아서 지운다. | ||
for(int i=0; i<rls.size(); i++) { | ||
if (rls.size() > 0) { //소켓이 종료되면 해당 세션값들을 찾아서 지운다. | ||
for (int i = 0; i < rls.size(); i++) { | ||
rls.get(i).remove(session.getId()); | ||
} | ||
} | ||
|
@@ -172,7 +171,7 @@ private static JSONObject jsonToObjectParser(String jsonStr) { | |
try { | ||
obj = (JSONObject) parser.parse(jsonStr); | ||
} catch (ParseException e) { | ||
log.error("jsonToObjectParser 실패" , e); | ||
log.error("jsonToObjectParser 실패", e); | ||
} | ||
return obj; | ||
} | ||
|
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.
roomNumber
가 null일 수도 있을까요?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.
roomNumber는 pk로 null일수 없습니다.