提交 a8db5171 编写于 作者: cxt104926's avatar cxt104926

聊天

上级 45554745
...@@ -33,6 +33,9 @@ public class ChatController { ...@@ -33,6 +33,9 @@ public class ChatController {
log.info("当前在线用户数:{}", clients.size()); log.info("当前在线用户数:{}", clients.size());
// todo 查看哪些用户在线,查看clients中的key即可 // todo 查看哪些用户在线,查看clients中的key即可
// todo 在用户连接成功后需要查看有哪些用户在线 将在线用户信息返回去 // todo 在用户连接成功后需要查看有哪些用户在线 将在线用户信息返回去
sendMessage(session, "连接成功"); sendMessage(session, "连接成功");
...@@ -49,9 +52,12 @@ public class ChatController { ...@@ -49,9 +52,12 @@ public class ChatController {
try { try {
ChatMsg msg = mapper.readValue(message, ChatMsg.class); ChatMsg msg = mapper.readValue(message, ChatMsg.class);
if ("1".equals(msg.getMsgType())) { if ("1".equals(msg.getMsgType())) {
// 群聊 // 广播
sendMessageAll(msg.getMsg(), msg.getSendUser()); sendMessageAll(msg.getMsg(), msg.getSendUser());
} else { } else if ("2".equals(msg.getMsgType())){
// 群聊
sendMessageGroup(msg.getMsg(), msg.getMsg());
}else if ("3".equals(msg.getMsgType())){
sendMessage(session, msg.getMsg()); sendMessage(session, msg.getMsg());
} }
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
...@@ -106,12 +112,31 @@ public class ChatController { ...@@ -106,12 +112,31 @@ public class ChatController {
} }
/** /**
* 群发 * 广播
* *
* @param message 发送的消息 * @param message 发送的消息
* @param username 发送人 * @param username 发送人
*/ */
public static void sendMessageAll(String message, String username) { public static void sendMessageAll(String message, String username) {
log.info("广播消息");
clients.forEach((key, session) -> {
if (!username.equals(key)) {
RemoteEndpoint.Async remote = session.getAsyncRemote();
if (remote == null) {
return;
}
remote.sendText(message);
}
});
}
/**
* 群聊
*
* @param message 发送的消息
* @param username 发送人
*/
public static void sendMessageGroup(String message, String username) {
log.info("群发消息"); log.info("群发消息");
clients.forEach((key, session) -> { clients.forEach((key, session) -> {
if (!username.equals(key)) { if (!username.equals(key)) {
...@@ -124,6 +149,7 @@ public class ChatController { ...@@ -124,6 +149,7 @@ public class ChatController {
}); });
} }
/** /**
* 单聊 * 单聊
* *
......
package com.stu.stusystem.controller.chat;
import com.stu.stusystem.service.chat.ChatMsgService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: cxt
* @time: 2021/6/15
*/
@RestController
@RequestMapping("/chat/msg")
public class ChatMsgController {
private ChatMsgService chatMsgService;
@ApiOperation("好友列表")
@GetMapping("/get")
public Object getFriends(@RequestParam("id") String id){
this.chatMsgService.getFriends(id);
return null;
}
@Autowired
public void setChatMsgService(ChatMsgService chatMsgService) {
this.chatMsgService = chatMsgService;
}
}
package com.stu.stusystem.mapper.chat;
import com.stu.stusystem.common.CommonMapper;
import com.stu.stusystem.model.chat.ChatMsg;
import com.stu.stusystem.service.system.vo.StuUserVO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author: cxt
* @time: 2021/6/15
*/
public interface ChatMsgMapper extends CommonMapper<ChatMsg> {
List<StuUserVO> getFriends(@Param("fId") String fId);
}
package com.stu.stusystem.model.chat; package com.stu.stusystem.model.chat;
import com.stu.stusystem.common.BaseModel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
...@@ -11,7 +12,9 @@ import lombok.NoArgsConstructor; ...@@ -11,7 +12,9 @@ import lombok.NoArgsConstructor;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class ChatMsg { public class ChatMsg extends BaseModel {
private String id;
// 消息内容 // 消息内容
private String msg; private String msg;
......
package com.stu.stusystem.service.chat;
import com.stu.stusystem.mapper.chat.ChatMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
/**
* @author: cxt
* @time: 2021/6/15
*/
@Service
public class ChatMsgService {
private ChatMsgMapper chatMsgMapper;
public void getFriends(String id) {
this.chatMsgMapper.getFriends(id);
}
@Autowired
public void setChatMsgMapper(ChatMsgMapper chatMsgMapper) {
this.chatMsgMapper = chatMsgMapper;
}
}
...@@ -3,10 +3,8 @@ ...@@ -3,10 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<property name="now" value="now()" dbms="h2"/>
<property name="now" value="now()" dbms="mysql"/> <property name="now" value="now()" dbms="mysql"/>
<property name="autoIncrement" value="true"/> <property name="autoIncrement" value="true"/>
<property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/> <property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<!-- <!--
Added the entity StuUser. Added the entity StuUser.
...@@ -45,6 +43,10 @@ ...@@ -45,6 +43,10 @@
<constraints nullable="true"/> <constraints nullable="true"/>
</column> </column>
<column name="head_img" type="varchar(22)" remarks="头像id">
<constraints nullable="true"/>
</column>
<column name="create_time" type="datetime" remarks="创建时间"> <column name="create_time" type="datetime" remarks="创建时间">
<constraints nullable="false"/> <constraints nullable="false"/>
</column> </column>
......
...@@ -3,10 +3,8 @@ ...@@ -3,10 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<property name="now" value="now()" dbms="h2"/>
<property name="now" value="now()" dbms="mysql"/> <property name="now" value="now()" dbms="mysql"/>
<property name="autoIncrement" value="true"/> <property name="autoIncrement" value="true"/>
<property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/> <property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<!-- <!--
Added the entity test. Added the entity test.
......
...@@ -3,10 +3,8 @@ ...@@ -3,10 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<property name="now" value="now()" dbms="h2"/>
<property name="now" value="now()" dbms="mysql"/> <property name="now" value="now()" dbms="mysql"/>
<property name="autoIncrement" value="true"/> <property name="autoIncrement" value="true"/>
<property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/> <property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<!-- <!--
Added the entity stu_class. Added the entity stu_class.
......
...@@ -3,10 +3,8 @@ ...@@ -3,10 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<property name="now" value="now()" dbms="h2"/>
<property name="now" value="now()" dbms="mysql"/> <property name="now" value="now()" dbms="mysql"/>
<property name="autoIncrement" value="true"/> <property name="autoIncrement" value="true"/>
<property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/> <property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<!-- <!--
Added the entity menu. Added the entity menu.
......
...@@ -3,10 +3,8 @@ ...@@ -3,10 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<property name="now" value="now()" dbms="h2"/>
<property name="now" value="now()" dbms="mysql"/> <property name="now" value="now()" dbms="mysql"/>
<property name="autoIncrement" value="true"/> <property name="autoIncrement" value="true"/>
<property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/> <property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<!-- <!--
Added the entity menu. Added the entity menu.
......
...@@ -3,10 +3,8 @@ ...@@ -3,10 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<property name="now" value="now()" dbms="h2"/>
<property name="now" value="now()" dbms="mysql"/> <property name="now" value="now()" dbms="mysql"/>
<property name="autoIncrement" value="true"/> <property name="autoIncrement" value="true"/>
<property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/> <property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<!-- <!--
Added the entity menu. Added the entity menu.
......
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<property name="now" value="now()" dbms="mysql"/>
<property name="autoIncrement" value="true"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<!--
Added the entity ChatFriends.
-->
<changeSet id="20210615-01" author="cxt">
<createTable tableName="chat_friends" remarks="好友表">
<column name="id" type="varchar(22)">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="f_id" type="varchar(22)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<property name="now" value="now()" dbms="mysql"/>
<property name="autoIncrement" value="true"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
<!--
Added the entity ChatMsg.
-->
<changeSet id="20210615-01" author="cxt">
<createTable tableName="chat_msg" remarks="聊天消息">
<column name="id" type="varchar(22)">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="msg" type="varchar(300)" remarks="消息内容">
<constraints nullable="false"/>
</column>
<column name="accept_user" type="varchar(22)" remarks="接收消息id">
<constraints nullable="false"/>
</column>
<column name="send_user" type="varchar(22)" remarks="发送消息id">
<constraints nullable="false"/>
</column>
<column name="send_type" type="varchar(2)" remarks="发送消息id">
<constraints nullable="false"/>
</column>
<column name="msg_type" type="varchar(2)" remarks="消息类型">
<constraints nullable="false"/>
</column>
<column name="create_time" type="datetime" remarks="创建时间">
<constraints nullable="false"/>
</column>
<column name="create_by" type="varchar(18)" remarks="创建人">
<constraints nullable="false"/>
</column>
<column name="update_time" type="datetime" remarks="更新时间">
<constraints nullable="true"/>
</column>
<column name="update_by" type="varchar(18)" remarks="更新人">
<constraints nullable="true"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
\ No newline at end of file
...@@ -10,5 +10,7 @@ ...@@ -10,5 +10,7 @@
<include file="classpath:config/liquibase/20210329_creat_table_menu.xml" relativeToChangelogFile="false"/> <include file="classpath:config/liquibase/20210329_creat_table_menu.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/20210331_creat_table_role.xml" relativeToChangelogFile="false"/> <include file="classpath:config/liquibase/20210331_creat_table_role.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/20210331_creat_table_role_jurisdiction.xml" relativeToChangelogFile="false"/> <include file="classpath:config/liquibase/20210331_creat_table_role_jurisdiction.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/20210615_creat_table_ChatFriends.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/20210615_creat_table_ChatMsg.xml" relativeToChangelogFile="false"/>
</databaseChangeLog> </databaseChangeLog>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.stu.stusystem.mapper.chat.ChatMsgMapper">
<select id="getFriends" resultType="com.stu.stusystem.service.system.vo.StuUserVO">
SELECT su.id, su.name, su.head_img AS headImg
FROM chat_friends cf
LEFT JOIN stu_user su ON cf.f_id = su.id
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册