|
|
|
@ -12,14 +12,19 @@ import com.example.leudaemialikeme.Activity.ChatActivity;
|
|
|
|
|
import com.example.leudaemialikeme.Activity.MainActivity;
|
|
|
|
|
import com.example.leudaemialikeme.Controller.ActivityController;
|
|
|
|
|
import com.example.leudaemialikeme.Fragment.MessageChannelFragment;
|
|
|
|
|
import com.example.leudaemialikeme.Fragment.MessageFragment;
|
|
|
|
|
import com.example.leudaemialikeme.Model.Message;
|
|
|
|
|
import com.example.leudaemialikeme.R;
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
|
|
|
|
|
|
import org.java_websocket.drafts.Draft_6455;
|
|
|
|
|
import org.java_websocket.handshake.ServerHandshake;
|
|
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.sql.Timestamp;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
public class WebSocketClient extends org.java_websocket.client.WebSocketClient {
|
|
|
|
|
|
|
|
|
@ -49,35 +54,29 @@ public class WebSocketClient extends org.java_websocket.client.WebSocketClient {
|
|
|
|
|
public void onMessage(String message){
|
|
|
|
|
try {
|
|
|
|
|
// friendDao=new FriendDao();
|
|
|
|
|
Log.d("WS",message);
|
|
|
|
|
Log.d("WS",message+" onMessage()回调");
|
|
|
|
|
//提取消息
|
|
|
|
|
Message unReadMsg= gson.fromJson(message, Message.class);
|
|
|
|
|
String content=(String) unReadMsg.getContent();
|
|
|
|
|
int messageType=(int)unReadMsg.getMessageType();
|
|
|
|
|
int receiverId=(int)unReadMsg.getReceiverNetId();
|
|
|
|
|
int senderId=(int)unReadMsg.getSenderNetId();
|
|
|
|
|
long createTime=(long) unReadMsg.getCreateTime();
|
|
|
|
|
Map<String, String> jsonMap = gson.fromJson(message, new TypeToken<Map<String, String>>() {}.getType());
|
|
|
|
|
String content = jsonMap.get("content");
|
|
|
|
|
int messageType = Integer.parseInt(Objects.requireNonNull(jsonMap.get("messageType")));
|
|
|
|
|
int receiverId = Integer.parseInt(Objects.requireNonNull(jsonMap.get("receiverNetId")));
|
|
|
|
|
int senderId = Integer.parseInt(Objects.requireNonNull(jsonMap.get("senderNetId")));
|
|
|
|
|
long createTime = Timestamp.valueOf(jsonMap.get("createTime")).getTime();
|
|
|
|
|
Message newMessage=new Message(senderId,receiverId,messageType, content, createTime);
|
|
|
|
|
Log.d("刷新","获取到对面发送的消息");
|
|
|
|
|
//获取当前活动对象
|
|
|
|
|
currentActivity= ActivityController.getCurrentActivity();
|
|
|
|
|
// //判断消息发送者是否为本地好友,如果不是本地好友,需要进行好友同步,0表示是系统发送的消息
|
|
|
|
|
// if (senderId!=0){
|
|
|
|
|
// Friend friend=friendDao.findByNetId(senderId,receiverId);
|
|
|
|
|
// if (friend==null){
|
|
|
|
|
// getNewFriends(senderId,receiverId);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
//根据消息类型进行判断
|
|
|
|
|
/****/
|
|
|
|
|
String currentActivityName=currentActivity.getLocalClassName();
|
|
|
|
|
Log.d("刷新","当前活动名:"+currentActivityName);
|
|
|
|
|
if (messageType==MSG_SYSTEM){
|
|
|
|
|
if (content.equals("offline")){
|
|
|
|
|
offline();
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
//如果当前活动为聊天活动则进行数据展示
|
|
|
|
|
if (currentActivityName.equals("activity.ChatActivity")){
|
|
|
|
|
if (currentActivityName.equals("Activity.ChatActivity")){
|
|
|
|
|
Log.d("刷新","当前在聊天界面活动页");
|
|
|
|
|
updateChatUi(newMessage);
|
|
|
|
|
// 如果当前活动为聊天列表界面,需要对指定的元素进行局部更新,如果没有该用户在聊天列表中,需要添加该好友项
|
|
|
|
|
}else if (currentActivity.findViewById(R.id.chat_list_layout)!=null){
|
|
|
|
@ -86,9 +85,9 @@ public class WebSocketClient extends org.java_websocket.client.WebSocketClient {
|
|
|
|
|
}else{
|
|
|
|
|
newMessage.save();
|
|
|
|
|
notification();
|
|
|
|
|
updateChatListUi();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/****/
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
@ -109,10 +108,10 @@ public class WebSocketClient extends org.java_websocket.client.WebSocketClient {
|
|
|
|
|
currentActivity.runOnUiThread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
//TODO
|
|
|
|
|
chatActivity=(ChatActivity) currentActivity;
|
|
|
|
|
chatActivity = (ChatActivity) currentActivity;
|
|
|
|
|
//如果当前正在聊天的人为消息的发送者,则进行展示,并存为已读消息
|
|
|
|
|
if (chatActivity.friendNetId==newMessage.getSenderNetId()){
|
|
|
|
|
if (chatActivity.friendNetId == newMessage.getSenderNetId()){
|
|
|
|
|
Log.d("刷新","当前聊天的人与消息发送者为同一人");
|
|
|
|
|
chatActivity.addMsg(newMessage,1);
|
|
|
|
|
}
|
|
|
|
|
//如果当前正在聊天的人不是消息发送者,则更新一个未读标记
|
|
|
|
@ -130,8 +129,17 @@ public class WebSocketClient extends org.java_websocket.client.WebSocketClient {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
mainActivity = (MainActivity) currentActivity;
|
|
|
|
|
MessageChannelFragment msgcf = (MessageChannelFragment) mainActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_message_channel);
|
|
|
|
|
msgcf.updateUi();
|
|
|
|
|
// FragmentManager fm = mainActivity.getSupportFragmentManager();
|
|
|
|
|
// Log.d("刷新","当前碎片管理:"+fm.toString());
|
|
|
|
|
// MessageChannelFragment mcf = (MessageChannelFragment) fm.findFragmentById(R.id.fragment_message_channel);
|
|
|
|
|
// Log.d("刷新","当前所在碎片:"+mcf);
|
|
|
|
|
// MessageChannelFragment msgcf = (MessageChannelFragment) mainActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_message_channel);
|
|
|
|
|
// MessageChannelFragment mcf = new MessageChannelFragment();
|
|
|
|
|
MessageFragment mf = (MessageFragment) mainActivity.getFragment(2);
|
|
|
|
|
MessageChannelFragment mcf = mf.getAdapter().getMcf(0);
|
|
|
|
|
Log.d("当前碎片的名字滑动页:", mcf.getMessageCategoryTitle());
|
|
|
|
|
mcf.updateUi();
|
|
|
|
|
Log.d("刷新","聊天列表刷新完成");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|