자바 패키지 소개

 

 

자바 폴더의 패키지 구조입니다.

 

before 스프링부트 다중채팅방 코드
after 스프링부트 다중채팅방 코드

chat 패키지의 수정된 부분은 ChatRoomRepository 클래스입니다.

chatRoomMap 변수에 public static이 붙었습니다.

 

++) 이 포스팅은 스프링부트 다중 채팅방 프로젝트를 기반으로합니다.

스프링부트 다중 채팅방 프로젝트 다운 및 설명 주소입니다.

kidmeokgu0.tistory.com/7?category=0

 

스프링부트 다중 채팅방 만들기 (2)

뷰 관점에서 본 코드 흐름 컨트롤러 패키지 구조입니다. 뷰 폴더 구조입니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.dms.controller; import java.util.Collection..

kidmeokgu0.tistory.com

controller 패키지는

1)  login 컨트롤러(클래스) 추가되었습니다.

2) room 컨트롤러(클래스)가 변경되었습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.dms.controller;
 
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
@RequestMapping("/multiRoom")
public class login {
 
    @GetMapping("/login")
    public String homeController(Model model, HttpServletRequest request) {
 
        return "login";
    }
}
 
cs

controller 패키지의 login 클래스 입니다.

++)login 뷰(jsp)로 이동합니다.

로그인 화면(로그인 뷰 실행화면)입니다.

 

 완료버튼을 누르면 loginPcs 클래스로 입력한 아이디가 전달됩니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.dms.process;
 
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/multiRoom")
public class loginPcs {
 
    @GetMapping("/loginPcs")
    public void loginPcsRc(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String my_id = request.getParameter("my_id");
        session.setAttribute("my_id", my_id);
        response.sendRedirect("/multiRoom/home");
    }
}
 
cs

process패키지의 loginPcs 클래스입니다.

 

HttpSession에 전달받은 id를 등록합니다.

그리고 home 컨트롤러로 이동합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.dms.controller;
 
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dms.entity.chatLog;
import com.dms.service.service;
 
@Controller
@RequestMapping("/multiRoom")
public class room {
 
    @GetMapping("/room")
    public String roomController(Model model, HttpServletRequest request) {
        service service = new service();
        HttpSession session = request.getSession();
        String roomId = request.getParameter("id");
        String my_id = (String) session.getAttribute("my_id");
        List<chatLog> list = service.getChatlog(roomId);
        
        model.addAttribute("roomId", roomId);
        model.addAttribute("my_id", my_id);
        model.addAttribute("list", list);
 
        return "room";
    }
}
 
cs

my_id 변수에 HttpSession에 등록된 id를 저장합니다.

my_id 변수를 모델에 등록합니다.

roomId 변수를 통해 service 클래스에 있는 getChatlog 메소드를 실행합니다.

그리고 실행결과를 list 변수에 넣습니다.

list 변수를 모델에 등록합니다.

 

ajax 패키지 구조
룸 뷰 화면

룸에서 메시지를 전송하면 saveMessage 클래스가 실행됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.dms.ajax;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dms.service.service;
 
@RestController
@RequestMapping("/multiRoom")
public class saveMessage {
 
    @PostMapping("/saveMessage")
    public void saveMessageAjax(Model model, HttpServletRequest request) {
        
        service service = new service();
        HttpSession session = request.getSession();
        
        String my_id = (String) session.getAttribute("my_id");
        String message = request.getParameter("message");
        String roomId = request.getParameter("roomId");
        int chatId = Integer.parseInt(request.getParameter("chat_id"));
        int size = Integer.parseInt(request.getParameter("size"));
 
        service.saveMessage(my_id, message, roomId, chatId, size);                    
        
    }
}
 
cs

saveMessage 클래스 입니다.

db에 메시지를 저장합니다.

 

chat_id 변수는 채팅 id입니다.

룸 뷰에서 getChatSeq 클래스를 연결하여 가져옵니다.(ajax)

그리고 saveMessage 클래스로 보냅니다.

 

size 변수는 방에 접속해있는 사람의 수입니다.

룸 뷰에서 getCncting 클래스를 연결하여 가져옵니다.(ajax)

그리고 saveMessage 클래스로 보냅니다.

 

my_id 변수는 누가 메시지를 보냈는지를 구분하기 위해 사용합니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.dms.ajax;
 
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dms.service.service;
 
@RestController
@RequestMapping("/multiRoom")
public class getChatSeq {
    
    @PostMapping("/getChatSeq")
    public int getChatSeqAjax(HttpServletResponse response) throws IOException {
        service service = new service(); 
        int chatSeq = service.getChatSeq();
        return chatSeq;
    }
}
 
cs

getChatSeq 클래스입니다.

채팅 id를 가져옵니다.

채팅 id는 오라클 db의 시퀀스입니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.dms.ajax;
 
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dms.chat.ChatRoomRepository;
 
@RestController
@RequestMapping("/multiRoom")
public class getCncting {
 
    @PostMapping("/getCncting")
    public int getLiveCntAjax(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String roomId = request.getParameter("roomId");
        int size = ChatRoomRepository.chatRoomMap.get(roomId).getSessions().size();
        return size;
    }
    
}
 
cs

getCncting 클래스입니다.

룸 뷰에서 방 번호(roomId)를 보낸걸 받습니다.

 이 방 번호(roomId)를 사용하여,

ChatRoomRepository 클래스의 chatRoomMap 변수에 저장된 chatRoom 클래스를 찾습니다.

 

++) chatRoomMap의 key는 roomId입니다.

그리고 value는 chatRoom 클래스입니다.

그러므로 map의 메소드인 get(key)을 사용하면 chatRoom 클래스를 찾을 수 있습니다.

그리고 찾은 chatRoom 클래스의 sessions 변수에 등록된 유저들의 수를 구합니다.

 

ChatRoomRepository, chatRoom 클래스의 관계는 스프링 부트 다중 채팅방 만들기

포스팅에 소개해놓았습니다.

 

db 구조

chatlog 테이블의 구조와 데이터 타입

unread 는 안읽은 사람의 수를 저장하는 칼럼입니다.

 

who_read는 메시지를 읽은 사람의 id를 저장하는 칼럼입니다.

 

 

 

gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}
 
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
 
repositories {
    mavenCentral()
    mavenLocal()
}
 
 dependencies {
 
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation('org.springframework.boot:spring-boot-starter-test'
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-websocket')
    compile('org.webjars:sockjs-client:1.1.2')
    compile('org.webjars:webjars-locator:0.30')
    runtime('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('org.springframework.boot:spring-boot-starter-test')
    compile('org.apache.tomcat.embed:tomcat-embed-jasper')
 
 
}
 
test {
    useJUnitPlatform()
}
 
cs

 

다음 포스팅은 뷰 소개 입니다.

 

+ Recent posts