๐Ÿ’ก ์†Œ์ผ“ ์ฑ„ํŒ…์—์„œ ๋ฉ”์‹œ์ง€๋งˆ๋‹ค ์ฝ์ง€ ์•Š์€ ์‚ฌ๋žŒ ์ˆ˜ ๋ณด์—ฌ์ฃผ๊ธฐ ๋ฐฉ๋ฒ•

ino5ยท2021๋…„ 8์›” 24์ผ
1

์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ ๊ตฌํ˜„ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
DB์™€ ์—ฐ๋™ํ•œ ์†Œ์ผ“์œผ๋กœ ๊ตฌํ˜„ํ•œ ์ฑ„ํŒ…๋ฐฉ์—์„œ ๋ฉ”์‹œ์ง€๋งˆ๋‹ค ์ฝ์ง€์•Š์€ ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.


โœ๏ธ ํ•œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ readMember() ํ˜ธ์ถœํ•˜๋ฉด์„œ๋ถ€ํ„ฐ ์‹œ์ž‘

readMember() ํ˜ธ์ถœ ์‹œ์ 

  • ๋ฐฉ์— ์ž…์žฅํ–ˆ์„ ๋•Œ (์†Œ์ผ“ ์—ฐ๊ฒฐ ๋์„ ๋•Œ)
  • ์†Œ์ผ“ ๋ฉ”์„ธ์ง€๋ฅผ ์ฝ์—ˆ์„ ๋•Œ

readMember()

  • ํด๋ผ์ด์–ธํŠธ ๋ณธ์ธ์ด ์ฝ์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์„œ๋ฒ„์— ์•Œ๋ ค์ฃผ๋Š” js ํ•จ์ˆ˜
  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ž…์žฅํ•œ ๋ฐฉ๊ณผ ๋ณธ์ธ์˜ ์•„์ด๋””(m_id)๋ฅผ ์•Œ๋ ค์ค€๋‹ค.
  • ์„œ๋ฒ„๋Š” db์— ํ•ด๋‹น ์œ ์ €๊ฐ€ ์ฝ์—ˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์—…๋ฐ์ดํŠธ
  • db ์—…๋ฐ์ดํŠธ ์ดํ›„ ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ์œ ์ €๋Š” send('readMember') ํ˜ธ์ถœ
function readMember() {
	$.ajax({
		url: _contextPath + '/talk/readMember',
		data: {	'roomNumber' : $("#roomNumber").val(),
				'm_id' : $("#m_id").val()
		},
		type: 'get',
		success: function (res) {
			send('readMember');
		},
		error : function(err){
			console.log('error');
		}
	});
}

โœ๏ธ send('readMember')๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์— type์ด 'readMember'์ธ ์†Œ์ผ“ ๋ฉ”์‹œ์ง€ ์ „์†ก

  • type์„ 'readMember'์ธ ์†Œ์ผ“ ๋ฉ”์‹œ์ง€ ์ „์†กํ•˜๋Š” js ํ•จ์ˆ˜
function send(type) {
	if (!type) {
		type = 'message';
	}
	var option ={
		type: type,
		roomNumber : $("#roomNumber").val(),  
		sessionId : $("#sessionId").val(),
		userName : $("#m_name").val(),
		msg : $("#chatting").val(),
		m_id : $("#m_id").val()
	}
	ws.send(JSON.stringify(option))
	if (type == 'message') {
		$('#chatting').val("");
	}
}

โœ๏ธ type์ด 'readMember'์ธ ์†Œ์ผ“ ๋ฉ”์‹œ์ง€๋ฅผ ์„œ๋ฒ„๊ฐ€ ๋ฐ›์•˜์„ ๋•Œ

sendMessageOfUpdateOfSession(rN) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ


โœ๏ธ sendMessageOfUpdateOfSession(rN)๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋ฐฉ ์†Œ์ผ“ ์„ธ์…˜๋“ค์—๊ฒŒ type์ด 'updateOfSession'์ธ ์†Œ์ผ“ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ

  • ๋ฐฉ๋ฒˆํ˜ธ rN์„ ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ์ž๋ฐ” ๋ฉ”์†Œ๋“œ
  • ํ•ด๋‹น ๋ฐฉ์— ์žˆ๋Š” ๋ชจ๋“  ์†Œ์ผ“ ์„ธ์…˜๋“ค์—๊ฒŒ type์ด 'updateOfSession'์ธ ์†Œ์ผ“ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ
	private void sendMessageOfUpdateOfSession(String roomNumber) {
		JSONObject objOfNumOfSession = new JSONObject();
		objOfNumOfSession.put("type", "updateOfSession");
		int numOfAllMember = talkService.selectJoinGroupMemberList(Integer.parseInt(roomNumber)).size();
		int numOfSessionMember = findNumOfSessionMember(roomNumber);
		objOfNumOfSession.put("numOfAllMember", numOfAllMember);
		objOfNumOfSession.put("numOfSessionMember", numOfSessionMember);
		sendMessage(roomNumber, objOfNumOfSession); // ํ•ด๋‹น ๋ฐฉ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์†Œ์ผ“ ์„ธ์…˜๋“ค์—๊ฒŒ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	}

โœ๏ธ type์ด 'updateOfSession'์ธ ์†Œ์ผ“ ๋ฉ”์‹œ์ง€๋ฅผ ํ•ด๋‹น ๋ฐฉ์— ์†Œ์ผ“ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ํด๋ผ์ด์–ธํŠธ๋“ค์ด ๋ฐ›์•˜์„ ๋•Œ

  • countUnread($("#roomNumber").val()) ํ˜ธ์ถœ

โœ๏ธ countUnread($("#roomNumber").val())๋ฅผ ํ†ตํ•ด ๋ฉ”์‹œ์ง€๋งˆ๋‹ค ์ฝ์ง€ ์•Š์€ ์‚ฌ๋žŒ ์ˆ˜ ๋ณด์—ฌ์ฃผ๊ธฐ

countUnread(tkrm_num)

  • ๋ฐฉ๋ฒˆํ˜ธ๋ฅผ ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” js ํ•จ์ˆ˜
  • ์„œ๋ฒ„์— ajax๋กœ ๋ฐฉ๋ฒˆํ˜ธ๋ฅผ ๋„˜๊ธฐ๋ฉด์„œ ํ•ด๋‹น ๋ฐฉ์˜ ๋ชจ๋“  ๋ฉ”์‹œ์ง€๋งˆ๋‹ค ์ฝ์ง€ ์•Š์€ ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ์‘๋‹ต๋ฐ›๋Š”๋‹ค.
  • ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ ๋ฉ”์‹œ์ง€ ์˜†์— ๋ณด์—ฌ์ค€๋‹ค.
function countUnread(tkrm_num) {
	$.ajax({
        url : _contextPath + '/talk/countUnread',
        data: {'roomNumber' : $("#roomNumber").val()},
        type : 'get',
        success: function(res) {
            let numOfUnreadList = res.numOfUnreadList;
            for(var i = 0; i<numOfUnreadList.length; i++) {
                let tk_num = numOfUnreadList[i].tk_num;
                let unread_num = numOfUnreadList[i].unread_num;
                $('#unReadNum' + tk_num).text(unread_num);
                if (unread_num == 0) {
                    $('#unReadNum' + tk_num).text('');
                }
            }
        },
        error : function(err){
            console.log('error');
        }        
	});
}
profile
์ง€๊ธˆ์€ ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ๋งŒ ํ•ด์š”... https://blog.naver.com/chero77

0๊ฐœ์˜ ๋Œ“๊ธ€