基于redis与websocket实现集群即时消息聊天---广播式

效果图:
基于redis与websocket实现集群即时消息聊天---广播式_第1张图片
基于redis与websocket实现集群即时消息聊天---广播式_第2张图片
在上一篇阻塞式不太适合大场景,所以采用广播式对群进行分组,一个主题代表一个群
代码如下:

集成redis

server.port=8080
spring.thymeleaf.cache=false


redis.host=127.0.0.1
## Redis服务器连接端口
redis.port=6379
## 连接超时时间(毫秒)
## Redis服务器连接密码(默认为空)
redis.password=
## 连接池中的最大连接数
redis.poolMaxTotal=10
## 连接池中的最大空闲连接
redis.poolMaxIdle=10
## 连接池最大阻塞等待时间(使用负值表示没有限制)
redis.poolMaxWait=3

        <dependency>
            <groupId>org.springframework.datagroupId>
            <artifactId>spring-data-redisartifactId>
            <version>2.0.9.RELEASEversion>
        dependency>
        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.9.0version>
        dependency>
@Configuration
public class RedisConfig {
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    /**
     * @author  描述:需要手动注册RedisMessageListenerContainer加入IOC容器
     * @return
     */
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer() {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();

        container.setConnectionFactory(jedisConnectionFactory);

        return container;

    }
}

控制层

@Controller
@RequestMapping("/websocket")
public class WebsocketController {

    @Value("${server.port}")
    private String port;

    public static final String INDEX = "index";
    /**
     * @author 
     * 描述:聊天页
     * @param topic 发布订阅的频道主题
     * @param myname 发布者的显示名称
     * @return
     */
    @RequestMapping("/index/{topic}/{myname}")
    public ModelAndView index(@PathVariable("topic")String topic, @PathVariable("myname")String myname
                             )  {
        ModelAndView mav = new ModelAndView(INDEX);
        mav.addObject("port", port);
        mav.addObject("topic",topic);
        mav.addObject("myname",myname);
        return mav;
    }
}

WebsocketConfig

@Configuration
public class WebsocketConfig {
    /**
     * 
描 述: @Endpoint注解的websocket交给ServerEndpointExporter自动注册管理 * @return */
@Bean public ServerEndpointExporter serverEndpointExporter(){ return new ServerEndpointExporter(); } }

websocket主要代码:WebsocketEndpoint

/**
 *@ServerEndpoint(value="/websocket")value值必须以/开路
 *备注:@ServerEndpoint注解类不支持使用@Autowire
 *{topic}指:向哪个频道主题里发消息
 *{myname}指:这个消息是谁的。真实环境里可以使用当前登录用户信息
 */
@Component
@ServerEndpoint(value="/websocket/{topic}/{myname}")
public class WebsocketEndpoint {

    /**
     * 因为@ServerEndpoint不支持注入,所以使用SpringUtils获取IOC实例
     */
    private StringRedisTemplate redisTampate = SpringUtils.getBean(StringRedisTemplate.class);

    private RedisMessageListenerContainer redisMessageListenerContainer = SpringUtils.getBean(RedisMessageListenerContainer.class);

    //存放该服务器该ws的所有连接。用处:比如向所有连接该ws的用户发送通知消息。
    private static CopyOnWriteArraySet<WebsocketEndpoint> sessions = new CopyOnWriteArraySet<>();

    private Session session;

    @OnOpen
    public void onOpen(Session session,@PathParam("topic")String topic){
        System.out.println("java websocket:打开连接");
        this.session = session;
        sessions.add(this);
        SubscribeListener subscribeListener = new SubscribeListener();
        subscribeListener.setSession(session);
        subscribeListener.setStringRedisTemplate(redisTampate);
        //设置订阅topic
        redisMessageListenerContainer.addMessageListener(subscribeListener, new ChannelTopic(topic));
    }

    @OnClose
    public void onClose(Session session){
        System.out.println("java websocket:关闭连接");
        sessions.remove(this);
    }

    @OnMessage
    public void onMessage(Session session,String message,@PathParam("topic")String topic,@PathParam("myname")String myname) throws IOException{
        message = myname+":"+message;
        System.out.println("java websocket 收到消息=="+message);
        PublishService publishService = SpringUtils.getBean(PublishService.class);
        publishService.publish(topic, message);
    }

    @OnError
    public void onError(Session session,Throwable error){
        System.out.println("java websocket 出现错误");
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }
}

消息发布订阅 主题

/**
 * @author  描述:发布service
 */
@Component
public class PublishService {
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * @param channel 消息发布订阅 主题
     * @param message 消息信息
     * @author 描述:发布方法
     */
    public void publish(String channel, Object message) {
        redisTemplate.convertAndSend(channel, message);
    }
}

订阅监听类

/**
 * @author  描述:订阅监听类
 */
public class SubscribeListener implements MessageListener {

    private StringRedisTemplate stringRedisTemplate;

    private Session session;

    /**
     * 订阅接收发布者的消息
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String msg = new String(message.getBody());
        System.out.println(new String(pattern) + "主题发布:" + msg);
        if(null!=session){
            try {
                session.getBasicRemote().sendText(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public StringRedisTemplate getStringRedisTemplate() {
        return stringRedisTemplate;
    }

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

}

@Repository
public final class SpringUtils implements BeanFactoryPostProcessor {

    private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

    public static ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws org.springframework.beans.BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) getBeanFactory().getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws org.springframework.beans.BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = (T) getBeanFactory().getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return getBeanFactory().containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().getAliases(name);
    }

}

客户端代码

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"></meta>
<title>websocket集群</title>
</head>
<body>
    本服务端口号:[[${port}]],使用redismq实现websocket集群<br/>
        [[${topic}]] 频道 聊天中。。。<br/>
<input id="input_id" type="text" /><button onclick="sendMessage()">发送</button>    <button onclick="closeWebsocket()">关闭</button>
<div id="message_id"></div>
</body>
<script type="text/javascript">
        document.getElementById('input_id').focus();
        var websocket = null;
        //当前浏览前是否支持websocket
        if("WebSocket" in window){
        var url = "ws://127.0.0.1:[[${port}]]/websocket/[[${topic}]]/[[${myname}]]";
        websocket = new WebSocket(url);
        }else{
        alert("浏览器不支持websocket");
        }

        websocket.onopen = function(event){
        setMessage("打开连接");
        }

        websocket.onclose = function(event){
        setMessage("关闭连接");
        }

        websocket.onmessage = function(event){
        setMessage(event.data);
        }

        websocket.onerror = function(event){
        setMessage("连接异常");
        }

        //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
        window.onbeforeunload = function(){
        closeWebsocket();
        }

        //关闭websocket
        function closeWebsocket(){
        //3代表已经关闭
        if(3!=websocket.readyState){
        websocket.close();
        }else{
        alert("websocket之前已经关闭");
        }
        }

        //将消息显示在网页上
        function setMessage(message){
        document.getElementById('message_id').innerHTML += message + '
'
; } //发送消息 function sendMessage(){ //1代表正在连接 if(1==websocket.readyState){ var message = document.getElementById('input_id').value; //setMessage(message); websocket.send(message); }else{ alert("websocket未连接"); } document.getElementById('input_id').value=""; document.getElementById('input_id').focus(); } </script> </html>

你可能感兴趣的:(spring,boot)