java使用WebSocket实现一对一实时对话

websocket实现多用户一对一实时聊天 websocket+ajax在线聊天功能 阅读详情

最近的项目中有实时对话的需求。这里也是查阅了很多的资料。使用webSocket实现一对一对话是最多的。

链接: https://pan.baidu.com/s/1Vn1e1qw7nRnU1_4R-4fcGg

提取码: qwer 

逻辑讲解:

现在我们要给张三丰发一个你好,我们要解决下面两个问题

·  这个你好怎么展示在我的窗口
·  这个你好怎么展示在张三丰的窗口


第一个问题很好解决,我们的消息都是存在一个数组里面的,而我们前端是使用vue实现的,我们只需要在发消息的时候把新的消息push到这个数组里面去就行了。

我们要推送消息,我们只要知道要推送的session就好了,我们可以在连接的时候,把每一个session装进Map里面去,key使用userId。

前端拿到数据后,在websocketonmessage里面push到数组里面去就好了。

这样就实现了两个用户实时的聊天了。

当前会话存在就显示消息,当前会话不存在就显示消息+1

我们知道一个人可能和多个用户聊天,如果当前消息是该会话用户发的,那么我们的消息要显示出来(上面说了直接push到list里面),如果不是当前会话,我们就要显示消息+1。

引入依赖:此依赖封装了对实时对话的创建到取消链接

		<!-- websocket -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-websocket</artifactId>
		</dependency>

项目所需要的表:

 消息表

用来存每一个的对话消息,通过当前用户id,和点击会话列表,我们就和查询到当前会话的消息了。

CREATE TABLE `msg_info` (
  `id` int(6) NOT NULL AUTO_INCREMENT COMMENT '消息id',
  `from_user_id` int(6) NOT NULL COMMENT '消息发送者id',
  `from_user_name` varchar(50) NOT NULL COMMENT '消息发送者名称',
  `to_user_id` int(6) NOT NULL COMMENT '消息接收者id',
  `to_user_name` varchar(50) NOT NULL COMMENT '消息接收者名称',
  `content` varchar(200) NOT NULL COMMENT '消息内容',
  `create_time` datetime NOT NULL COMMENT '消息发送时间',
  `un_read_flag` int(1) NOT NULL COMMENT '是否已读(1 已读)',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=273 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='消息表';

 会话列表

A和B、A和C、A和D、B和A,都可以建立会话,我们需要把存在的会话都存在起来,这样用户一登陆就可以看到自己之前的会话列表了。

CREATE TABLE `session_list` (
  `id` int(6) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `user_id` int(6) NOT NULL COMMENT '所属用户',
  `to_user_id` int(6) NOT NULL COMMENT '到用户',
  `list_name` varchar(50) NOT NULL COMMENT '会话名称',
  `un_read_count` int(6) NOT NULL COMMENT '未读消息数',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='会话列表';

 用户表(一般只需要id和用户名建立通话即可,其他的如有需求自行添加)

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户名',
  `password` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '密码',
  `nick_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '昵称',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `sex` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '性别',
  `address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '地址',
  `avatar` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '头像',
  `account` decimal(10,2) DEFAULT 0.00 COMMENT '账户余额',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='用户信息表';

配置WebSocketConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig {

    /**
     * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

配置跨域


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    // 当前跨域请求最大有效时长。这里默认1天
    private static final long MAX_AGE = 24 * 60 * 60;

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
        corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
        corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
        corsConfiguration.setMaxAge(MAX_AGE);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }
}

 mybatis-plus配置   分页插件

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *  mybatis-plus 分页插件
 */
@Configuration
@MapperScan("com.jt.mapper")
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

Util工具类

CurPool 统一管理session、websocket、curUser(存储当前登录的用户)

package com.jt.util;

import com.jt.pojo.User;
import com.jt.websocket.WebSocket;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 统一管理session、websocket、curUser
 */
public class CurPool {

    //    public static CopyOnWriteArraySet<WebSocket> webSockets =new CopyOnWriteArraySet<>();
    public static Map<Integer, WebSocket> webSockets = new ConcurrentHashMap<>();
    // list 里面第一个存sessionId,第二个存session
    public static Map<Integer, List<Object>> sessionPool = new ConcurrentHashMap<>();
    // 当前登录用户x
    public static Map<String, User> curUserPool = new ConcurrentHashMap<>();
}

JsonUtils JSON转换工具类,其实可以用其他的

例如:alibba的JSON  gson的JSON

package com.jt.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * json 相关格式化
 */
public class JsonUtils {

    // 定义jackson对象
    private static ObjectMapper MAPPER = new ObjectMapper();

    {
        MAPPER.setSerializationInclusion(Include.ALWAYS);
    }

    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p >
     * <p>Description: </p >
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
        try {
            String string = MAPPER.writeValueAsString(data);
            return string;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JsonNode stringToJsonNode(String data) {
        try {
            JsonNode jsonNode = MAPPER.readTree(data);
            return jsonNode;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @return
     * @throws Exception
     */
    public static <T> T objectToPojo(Object jsonData, Class<T> beanType) throws Exception {
        T t = MAPPER.readValue(MAPPER.writeValueAsString(jsonData), beanType);
        return t;
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @return
     * @throws Exception
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) throws Exception {
        T t = MAPPER.readValue(jsonData, beanType);
        return t;
    }

    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p >
     * <p>Description: </p >
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            List<T> list = MAPPER.readValue(jsonData, javaType);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 将object数据转换成pojo对象list
     * <p>Title: jsonToList</p >
     * <p>Description: </p >
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T>List<T> objectToList(Object jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            List<T> list = MAPPER.readValue(MAPPER.writeValueAsString(jsonData), javaType);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 将JSON数据转换成Map
     * @param jsonData
     * @return
     */
    public static Map<String,Object> jsonToMap(String jsonData) {
        try {
            Map map = MAPPER.readValue(jsonData, Map.class);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

SpringContextUtil 重要:获取spring中的bean对象工具类,因为websocket中无法直接使用mapper

package com.jt.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 获取spring中的bean对象工具类
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    /**
     *  Spring应用上下文环境
     */
    private static ApplicationContext applicationContext;


    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 获取对象 这里重写了bean方法,起主要作用
     */
    public static Object getBean(String beanId) throws BeansException {
        return applicationContext.getBean(beanId);
    }
}

Controller

MsgInfoController   获取消息列表

package com.jt.controller;


import com.jt.mapper.MsgInfoMapper;
import com.jt.mapper.SessionListMapper;
import com.jt.pojo.MsgInfo;
import com.jt.pojo.SessionList;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MsgInfoController {

    @Resource
    private MsgInfoMapper msgInfoMapper;

    @Autowired
    private SessionListMapper sessionListMapper;

    // 消息列表
    @GetMapping("/msgList")
    public SysResult msgList(@RequestParam Integer sessionId){
        SessionList sessionList = sessionListMapper.selectByPrimaryKey(sessionId);
        if(sessionList == null){
            return SysResult.success();
        }
        Integer fromUserId = sessionList.getUserId();
        Integer toUserId = sessionList.getToUserId();
        List<MsgInfo> msgInfoList = msgInfoMapper.selectMsgList(fromUserId,toUserId);
        // 更新消息已读
        msgInfoMapper.msgRead(fromUserId, toUserId);
        // 更新会话里面的未读消息
        sessionListMapper.delUnReadCount(fromUserId, toUserId);
        return SysResult.success(msgInfoList);
    }


}

SessionController 创建会话、查询可建立的会话、查询已建立的会话

package com.jt.controller;


import com.jt.mapper.SessionListMapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.SessionList;
import com.jt.pojo.User;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SessionController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SessionListMapper seesionListMapper;

    /**
     * 已建立会话
     */
    @GetMapping("/sessionList/already")
    public SysResult sessionListAlready(@RequestParam Integer id){
        List<SessionList> sessionLists = seesionListMapper.selectByUserId(id);
        return  SysResult.success(sessionLists);
    }

    // 可建立会话的用户
    @GetMapping("/sessionList/not")
    public SysResult sessionListNot(@RequestParam Integer id){
        List<Integer> list = seesionListMapper.selectUserIdByUserId(id);
        list.add(id);
        List<User> cloudList = userMapper.getCloudList(list);
        return SysResult.success(cloudList);
    }

    // 创建会话
    @GetMapping("/createSession")
    public SysResult createSession(@RequestParam Integer id,@RequestParam Integer toUserId,@RequestParam String toUserName){
        SessionList sessionList = new SessionList();
        sessionList.setUserId(id);
        sessionList.setUnReadCount(0);
        sessionList.setListName(toUserName);
        sessionList.setToUserId(toUserId);
        seesionListMapper.insert(sessionList);
        // 判断对方和我建立会话没有? 没有也要建立
        Integer integer = seesionListMapper.selectIdByUser(toUserId, id);
        if (integer == null || integer <= 0){
            User user = userMapper.selectByPrimaryKey(id);
            sessionList.setUserId(toUserId);
            sessionList.setToUserId(id);
            sessionList.setListName(user.getName());
            seesionListMapper.insert(sessionList);
        }
        return SysResult.success();
    }

    // 删除会话
    @GetMapping("/delSession")
    public SysResult delSession(@RequestParam Integer sessionId){
        seesionListMapper.deleteByPrimaryKey(sessionId);
        return SysResult.success();
    }

}

UserController  用户注册、登录(登录时将用户存入池中)、注销(用户退出时,在池中清除)

package com.jt.controller;


import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.util.CurPool;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    // 注册用户
    @GetMapping("/register")
    public SysResult register(@RequestParam String name) {
        String s = userMapper.selectByName(name);
        if (s != null || "".equals(s)){
            return SysResult.fail().setMsg("改用户已存在");
        }
        User user = new User();
        user.setName(name);
        userMapper.insert(user);
        return SysResult.success();
    }

    // 登录
    @GetMapping("/login")
    public SysResult login(@RequestParam String name){

        if (CurPool.curUserPool.get(name) != null){
            return SysResult.fail().setMsg("该用户已登录");
        }
        User user = userMapper.selectUserByName(name);
        if (user == null || user.getId() == null){
            return SysResult.fail().setMsg("该用户不存在");
        }
        CurPool.curUserPool.put(user.getName(), user);
        return SysResult.success(user);
    }

    // 注销
    @GetMapping("/loginOut")
    public SysResult loginOut(@RequestParam String name){
        if (name != null && !"".equals(name)){
            CurPool.curUserPool.remove(name);
            User user = userMapper.selectUserByName(name);
            CurPool.sessionPool.remove(user.getId());
            CurPool.webSockets.remove(user.getId());
            System.out.println("【websocket消息】连接断开,总数为:"+CurPool.webSockets.size());
        }
        return SysResult.success();
    }


}

Mapper 

MsgInfoMapper

package com.jt.mapper;

import com.jt.pojo.MsgInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface MsgInfoMapper  {

    int insert(MsgInfo msgInfo);

    void msgRead(@Param("fromUserId") Integer fromUserId, @Param("toUserId") Integer toUserId);

    List<MsgInfo> selectMsgList(@Param("fromUserId") Integer fromUserId, @Param("toUserId") Integer toUserId);
}

 MsgInfoMapper.xml

<?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.jt.mapper.MsgInfoMapper">
    <resultMap id="BaseResultMap" type="com.jt.pojo.MsgInfo">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="from_user_id" jdbcType="INTEGER" property="fromUserId" />
        <result column="from_user_name" jdbcType="VARCHAR" property="fromUserName" />
        <result column="to_user_id" jdbcType="INTEGER" property="toUserId" />
        <result column="to_user_name" jdbcType="VARCHAR" property="toUserName" />
        <result column="content" jdbcType="VARCHAR" property="content" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="un_read_flag" jdbcType="INTEGER" property="unReadFlag" />
    </resultMap>
    <sql id="Base_Column_List">
        id, from_user_id, from_user_name, to_user_id,to_user_name, content, create_time, un_read_flag
    </sql>

    <insert id="insert" parameterType="com.jt.pojo.MsgInfo"  useGeneratedKeys="true" keyProperty="id">
        insert into msg_info (id, from_user_id, from_user_name,
                              to_user_id, to_user_name, content, create_time,
                              un_read_flag)
        values (#{id,jdbcType=INTEGER}, #{fromUserId,jdbcType=INTEGER}, #{fromUserName,jdbcType=VARCHAR},
                #{toUserId,jdbcType=INTEGER},#{toUserName,jdbcType=VARCHAR},  #{content,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
                #{unReadFlag,jdbcType=INTEGER})
    </insert>



    <update id="msgRead">
        UPDATE msg_info SET un_read_flag = 1 WHERE from_user_id = #{fromUserId} AND to_user_id = #{toUserId}
    </update>

    <select id="selectMsgList" resultMap="BaseResultMap">
        SELECT * FROM msg_info
        WHERE (
                      (to_user_id = #{toUserId} AND from_user_id = #{fromUserId})
                      OR (to_user_id = #{fromUserId} AND from_user_id = #{toUserId})
                  )
    </select>
</mapper>

SessionListMapper

package com.jt.mapper;


import com.jt.pojo.SessionList;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface SessionListMapper {

    int deleteByPrimaryKey(Integer id);

    int insert(SessionList sessionList);

    SessionList selectByPrimaryKey(Integer id);

    List<Integer> selectUserIdByUserId(Integer id);

    List<SessionList> selectByUserId(Integer id);

    Integer selectIdByUser(@Param("fromId") Integer fromId,@Param("toId") Integer toId);


    void addUnReadCount(@Param("userId") Integer userId,@Param("toUserId") Integer toUserId);

    void delUnReadCount(@Param("fromUserId") Integer fromUserId,@Param("toUserId") Integer toUserId);
}

 SessionListMapper.xml

<?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.jt.mapper.SessionListMapper">
    <resultMap id="BaseResultMap" type="com.jt.pojo.SessionList">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="user_id" jdbcType="INTEGER" property="userId" />
        <result column="to_user_id" jdbcType="INTEGER" property="toUserId" />
        <result column="list_name" jdbcType="VARCHAR" property="listName" />
        <result column="un_read_count" jdbcType="INTEGER" property="unReadCount" />
    </resultMap>
    <sql id="Base_Column_List">
        id, user_id,to_user_id, list_name, un_read_count
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from session_list
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from session_list
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" parameterType="com.jt.pojo.SessionList">
        insert into session_list (id, user_id, to_user_id,list_name,
                                  un_read_count)
        values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{toUserId,jdbcType=INTEGER}, #{listName,jdbcType=VARCHAR},
                #{unReadCount,jdbcType=INTEGER})
    </insert>



    <select id="selectUserIdByUserId" resultType="java.lang.Integer">
        SELECT to_user_id FROM session_list WHERE user_id = #{id}
    </select>

    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT * FROM session_list WHERE user_id = #{id}
    </select>

    <select id="selectIdByUser" resultType="java.lang.Integer">
        SELECT id FROM session_list WHERE user_id = #{fromId} AND to_user_id = #{toId}
    </select>



    <update id="addUnReadCount">
        UPDATE session_list SET un_read_count = un_read_count + 1 WHERE user_id = #{userId} AND to_user_id = #{toUserId}
    </update>

    <update id="delUnReadCount">
        UPDATE session_list SET un_read_count = 0 WHERE user_id = #{fromUserId} AND to_user_id = #{toUserId}
    </update>
</mapper>

UserMapper

package com.jt.mapper;

import cn.hutool.db.Page;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserMapper {

    int insert(User user);

    User selectByPrimaryKey(Integer id);


    String selectByName(String name);

    List<User> getCloudList(@Param("list") List<Integer> list);

    User selectUserByName(@Param("name") String name);

}

 UserMapper.xml

<?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.jt.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.jt.pojo.User">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
    </resultMap>
    <sql id="Base_Column_List">
        id, `name`
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from user
        where id = #{id,jdbcType=INTEGER}
    </select>


    <insert id="insert" parameterType="com.jt.pojo.User">
        insert into user (id, `name`)
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
    </insert>



    <select id="selectByName" resultType="java.lang.String">
        SELECT name FROM user WHERE name = #{name}
    </select>

    <select id="getCloudList" resultMap="BaseResultMap">
        SELECT id,name FROM user
        <if test="list != null">
            WHERE id not in
            <foreach collection="list" open="(" close=")" separator="," item="i">
                #{i}
            </foreach>
        </if>
    </select>

    <select id="selectUserByName" resultMap="BaseResultMap" parameterType="java.lang.String">
        SELECT id,name FROM user WHERE name = #{name}
    </select>
</mapper>

 连接建立成功调用的方法                onOpen

连接关闭调用的方法                        onClose

收到客户端消息后调用的方法          onMessage

发生错误时调用                                onError

package com.jt.websocket;


import com.jt.mapper.MsgInfoMapper;
import com.jt.mapper.SessionListMapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.MsgInfo;
import com.jt.pojo.SessionList;
import com.jt.pojo.User;
import com.jt.util.CurPool;
import com.jt.util.JsonUtils;
import com.jt.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@ServerEndpoint("/websocket/{userId}/{sessionId}")
//此注解相当于设置访问URL
public class WebSocket {

    @Autowired
    private SessionListMapper sessionListMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MsgInfoMapper msgInfoMapper;

    private Session session;

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session,@PathParam(value="userId")Integer userId, @PathParam(value="sessionId")String sessionId) {
        this.session = session;
        CurPool.webSockets.put(userId,this);
        List<Object> list = new ArrayList<>();
        list.add(sessionId);
        list.add(session);
        CurPool.sessionPool.put(userId , list);
        System.out.println("【websocket消息】有新的连接,总数为:"+CurPool.webSockets.size());
    }
    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        // 断开连接删除用户删除session
        Integer userId = Integer.parseInt(this.session.getRequestParameterMap().get("userId").get(0));
        CurPool.sessionPool.remove(userId);
        CurPool.webSockets.remove(userId);
        if (userMapper == null){
            this.userMapper = (UserMapper) SpringContextUtil.getBean("userMapper");
        }
        User user = userMapper.selectByPrimaryKey(userId);
        CurPool.curUserPool.remove(user.getName());
        System.out.println("【websocket消息】连接断开,总数为:"+CurPool.webSockets.size());
    }
    /**
     * 收到客户端消息后调用的方法
     * 后台收到客户端发送过来的消息
     * onMessage 是一个消息的中转站
     * @param message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message) {

        String sessionId = this.session.getRequestParameterMap().get("sessionId").get(0);
        if (sessionId == null){
            System.out.println("sessionId 错误");
        }
        // 在这里无法注入Mapper所以使用这种方式注入Mapper
        if (sessionListMapper == null){
            this.sessionListMapper = (SessionListMapper)SpringContextUtil.getBean("sessionListMapper");
        }
        if (userMapper == null){
            this.userMapper = (UserMapper)SpringContextUtil.getBean("userMapper");
        }
        if (msgInfoMapper == null){
            this.msgInfoMapper = (MsgInfoMapper)SpringContextUtil.getBean("msgInfoMapper");
        }
        SessionList sessionList = sessionListMapper.selectByPrimaryKey(Integer.parseInt(sessionId));
        User user = userMapper.selectByPrimaryKey(sessionList.getUserId());
        MsgInfo msgInfo = new MsgInfo();
        msgInfo.setContent(message);
        msgInfo.setCreateTime(new Date());
        msgInfo.setFromUserId(sessionList.getUserId());
        msgInfo.setFromUserName(user.getName());
        msgInfo.setToUserId(sessionList.getToUserId());
        msgInfo.setToUserName(sessionList.getListName());
        msgInfo.setUnReadFlag(0);
        // 消息持久化
        msgInfoMapper.insert(msgInfo);

        // 判断用户是否存在,不存在就结束
        List<Object> list = CurPool.sessionPool.get(sessionList.getToUserId());
        if (list == null || list.isEmpty()){
            // 用户不存在,更新未读数
            sessionListMapper.addUnReadCount(sessionList.getToUserId(),sessionList.getUserId());
        }else{
            // 用户存在,判断会话是否存在
            String id = sessionListMapper.selectIdByUser(sessionList.getToUserId(), sessionList.getUserId())+"";
            String o = list.get(0) + "";
            if (id.equals(o)){
                // 会话存在直接发送消息
                sendTextMessage(sessionList.getToUserId(), JsonUtils.objectToJson(msgInfo));
            }else {
                // 判断会话列表是否存在
                if (id == null || "".equals(id) || "null".equals(id)){
                    // 新增会话列表
                    SessionList tmpSessionList = new SessionList();
                    tmpSessionList.setUserId(sessionList.getToUserId());
                    tmpSessionList.setToUserId(sessionList.getUserId());
                    tmpSessionList.setListName(user.getName());
                    tmpSessionList.setUnReadCount(1);
                    sessionListMapper.insert(tmpSessionList);
                }else {
                    // 更新未读消息数量
                    sessionListMapper.addUnReadCount(sessionList.getToUserId(),sessionList.getUserId());
                }
                // 会话不存在发送列表消息
                List<SessionList> sessionLists = sessionListMapper.selectByUserId(sessionList.getToUserId());
                sendTextMessage(sessionList.getToUserId() ,JsonUtils.objectToJson(sessionLists));
            }
        }
        System.out.println("【websocket消息】收到客户端消息:"+message);
    }

    @OnError
    public void onError(Session session, Throwable error) {
        log.error("发生错误");
        error.printStackTrace();
    }

    // 此为广播消息
//    public void sendAllMessage(String message) {
//        for(WebSocket webSocket : webSockets) {
//            System.out.println("【websocket消息】广播消息:"+message);
//            try {
//                webSocket.session.getAsyncRemote().sendText(message);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }

    // 此为单点消息 (发送文本)
    public void sendTextMessage(Integer userId, String message) {
        Session session = (Session)CurPool.sessionPool.get(userId).get(1);
        if (session != null) {
            try {
                session.getBasicRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 此为单点消息 (发送对象)
//    public void sendObjMessage(String sessionId, Object message) {
//        Session session = CurPool.sessionPool.get(sessionId);
//        if (session != null) {
//            try {
////                session.getAsyncRemote().sendObject(message);
//                session.getBasicRemote().sendText(JsonUtils.objectToJson(message));
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }

}

前端vue调用:

<template>
 <div class="main"> 
  <div class="main_up">
   <div v-if="curUserName == ''">
    <el-button @click="openDialog('登录')">登录</el-button>
    <el-button @click="openDialog('注册')">注册</el-button>
   </div>
   <div v-else>
    {{this.curUserName}}
    <el-button type="info" @click="loginOut">退出登录</el-button>
   </div>
   
  </div>
  <div class="main_down">
   <div class="left">
    <div class="left_up">
     <div class="label_title">
      已建立会话
     </div>
     <div :class="curSessionId == item.id ? 'box_select' : 'box'" v-for="item in sessionList_already" :key="item.id">
      <div class="box_left"  @click="startSession(item.id)">
       {{item.listName}}
      </div>
      <div class="right_left">
       <div class="right_left_count">
        {{item.unReadCount}}
       </div>
       <div class="right_left_del">
        <i class="el-icon-close" @click="delSession(item.id)"></i>
       </div>
      </div>
     </div>
    </div>
    <div class="left_down">
     <div class="label_title">
      可建立会话
     </div>
     <div v-for="item in sessionList_not" :key="item.id" class="box" @click="createSession(item.id, item.name)">
      <div class="box_left">
       {{item.name}}
      </div>
     </div> 
    </div>

   </div>
   <div class="right">
    <div class="up" ref="element" id="msg_end">
     <div v-for="(item,i) in list" :key="i" :class="item.fromUserId === curUserId ? 'msg_right' : 'msg_left'">
      <div class="msg_right_up">
       {{item.fromUserName}}
      </div>
      <div :class="item.fromUserId === curUserId ? 'msg_right_down' : 'msg_left_down'">
       {{item.content}}
      </div>
     </div>
     
    </div>
    <div class="down">
     <el-input
     type="textarea"
     :rows="9"
     placeholder="请输入内容,回车发送!"
     @keyup.enter.native = "sendMsg"
     v-model="textarea">
     </el-input>

     <!-- <el-button  @click="sendMsg">发送</el-button> -->
    </div>
   </div>
  </div>

  <el-dialog
   :title="dialogTitle"
   :visible.sync="dialogVisible"
   width="30%"
   >
   <el-input v-model="loginName" placeholder="请输入用户名..."></el-input>
   <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="loginOrRegister">确 定</el-button>
   </span>
  </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
export default {
 
 name: 'HelloWorld',
 data(){
  return{
   dialogVisible: false,
   dialogTitle: '',
   loginName: '',
   textarea: "",
   list: [],
   curUserId: "",
   curUserName: "",
   curSessionId: '',
   sessionList_already:[],
   sessionList_not:[],
  }
 },
 created() { // 页面创建生命周期函数              
  
 },     
 updated(){
  // 解决每次发消息到最后面
  var elmnt = document.getElementById("msg_end");
  elmnt.scrollTop = elmnt.scrollHeight;
 },   
 destroyed: function () { // 离开页面生命周期函数              
  this.websocketclose();        
 },
 methods: {            
  initWebSocket: function (userId,sessionId) {                
   // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https                
   this.websock = new WebSocket("ws://127.0.0.1:9090/websocket/"+userId+"/"+sessionId);                
   this.websock.onopen = this.websocketonopen;                
   this.websock.onerror = this.websocketonerror;                
   this.websock.onmessage = this.websocketonmessage;                
   this.websock.onclose = this.websocketclose;
  },              
  websocketonopen: function () {                
   console.log("WebSocket连接成功");    
  },              
  websocketonerror: function (e) { 
   console.log("WebSocket连接发生错误",e);              
  },              
  websocketonmessage: function (e) {  
   let data = JSON.parse(e.data);
   if(data instanceof Array){
    // 列表数据
    this.sessionList_already = data
   }else{
    // 消息数据
    this.list.push(data)
   }
  },              
  websocketclose: function (e) {
   if(this.curUserId != null){
    if(this.curSessionId != null){
     this.initWebSocket(this.curUserId, this.curSessionId)
    }else{
     this.initWebSocket(this.curUserId, 99999999)
    }
   }
   console.log("connection closed",e);     
   console.log(e);              
  },
  // 消息发送
  sendMsg(){
   if(this.curSessionId == ''){
    return this.$message.error("请选择左边的对话框开始聊天!")
   }
   let data = {
    "fromUserId": this.curUserId,
    "fromUserName": this.curUserName,
    "content": this.textarea
   }
   this.list.push(data)
   this.websock.send(this.textarea)
   this.textarea = ''
  },
  openDialog(openName){
   this.dialogTitle = openName
   this.dialogVisible = true
  },
  // 登录or注册
  loginOrRegister(){
   let thus = this
   if(this.dialogTitle == "注册"){
    axios.get('http://127.0.0.1:9090/register?name=' + this.loginName)
    .then(function (response) {
     if(response.data.code == -1){
      return thus.$message.error(response.data.errDesc);
     }
     thus.$message.success("注册成功");
     thus.dialogVisible = false
    })
    .catch(function (error) {
     console.log(error);
    });
   }else if(this.dialogTitle == '登录'){
    axios.get('http://127.0.0.1:9090/login?name=' + this.loginName)
    .then(function (response) {
     console.log(response.data);
     if(response.data.code == -1){
      return thus.$message.error(response.data.errDesc);
     }
     thus.curUserId = response.data.data.id
     thus.curUserName = response.data.data.name
     thus.dialogVisible = false
     thus.getSessionListNot()
     thus.sessionListAlready()
     thus.startSession(99999999)
    })
    .catch(function (error) {
     console.log(error);
    });
   }
  },
  // 获取可建立会话列表
  getSessionListNot(){
   let thus = this
   axios.get('http://127.0.0.1:9090/sessionList/not?id=' + this.curUserId)
   .then(function (response) {
    if(response.data.code == -1){
     return thus.$message.error(response.data.errDesc);
    }
    thus.sessionList_not = response.data.data
   })
   .catch(function (error) {
    console.log(error);
   });
  },
  // 获取已存在的会话列表
  sessionListAlready(){
   let thus = this
   axios.get('http://127.0.0.1:9090/sessionList/already?id=' + this.curUserId)
   .then(function (response) {
    if(response.data.code == -1){
     return thus.$message.error(response.data.errDesc);
    }
    thus.sessionList_already = response.data.data
   })
   .catch(function (error) {
    console.log(error);
   });
  },
  // 创建会话
  createSession(toUserId, toUserName){
   let thus = this
   axios.get('http://127.0.0.1:9090/createSession?id=' + this.curUserId + "&toUserId=" + toUserId + "&toUserName=" + toUserName)
   .then(function (response) {
    if(response.data.code == -1){
     return thus.$message.error(response.data.errDesc);
    }
    thus.getSessionListNot()
    thus.sessionListAlready()
   })
   .catch(function (error) {
    console.log(error);
   });
  },
  // 开始会话
  startSession(sessionId){
   console.log(this.websock);
   if(this.websock != undefined){
    this.websock.close()
   }
   this.curSessionId = sessionId
   this.initWebSocket(this.curUserId, sessionId)
   this.msgList(sessionId)
  },
  // 删除会话
  delSession(sessionId){
   let thus = this
   axios.get('http://127.0.0.1:9090/delSession?sessionId=' + sessionId)
   .then(function (response) {
    if(response.data.code == -1){
     return thus.$message.error(response.data.errDesc);
    }
    thus.getSessionListNot()
    thus.sessionListAlready()
   })
   .catch(function (error) {
    console.log(error);
   });
  },
  // 退出登录
  loginOut(){
   let thus = this
   axios.get('http://127.0.0.1:9090/loginOut?name=' + this.curUserName)
   .then(function (response) {
    if(response.data.code == -1){
     return thus.$message.error(response.data.errDesc);
    }
    thus.curUserId = ''
    thus.curUserName = ''
    return thus.$message.success("退出登录成功");
   })
   .catch(function (error) {
    console.log(error);
   });
  },
  // 获取消息数据
  msgList(sessionId){
   let thus = this
   axios.get('http://127.0.0.1:9090/msgList?sessionId=' + sessionId)
   .then(function (response) {
    if(response.data.code == -1){
     return thus.$message.error(response.data.errDesc);
    }
    thus.list = response.data.data
    // 从新获取列表
    thus.sessionListAlready()
   })
   .catch(function (error) {
    console.log(error);
   });
 
  }
 }    
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 .main{
  width: 980px;
  /* border: 1px #1890ff solid; */
  margin-top: 50px;
  height: 790px;
 }
 .main_up{
  width: 980px;
  height: 40px;
  /* border:1px red solid; */
 }
 .main_down{
  width: 981px;
  height: 750px;
  border: 1px #1890ff solid;
  display: flex;
  justify-self: space-between;
 }
 .left{
  width: 300px;
  height: 750px;
  border-right: 1px #1890ff solid;
 }
 .left_up{
  width: 300px;
  height: 450px;
  overflow-y: auto;
  /* border: 1px red solid; */
 }
 .label_title{
  width: 282px;
  height: 25px;
  background-color: #f8f8f8;
  font-weight: 600;
  padding: 8px;
 }
 .left_down{
  width: 300px;
  height: 300px;
  overflow-y: auto;
  /* border: 1px green solid; */
 }
 .right{
  width: 680px;
  height: 750px;
  /* border-right: 1px #1890ff solid; */
 }
 .box{
  width: 250px;
  height: 22px;
  padding: 10px 25px 10px 25px;
  display: flex;
  justify-self: flex-end;
  /* border: 1px red solid; */
 }
 .box:hover{
  background-color: gainsboro;
  cursor: pointer;
 }
 .box_select{
  width: 250px;
  height: 22px;
  padding: 10px 25px 10px 25px;
  display: flex;
  justify-self: flex-end;
  background-color: gainsboro;
 }
 .box_left{
  width: 230px;
  height: 22px;
 }
 .right_left{
  width: 50px;
  height: 22px;
  display: flex;
  justify-content: space-between;
  /* border: 1px red solid; */
 }
 .right_left_count{
  /* border: 1px blue solid; */
  padding-left: 10px;
  width: 20px;
 }
 .right_left_del{
  width: 20px;
  padding-left: 10px;
 }
 .up{
  width: 680px;
  height: 550px;
  overflow-y: scroll;
  overflow-x: hidden;
  /* padding-bottom: 40px; */
  border-bottom: 1px #1890ff solid;
 }
 .msg_left{
  width: 675px;
  /* padding-left: 5px; */
  margin-top: 5px;
  /* border: 1px #1890ff solid; */
 }
 .msg_left_up{
  height: 25px;
  margin-top: 5px;
 }
 .msg_left_down{
  height: 25px;
  /* border: 1px #1890ff solid; */
  padding-left: 10px;
 }
 .msg_right{
  width: 660px;
  /* padding-right: 20px; */
  margin-top: 5px;
  /* border: 1px #1890ff solid; */
  text-align: right;
 }
 .msg_right_up{
  height: 25px;
  
 }
 .msg_right_down{
  height: 25px;
  /* border: 1px #1890ff solid; */
  padding-right: 10px;
 }
 .down{
  width: 680px;
  height: 200px;
  /* border: 1px red solid; */
 }
</style>

b2fa5c23d4fd40619320ed04b70ad32b.png

 942308e290054955ad3ea82a88a32eab.png

实现发送语音

生成音频文件,并进行保存上传到服务器中。然后将文件名字保存到数据库中,聊天时前端判断返回的数据是否为语音,如果为语音则直接根据名字调用音频文件获得语音内容。

1.用户点击发送语音,保存语音到服务器上,并保存名字到数据库中

2.接收语音时获得音频文件即可

3.前端根据获得的音频文件进行展示即可。

 

package com.jt.controller;

import com.jt.util.FtpUtil;

import javax.sound.sampled.*;
import java.io.*;

public class EngineeCore {

    String filePath = timeNumber()+".mp3";//要存储文件的位置

    AudioFormat audioFormat;
    TargetDataLine targetDataLine;
    boolean flag = true;

    public static void main(String[] args) {
        EngineeCore engineeCore = new EngineeCore();  //调用麦克风获取音频

        engineeCore.startRecognize();
    }

    private void stopRecognize() {
        flag = false;
        targetDataLine.stop();
        targetDataLine.close();
    }private AudioFormat getAudioFormat() {
        float sampleRate = 16000;
        // 8000,11025,16000,22050,44100
        int sampleSizeInBits = 16;
        // 8,16
        int channels = 1;
        // 1,2
        boolean signed = true;
        // true,false
        boolean bigEndian = false;
        // true,false
        return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    }// end getAudioFormat


    private void startRecognize() {
        try {
            // 获得指定的音频格式
            audioFormat = getAudioFormat();
            DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
            targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);

            // Create a thread to capture the microphone
            // data into an audio file and start the
            // thread running. It will run until the
            // Stop button is clicked. This method
            // will return after starting the thread.
            flag = true;
            new CaptureThread().start();
        } catch (Exception e) {
            e.printStackTrace();
        } // end catch
    }// end captureAudio method

    class CaptureThread extends Thread {
        public void run() {
            AudioFileFormat.Type fileType = null;

            File audioFile = new File(filePath);

            fileType = AudioFileFormat.Type.WAVE;
            //声音录入的权值
            int weight = 2;
            //判断是否停止的计数
            int downSum = 0;

            ByteArrayInputStream bais = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            AudioInputStream ais = null;
            try {
                targetDataLine.open(audioFormat);
                targetDataLine.start();
                byte[] fragment = new byte[1024];

                ais = new AudioInputStream(targetDataLine);
                while (flag) {

                    targetDataLine.read(fragment, 0, fragment.length);
                    //当数组末位大于weight时开始存储字节(有声音传入),一旦开始不再需要判断末位
                    if (Math.abs(fragment[fragment.length-1]) > weight || baos.size() > 0) {
                        baos.write(fragment);
                        System.out.println("守卫:"+fragment[0]+",末尾:"+fragment[fragment.length-1]+",lenght"+fragment.length);
                        //判断语音是否停止
                        if(Math.abs(fragment[fragment.length-1])<=weight){
                            downSum++;
                        }else{
                            System.out.println("重置奇数");
                            downSum=0;
                        }//计数超过20说明此段时间没有声音传入(值也可更改)
                        if(downSum>20){
                            System.out.println("停止录入");
                            break;
                        }

                    }
                }

                //取得录音输入流
                audioFormat = getAudioFormat();
                byte audioData[] = baos.toByteArray();
                bais = new ByteArrayInputStream(audioData);
                ais = new AudioInputStream(bais, audioFormat, audioData.length / audioFormat.getFrameSize());
                //定义最终保存的文件名
                System.out.println("开始生成语音文件");
                AudioSystem.write(ais, AudioFileFormat.Type.WAVE, audioFile);
                downSum = 0;
                stopRecognize();

                //上传到服务器中
                BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(filePath));
                boolean b = FtpUtil.uploadFileFTP("111.111.111.111", 21, "ftpuser", "ftpuser", "/www/wwwroot/ftpuser", timeNumber()+".mp3", buffer);
                System.out.println("b = " + b);

                boolean delete = audioFile.delete();//删除本地文件
                System.out.println("delete = " + delete);
                
                buffer.close();
                
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭流

                try {
                    ais.close();
                    bais.close();
                    baos.reset();
                    
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    //获得当前时间戳
    public Long timeNumber(){
        long l = System.currentTimeMillis();
        return l;
    }
}

WebSocket开发(一对一聊天)功能 从0开始基于WebSocket建立easy-chatroom,本篇为websocket实现一对一私聊和统计在线人数等功能。下面代码都是基于上篇文章中的代码进行的修改,开始的步骤中为了一步步的循环渐进所以只展示修改部分的代码,如果有感觉不连贯的同学可以完成上篇文章后再按本篇步骤执行,完整代码会在最后贴出。 阅读详情

相关推荐

Java中的WebSocket编程:实现实时通信

WebSocket是一种在单个TCP连接上进行全双工通信的协议,它允许服务器主动向客户端发送数据,非常适合需要实时数据更新的应用场景,如在线聊天、实时通知、在线游戏等。WebSocket是实现实时通信的强大工具,它能够极大地提升用户体验,尤其在需要实时更新的应用中非常实用。在这篇文章中,我们将通过Spring Boot来演示如何在Java中实现WebSocket,并展示如何通过WebSocket实现客户端与服务端的实时通信。为了测试WebSocket的实时通信,我们需要一个简单的前端页面与服务器进行通信。

聚娃科技开发者博客 2085

Java实现在线聊天

注:可以直接看整合javat-io是基于java开发的一个开源的网络编程架构,大家都知道现在手机上或者电脑上都装了很多APP,这些APP都不是一个个在手机上或电脑上孤立的使用,而是能访问其他的地方数据或者与其他节点进行实时聊天,故每个APP中都要有一个对外进行通信的模块,这块内容从编程的角度来看就能通过网络编程框架来实现,t-io就是完成这块的内容的最好的编程软件。...............

学习学习学习 1万+

Java websocket + redis 实现多人单聊天室,多人多聊天室, 一对一聊天

多人,单聊天室版 FEATURE 多人聊天, 界面简洁美观, 使用ueditor支持发送文字,图片信息群聊成员列表, 登入登出公告存储聊天记录, 查看历史消息 技术点 使用CopyOnWriteMap存储websocketServer对象,线程安全redis存储消息记录ConcurrentLinkedQueue存储聊天成员 TODO 没有处理高并发,高并发情

timeless的博客 1万+

JAVA中webSockt一对一聊天

java 实现 一对一 通信聊天

IT小郭的技术博客 1671

Springboot + websocket 实现 一对一 单人聊天

websocket 简单聊天

射手座的程序媛的专栏 1623

WebSocket服务器的完美解决方案——解决WebSocket会话问题

WebSocket会话是指服务器与客户端之间的持久化连接,它允许双方在一个会话中进行多次通信。在本文中,我们将讨论如何解决WebSocket会话问题,并提供相应的源代码示例。通过使用Tornado框架和适当的逻辑处理,我们可以轻松地建立稳定的WebSocket服务器,并处理会话断开的情况。Tornado提供了强大的异步IO功能,可以处理大量并发连接,并提供了易于使用的WebSocket接口。当检测到会话关闭时,我们可以采取一些措施来重新建立连接,比如向客户端发送重新连接的请求或执行其他逻辑操作。

IsdCoding的博客 504

java websocket一对一_【笔记】websockt一对一聊天java部分

@Configuration("otcChatWebSocketConfig")@EnableWebSocketpublic class OtcChatWebSocketConfig implements WebSocketConfigurer {public static final Logger log = LoggerFactory.getLogger(OtcChatWebSocketCon...

weixin_39661405的博客 247

SpringBoot实现WebSocket实现用户一对一和一对多信息的发送

下面是我的一个xml配置,可以一下:只是一些非常简单的maven,主要是为了简单的学习websocket在springboot方面该怎么实现消息的发送和接收。下面的图片连接和发送的结果:(这里面的逻辑可以在后端修改,通过这个发送的信息默认调用@onMessage注解标注的方法,在端点类里面)service:(因为端点类里也注入了这个service,所以里面有一些端点类的实现,当然也有controller类的实现)我们点击连接用户1和用户2,用户1的id为1001,用户2的id为1002.

LYH2771994122的博客 969

spring boot Java实现网页一对一 一对多实时聊天 webSocket

Java实现网页一对一 一对多实时聊天websocket 后台代码 最重要的类 package com.socket.websocket.chat; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org....

weixin_44604118的博客 4537

Vivado找不到板载Flash型号?手把手教你修改配置文件添加W25Q128FV

本文详细解析了Vivado工具链中Flash芯片配置文件的修改方法,手把手教你如何添加W25Q128FV型号支持。从配置文件体系结构到实战操作步骤,涵盖CSV字段详解、烧写验证及常见问题排查,帮助开发者解决Vivado找不到板载Flash型号的问题,提升FPGA开发效率。

weixin_42524276的博客 405

强化学习赋能宇树机器狗:Isaac Gym与MuJoCo仿真训练全解析

本文详细解析了如何利用Isaac Gym和MuJoCo两大仿真平台为宇树机器狗构建强化学习训练管道。内容涵盖环境配置、模型导入、PPO算法框架设计、奖励函数构建以及从仿真到实机的策略迁移技巧,为开发者提供了一套完整的宇树狗仿真训练实践方案。

zeta9的博客 631

SpringBoot使用WebSocket实现简单的网页一对一聊天功能

温故而知新,可以为师矣。 ——孔子 SpringBoot使用WebSocket实现简单的网页一对一聊天功能。 WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议。 WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在 WebSocket API 中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。

爱辰 3397

Python处理高光谱数据#3 降维

基于Python的高光谱降维操作 高光谱数据:点击此处可下载 用到的库:matplotlib、scipy、spectral 主要内容:主成分分析(PCA)与线性判别(LDA) 欢迎有兴趣的朋友交流指点。最后,废话不多说直接上代码???? import matplotlib.pyplot as plt from scipy.io import loadmat import spectral as spy

qq_273058791的博客 6944

SpringBoot+Mybatis-Plus使用webSocket实现一对一聊天

SpringBoot+Mybatis-Plus使用webSocket实现一对一聊天 一、WebSocket WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议。 WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在 WebSocket API 中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。 在 WebSocket API 中,浏览器和服务器只需要做一个握手的动作

w75545521的博客 2006

富士SC2022,C325,C328打印机扫描到网络详细教程

富士打印机SC2022,C328,C325等型号扫描到电脑教程

weixin_41926602的博客 1万+

vectorCast——Probe point 功能实现故障注入,局部变量打印,断点调试。

选择一个测试用例,选择coverage窗口进行查看。点击edit probe point,如图所示绿色的小圆圈。 选代码中选择需要打断点的地方进行点击。黑色的小圆点都可以选。 点击黑色小圆点,小圆点变绿,表示打断点成功。 此时就可以根据自己的需求在打断点的位置编写一些C语言的命令语句。 点击编译,成功会弹出窗口显示编译成功,失败会在下方报错error,并显示错误的原因。 点击应用保存,选择yes to all就可以了。 点击用例执行就可以看到打断点情况下的执行结果了。

weixin_59334478的博客 2080

使用 WebSocket 实现实时聊天

在现代网络应用中,实时聊天功能已经成为用户体验的重要组成部分。无论是社交媒体平台、在线客服系统还是多人在线游戏,实时聊天功能都为用户提供了即时沟通的便利。实现实时聊天的技术有很多种,其中 WebSocket 是最受欢迎和高效的解决方案之一。本文将介绍如何使用 WebSocket 实现一个简单的实时聊天应用,包括 WebSocket 的基本概念、服务端和客户端的实现、以及一些可能的优化和扩展。WebSocket 是一种在单个 TCP 连接上进行全双工、双向交互的协议。

私聊前往站内信:https://i.csdn.net/#/msg/chat/weixin_44976692 9167

49、非合作目标运动参数估计与状态滤波方法研究

本文研究了非合作空间目标的运动参数估计与状态滤波方法。通过分析目标的章动运动特性,建立了运动方程,并分别提出了基于最小二乘法的质心运动参数估计方法和旋转运动参数估计方法。同时,设计了误差状态卡尔曼滤波器(ESKF)用于姿态滤波和角速度估计,以提高估计精度。仿真实验验证了所提方法的有效性,能够准确估计目标的运动参数和状态信息,为在轨服务提供了技术支持。

gpt4scribbler的博客 95

WebSocket一对一聊天功能,springboot项目

一个springboot集成websocket一对一聊天的小demo websocket所需的jar包 <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> ...

qq_42240818的博客 5846

C#项目实战——YModem协议文件传输【实例】

1、先由上位机发送*update*的握手信号,下位机返回响应字符‘C’(字符C,ASII码为0x43),之后通信双方开始以YMODEM协议进行数据通信。2、 YMODEM协议格式说明(1)起始帧数据格式帧类型(1byte)帧序号帧序号反码传输的文件名文件大小数据CRCSOH00FFfilenamefileziseNULCRCSOH=0x01,表示这个数据帧中包含着128个字节的数据(STX表示1024字节,初始帧只有128个);00表示数据帧序号,初始是0,依次向下排。

做有价值的事,积累有价值的笔记! 3661

2023推免(夏令营+套实验室)面试总结(博文附问题汇总和参考答案)

2023推免(夏令营+套实验室)面试总结(博文附问题汇总和参考答案)

m0_51339444的博客 1819

基于webSocket实现的一对一在线聊天系统

简单介绍一下websocket(来自网络):随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了。近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端的通信功能,使服务端也能主动向客户端发送数据。 环境: tomcat7.0.70(tomcat需要在7.0.27以上才能支持) jdk1.8.0_92 ec

勤奋的老黄牛 1万+

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

java websocket一对一_SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送...

之前实现WebSocket基于STOMP的,觉得SpringBoot封装的太高,不怎么灵活,现在实现一个纯H5的,也大概了解webSocket在内部是怎么传输的。1.环境搭建因为在上一篇基于STOMP协议实现的WebSocket里已经有大概介绍过Web的基本情况了,所以在这篇就不多说了,我们直接进入正题吧,在SpringBoot中,我们还是需要导入WebSocket的包。在pox.xml加上对sp...

weixin_29765949的博客 772

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

java websocket一对一_WebSocket(3)---实现一对一聊天功能

实现一对一聊天功能功能介绍:实现A和B单独聊天功能,即A发消息给B只能B接收,同样B向A发消息只能A接收。本篇博客是在上一遍基础上搭建,上一篇博客地址:【WebSocket】---实现游戏公告功能。底部有源码。先看演示效果:一、案例解析1、PTPContoller/*** 功能描述:简单版单人聊天* 这里没有用到@SendTo("/topic/game_chat")来指定订阅地址,而是通过Simp...

weixin_28908857的博客 1071

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Java用WebSocket + tail命令实现Web实时日志

在Linux操作系统中,经常需要查看日志文件的实时输出内容,通常会使用tail -f或者taif命令。查看实时日志可能会需要首先SSH连上Linux主机,步骤很麻烦不说,如果是生产环境的服务器,可能还会控制各种权限。基于Web的实时日志可以解决这个问题。由于传统的HTTP协议是请求/响应模式,而实时日志需要不定时的持续的输出,由服务器主动推送给客户端浏览器。所以这里使用的是HTML5的WebSock

叉叉哥的BLOG 3万+

直播一对一源码在Android音频开发中如何实现对讲机实时语音对话

直播一对一源码在Android音频开发中如何实现对讲机实时语音对话 准备工作 一开始本来打算用Web端来做客户端, 但是由于技术有限, 中途换成Android(Kotlin) 端, 后台是 SpringBoot. 前后端交互是通过WebSocket进行实时数据交互. 确定技术方案后, 然后了解相关音频格式 PCM PCM(Pulse Code Modulation)也被称为脉码编码调制。PCM文件是模拟音频信号经模数转换(A/D变换)直接形成的二进制序列,该文件没有附加的文件头和文件结束标志。P

云豹直播源码 1158
上一篇: java实现物流查询(使用阿里云物流查询接口)
下一篇: Activiti7工作流引擎
辣椒太少不适合吃
博客等级 码龄5年 2691粉丝 88原创
评论 10
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址