채팅 메시지
채팅 메시지를 주고받기 위해서는 필요한 객체 선언과 채팅 UI 기능들이 사전에 준비되어야 합니다. 아직 준비가 완료되지 않았다면 시작하기 가이드를 먼저 확인해 주세요.
메시지 전송
채팅방에 일반 메시지를 전송하는 방법입니다.
java
JSONObject param = new JSONObject();
param.put("message", "안녕하세요");
param.put("mimeType", "text");
channel.sendMessage(param, new ChannelCallback() {
@Override
public void callback(Object o, VChatCloudException e) {
if (e != null) {
Log.d(TAG,"메시지 전송오류");
}
}
});
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
파라미터 값
값 식별자 설명 message String 전송할 메시지 mimeType String 메시지형태 (text: 일반텍스트, emoji_img: 이모지) 결과 값
값 식별자 설명 code String 에러 코드 message String 에러 메시지
전송버튼 클릭 이벤트
java
// ChatActivity.java
$SendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JSONObject param = new JSONObject();
JSONObject messageType = new JSONObject();
messageType.put("profile", String.valueOf(nick_icon_index + 1));
param.put("message", $EditText.getText().toString());
param.put("mimeType", "text");
param.put("messageType", messageType.toJSONString());
if (userInfo != null) {
param.put("userInfo", userInfo.toJSONString());
}
sendMsg(param);
$EditText.setText("");
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
이모지 버튼 이벤트
java
// EmojiAdapter.java
viewById.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
JSONObject param = new JSONObject();
JSONObject messageType = new JSONObject();
messageType.put("profile", String.valueOf(ChatActivity.nick_icon_index + 1));
DecimalFormat df = new DecimalFormat("000");
String text = msgStart + df.format(i + 1) + msgEnd;
param.put("message", text);
param.put("mimeType", "emoji_img");
param.put("messageType", messageType.toJSONString());
ChatActivity.sendMsg(param);
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
신규 메시지 이벤트
채팅방에 새로운 메시지가 도착했을 때 발생하는 이벤트입니다.
java
// ChatActivity.java
public void onNotifyMessage(JSONObject data) { // 메시지 이벤트
Log.d("채팅글", data);
Message msg = new Message(data);
msg.setType("msg");
messageExposure(msg, false);
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
수신된
JSONObject
데이터 구조값 식별자 설명 nickName String 메시지를 전송한 채팅방 입장 유저의 별명 roomId String 채팅방 생성 후 발급받은 Channel Key clientKey String 메시지를 전송한 접속 단말 설정 고유키 message String 전송된 메시지 mimeType String 메시지형태 (text: 일반텍스트, emoji: 이모지) messageDt String 전송 날짜 messageCount String 채팅방 메시지 전송 수 grade String 메시지를 전송한 유저의 등급 ( 사용자등급표에서 확인 )
특정 사용자에게 귓속말 전송
java
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, "Yes Btn Click");
// Text 값 받아서 로그 남기기
String value = et.getText().toString();
Log.d(TAG, value);
JSONObject json = new JSONObject();
json.put("receivedClientKey", message.getClientKey());
json.put("message", value);
json.put("mimeType", "text");
channel.sendWhisper(json, new ChannelCallback() {
@Override
public void callback(Object o, VChatCloudException e) {
if (e != null) {
Log.d(TAG,"메시지 전송오류");
}
$RecyclerView.smoothScrollToPosition($Adapter.getItemCount());
}
});
dialog.dismiss(); //닫기
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
json
파라미터 값값 식별자 설명 message String 전송할 메시지 mimeType String 메시지 형태 (text: 일반텍스트, emoji: 이모지) receivedClientKey String 수신 유저의 접속 단말 설정 고유키 결과 값
- 에러 정보 (
VChatCloudException
)
값 식별자 설명 code String 에러 코드 message String 에러 메시지 - 메시지 정보
값 식별자 설명 roomId String 채팅방 생성 후 발급받은 Channel Key nickName String 채팅방 입장 유저의 별명 clientKey String 메시지를 전송한 접속 단말 설정 고유키 message Date 전송한 메시지 mimeType String 메시지 형태 (text: 일반텍스트, emoji: 이모지) messageDt String 전송 날짜 messageCount String 채팅방 메시지 전송 수 receivedNickName String 메시지를 받는 유저의 별명 receivedClientKey String 메시지를 받는 접속 단말 설정 고유키 grade String 메시지를 전송한 유저의 등급 ( 사용자등급표에서 확인 ) - 에러 정보 (
개인 귓속말 메시지 수신 이벤트
자신에게 귓속말 메시지가 도착했을 때 발생하는 이벤트입니다.
java
// ChatActivity.java
public void onPersonalWhisper(JSONObject data) { // 개인 귓속말 수신 이벤트
Message msg = new Message(data);
msg.setType("preWhisper");
messageExposure(msg, false);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
수신된
JSONObject
데이터 구조값 식별자 설명 roomId String 채팅방 생성 후 발급받은 Channel Key nickName String 채팅방 입장 유저의 별명 clientKey String 메시지를 전송한 접속 단말 설정 고유키 message Date 전송한 메시지 mimeType String 메시지 형태 (text: 일반텍스트, emoji: 이모지) messageDt String 전송 날짜 messageCount String 채팅방 메시지 전송 수 receivedClientKey String 메시지를 받는 접속 단말 설정 고유키 grade String 메시지를 전송한 유저의 등급 ( 사용자등급표에서 확인 )