聊天室

This commit is contained in:
曹磊 2024-08-30 11:39:00 +08:00
parent 80f9ed08a7
commit e136f303a7
6 changed files with 39 additions and 7 deletions

View File

@ -17,6 +17,7 @@ import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -33,6 +34,9 @@ public class AppChatSocket {//用户聊天
* 以用户的id为keyWebSocket为对象保存起来
*/
private static Map<String, AppChatSocket> clients = new ConcurrentHashMap<String, AppChatSocket>();
private static Map<String, Session> sessionMap = new ConcurrentHashMap<String, Session>();
private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 会话
@ -76,6 +80,7 @@ public class AppChatSocket {//用户聊天
clients.remove(userId);
}
clients.put(userId, this);
sessionMap.put(userId,session);
/*sendMessageTo("恭喜你连接成功!",wxUserId);*/
}
@ -129,13 +134,21 @@ public class AppChatSocket {//用户聊天
wxChatContent.setContent(textMessage);
wxChatContent.setMessageType(messageType);
wxChatContent.setCreateTime(sdf.format(new Date()));
//判断对方是否在线
AppChatSocket chatSocket = clients.get(userId);
if (chatSocket!=null) {
chatSocket.session.getAsyncRemote().sendText(message);
}
wxChatContent.setStatus(0);
chatContentService.save(wxChatContent);
//判断对方是否在线
// AppChatSocket chatSocket = clients.get(userId);
// if (chatSocket!=null) {
// chatSocket.session.getAsyncRemote().sendText(message);
// }
List<ChatContent> toUsers = chatContentService.selectUserByConversationId(Long.parseLong(chatConversationId));
for(ChatContent toUser :toUsers){
Session userSession = sessionMap.get(toUser.getUserId());
if (userSession!=null) {
userSession.getAsyncRemote().sendText(message);
System.err.println(this.userId+"发送成功:"+userId);
}
}
UserEntity userEntity = userService.selectUserById(Long.parseLong(userId));
if(userEntity!=null && StringUtils.isNotBlank(userEntity.getClientid())){
UserEntity user = userService.selectUserById(Long.parseLong(this.userId));
@ -146,6 +159,9 @@ public class AppChatSocket {//用户聊天
}
userService.pushToSingle("新消息提醒",user.getUserName()+":"+textMessage,userEntity.getClientid());
}
/*AppChatSocket chatSocket = clients.get(userId);
if (chatSocket!=null) {
chatSocket.session.getAsyncRemote().sendText(message);
@ -154,7 +170,6 @@ public class AppChatSocket {//用户聊天
catch (Exception e){
log.error("发生了错误了"+e.getMessage(),e);
}
}

View File

@ -22,4 +22,6 @@ public interface ChatContentDao extends BaseMapper<ChatContent> {
int selectChatCount(@Param("userId") Long userId);
List<ChatContent> selectUserByConversationId(@Param("chatConversationId") Long chatConversationId);
}

View File

@ -6,6 +6,8 @@ import com.sqx.common.utils.PageUtils;
import com.sqx.modules.chat.entity.ChatContent;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ChatContentService extends IService<ChatContent> {
PageUtils selectChatContentPage(Integer page, Integer limit, Long chatConversationId,String content);
@ -14,4 +16,6 @@ public interface ChatContentService extends IService<ChatContent> {
int selectChatCount(Long userId);
List<ChatContent> selectUserByConversationId(Long chatConversationId);
}

View File

@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
@ -36,4 +37,9 @@ public class ChatContentServiceImpl extends ServiceImpl<ChatContentDao, ChatCont
return baseMapper.selectChatCount(userId);
}
@Override
public List<ChatContent> selectUserByConversationId(Long chatConversationId) {
return baseMapper.selectUserByConversationId(chatConversationId);
}
}

View File

@ -47,7 +47,7 @@ public class SysUserTokenServiceImpl extends ServiceImpl<SysUserTokenDao, SysUse
this.updateById(tokenEntity);
}
Result r = Result.success().put("token", token).put("expire", EXPIRE);
Result r = Result.success().put("token", token).put("userId", userId).put("expire", EXPIRE);
return r;
}

View File

@ -25,5 +25,10 @@
where status=0 and user_id!=#{userId} and chat_conversation_id in (select chat_conversation_id from chat_conversation where user_id=#{userId} or focused_user_id=#{userId})
</select>
<select id="selectUserByConversationId" resultType="ChatContent">
select distinct user_id
from chat_content
where chat_conversation_id=#{chatConversationId}
</select>
</mapper>