pingpong 구현, 연결 확인,

249 views
Skip to first unread message

노유비

unread,
May 23, 2016, 12:42:22 AM5/23/16
to Netty Korean User Group
안녕하세요. 이제 막 netty를 접한 초보 개발자입니다.

netty를 WebSocket 형식으로 만들어 현재 컴퓨터에서 모바일을 제어하는 서버를 만들고 있습니다.

현재 서버에서 클라이언트의 갑작스러운 종료를 인식하지 못하는 이슈가 있어
현재 메시지를 특정한 시간마다 반복해서 보내는 방법으로 문제를 해결하였습니다.

하지만 테스트 하는 와중 A모바일에서 Web Socket Sever에 n개의 접속을 만든 뒤 B모바일에서 다시 n개의 접속을 만들면
기존에 살아있는지 체크하는 메시지들이 더 이상 보내지지 않는 문제가 있습니다.

원인으로는 서버 내부에 저장한 데이터(채널 정보)를 제대로 찾지 못하는 것을 확인하였으나
무슨 이유로 제대로 못 찾는지 잘 몰라 질문을 드리게 되었습니다.

코드를 보시면 tenent를 이용하여 정보를 찾으나 올바른 tenant를 찾지 못하고 있습니다.
올바르게 해결할 수 있도록 지도 부탁드립니다.

다음은 구현 코드 부분입니다.

public void onMessageWebSocketFrame(WebSocketServerHandshaker handshaker, final ChannelHandlerContext ctx,
final WebSocketFrame msg) {

// 소켓 닫을때
if (msg instanceof CloseWebSocketFrame && handshaker != null) {
handshaker.close(ctx.channel(), (CloseWebSocketFrame) msg.retain());
return;
}

if (msg instanceof TextWebSocketFrame) {
String msgTmp = ((TextWebSocketFrame) msg).text();

try {
// msg type 확인 하고 각 처리
JSONObject msgObj = new JSONObject(msgTmp);
JSONObject header = msgObj.getJSONObject(Res.HEADER);
JSONObject data = msgObj.getJSONObject(Res.DATA);

Tenant tenent = TenantMng.getinstance().getTenant(header);
Agent agent = tenent.getAgent(header, data);

// 메세지 해석 후 메세지 전달
agent.send(msgObj, ctx);

Log.i("ChannelMng", "onMessageWebSocketFrame : " + "send" + " "
+ header.getString(Res.HDR_TENANT_PREFIX) + " " + AddressMng.getinstance().getSize());
// Log.i("ChannelMng", "onMessageWebSocketFrame : " + " send" +
// " " + CommonUtil.getTime()
// +" "+ AddressMng.getinstance().getSize());

} catch (JSONException e) {
Log.e("ChannelMng", "onMessageWebSocketFrame " + e.getMessage());
}

}

}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
private static Map<String, Tenant> tenants = new HashMap<String, Tenant>();

public Tenant getTenant(JSONObject header) throws JSONException {
String tenantPrefix = header.getString(Res.HDR_TENANT_PREFIX);
Tenant tenant = tenants.get(tenantPrefix);

if (tenant == null) {
tenant = new Tenant(tenantPrefix);
tenants.put(tenantPrefix, tenant);
}

return tenant;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

private static Map<String, Agent> Agents = new HashMap<String, Agent>();

public Agent getAgent(JSONObject header, JSONObject data) throws JSONException {
// TODO Auto-generated method stub
Agent agent = AgentMng.getinstance().getAgent(Agents, header);

return agent;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


public void send(JSONObject msgObj, ChannelHandlerContext ctx) throws NullPointerException {
// TODO Auto-generated method stub

// 갑 할당
try {
header = msgObj.getJSONObject(Res.HEADER);
data = msgObj.getJSONObject(Res.DATA);
init(header, data);
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.i("Agent", "init : " + e.getMessage());
}

// header(정적) 메시지
JSONObject headerMessage = MsgMng.getHeaderMsg(tenantPrefix, agentId, system, IF_GROUP, IF_ID);

// 추가로 정보를 확인한 뒤 상대방을 찾아 메시지를 전달

}

}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
감사합니다.


Message has been deleted

노유비

unread,
May 23, 2016, 12:46:39 AM5/23/16
to Netty Korean User Group

참고로 메시지 주기는 10초로 되어있습니다.
서버 -> 클라이언트 -> 서버 -> 클라이언트 
메시지 구조는 받으면 다시 같은 내용을 전달하는 구조입니다.

노유비

unread,
May 23, 2016, 2:05:28 AM5/23/16
to Netty Korean User Group
자답입니다. 전체적인 구조는 문제가 없는데 static을 오용해서 생긴 문제입니다.

감사합니다.

2016년 5월 23일 월요일 오후 1시 46분 39초 UTC+9, 노유비 님의 말:
Reply all
Reply to author
Forward
0 new messages