io.sockets.to(RoomID).emit不在nodeJS上使用

时间:2018-12-05 03:53:41

标签: node.js sockets socket.io

代码:

io.sockets.to(findRoomID).emit('COMPLETE_MATCH', { start: "true" });

 *findRoomID => bbbb

控制台:

  

{bbbb:房间{插座:{bbbb:是,aaa:是},长度:2}}

但是客户没有正确收到COMPLETE_MATCH

我该怎么办?

我上传了套接字io服务器代码。

您能说出是哪一部分吗?

我不知道是哪一部分。

[服务器]

const app = require('express');
const http = require('http').Server(app);
const io = require('socket.io')(http);

var rooms = [];
var clients = [];

var clients = [];
var rooms = [];

io.on('connection', function(socket) {

    socket.on('MATCH', function (data) {

        socket.leave(socket.id);

        var NickName = data.name;
        socket.id = NickName;
        clients.push(socket.id);

        //search all rooms
        var isEmpty = false;
        var isNum = 0;
        for (var ii = 0; ii < rooms.length; ii++)
        {
            if (rooms[ii].count < 2)
            {
                isEmpty = true;
                isNum = ii;
            }
        }

        if (isEmpty) //join room
        {
            var findRoomID = rooms[isNum].id;
            rooms[isNum].sockets += ',' + socket.id;
            rooms[isNum].count++;
            socket.join(findRoomID);

            //send msg to select room
            io.in(findRoomID).emit('COMPLETE_MATCH', {start: "true"});

        }
        else //create room
        {
            var sockets = {
                id: socket.id,
                sockets : socket.id,
                count: 1
            }
            rooms.push(sockets);
            socket.join(socket.id);

        }

    });

});

客户端代码是在Unity 5.6.6上编写的。客户可以正确发出MATCH,但  无法立即获得COMPLETE_MATCH。 [客户端-Unity5.6.6]

public class ScSocket : MonoBehaviour {

    public SocketIOComponent socket;

    public Button btnMatch;
    public InputField input;
    public Text txTest;

    void Start ()
    {
        btnMatch.onClick.AddListener(() => StartCoroutine(StartMatch()));

        //
        socket.On("COMPLETE_MATCH", OnCompletedMatch);

        DontDestroyOnLoad(gameObject);
    }

    IEnumerator StartMatch()
    {
        yield return new WaitForSeconds(0.5f);
        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add("name", input.text);
        socket.Emit("MATCH", new JSONObject(data));
    }

    void OnCompletedMatch(SocketIOEvent evt)
    {
        Debug.Log(evt);
        txTest.text = JsonToString(evt.data.ToString(), "\"");
    }

    string JsonToString(string target, string s)
    {
        string[] newString = Regex.Split(target, s);
        return newString[1];
    }
}

1 个答案:

答案 0 :(得分:0)

如果您要将COMPLETE_MATCH广播给findRoomID预期发件人中的所有客户端,请使用:

socket.broadcast.to(findRoomID).emit('COMPLETE_MATCH', { start: "true" });

但是如果要设置为'findRoomID'中的所有客户端,请使用

io.in(findRoomID).emit('COMPLETE_MATCH', { start: "true" });

在服务器代码中,您使用socket.id作为已连接套接字的房间名称,但是socket.id是每个已连接套接字的唯一标识符,是由socket.io引擎生成的。我建议使用随机数生成您的房间名称,不要更改socket.id

[服务器]

const app = require('express');
const http = require('http').Server(app);
const io = require('socket.io')(http);

var clients = [];
var rooms = [];

io.on('connection', function(socket) {

    socket.on('MATCH', function (data) {

        if(socket.roomName){
            socket.leave(socket.roomName);
        }


        socket.NickName = data.name;
        clients.push(socket);

        //search all rooms
        var isEmpty = false;
        var isNum = 0;
        for (var ii = 0; ii < rooms.length; ii++)
        {
            if (rooms[ii].count < 2)
            {
                isEmpty = true;
                isNum = ii;
                break;
            }
        }

        if (isEmpty) //join room
        {
            var findRoomID = rooms[isNum].id;
            rooms[isNum].sockets.push(socket.id);
            rooms[isNum].count++;
            socket.join(findRoomID);

            //send COMPLETE_MATCH to all sockets of this room
            io.in(findRoomID).emit('COMPLETE_MATCH', {start: "true"});

        }
        else //create room
        {
            //generate a random room name
            let roomName = Math.round(Math.random()*(999999999 - 100000000)+ 100000000).toString();

            let room = {id: roomName, sockets:[], count: 0};

            socket.join(roomName);
            socket.roomName = roomName;

            room.sockets.push(socket.id);
            room.count++;

            rooms.push(room);

        }

    });

});