效果图:
在上一篇阻塞式不太适合大场景,所以采用广播式对群进行分组,一个主题代表一个群
代码如下:
集成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
org.springframework.data
spring-data-redis
2.0.9.RELEASE
redis.clients
jedis
2.9.0
@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 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 getBean(String name) throws BeansException {
return (T) getBeanFactory().getBean(name);
}
/**
* 获取类型为requiredType的对象
*
* @param clz
* @return
* @throws org.springframework.beans.BeansException
*
*/
public static T getBean(Class 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);
}
}
客户端代码
websocket集群
本服务端口号:[[${port}]],使用redismq实现websocket集群
[[${topic}]] 频道 聊天中。。。