bbbb 控制台: {-6ren">
gpt4 book ai didi

node.js - io.sockets.to(RoomID).emit 在 NodeJS 上不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 22:21:52 27 4
gpt4 key购买 nike

代码:

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

*findRoomID => bbbb

控制台:

{ bbbb: Room { sockets: { bbbb: true, aaa: true }, length: 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];
}
}

最佳答案

如果您想向 findRoomID 中除发送者之外的所有客户端广播 COMPLETE_MATCH,请使用:

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

}

});

});

关于node.js - io.sockets.to(RoomID).emit 在 NodeJS 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com