当前位置:   article > 正文

websocket 实现小房间内收发_websocket 游戏房间 转发数据

websocket 游戏房间 转发数据

导包

  		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4

配置

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();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

服务端

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();
                }
            }
        }
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/474798
推荐阅读
相关标签
  

闽ICP备14008679号