websocket 实现小房间内收发

导包

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

配置

package com.example.nettyws.config;

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

/**
 * @author yanshuoshi
 */
@Configuration
public class WebsocketConfiguration {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

服务端

package com.example.nettyws.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint("/ws/{arg}")
@Component
public class MyWebSocket1 {

    private static ConcurrentHashMap<String, Set<Session>> rooms = new ConcurrentHashMap<>();

    @OnOpen
    public void onOpen(Session session, @PathParam("arg") String arg) {
        //查询当前动作

        if (!rooms.containsKey(arg)) {
            // 创建房间不存在时,创建房间
            Set<Session> room = new HashSet<>();
            // 添加用户
            room.add(session);
            rooms.put(arg, room);
        } else {
            // 房间已存在,直接添加用户到相应的房间
            rooms.get(arg).add(session);
        }
        System.out.println("当前共:" + rooms.size() + "个房间!");
    }

    @OnClose
    public void onClose(Session session,@PathParam("arg")  String arg) throws IOException {
        System.out.println("one connection closed");

        rooms.get(arg).remove(session);
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        throwable.printStackTrace();
    }

    @OnMessage
    public void onMessage(Session session, String message,@PathParam("arg") String arg) {

        System.out.println("接收到数据: " + message);
        if(message != null && "success".equals(message)){
            try {
                session.getBasicRemote().sendText(message);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            return;
        }
        // message等于空说明此次连接是为首次连接操作
        if (message != null) {

            for (Session session1 : rooms.get(arg)) {
                try {
                    session1.getBasicRemote().sendText(message);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值