- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
bbbb 控制台: {-6ren">
代码:
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/
我是 Rocket.Chat 的新手。我需要向 channel 发送消息。我已经获得授权了。当我阅读有关发送消息的文档时,我发现了 roomId 参数,但我不知道如何获取此 ID。我尝试在 Rocke
我正在尝试使用单独的房间创建 FireBase 聊天,并且仅显示来自该房间的消息。如何仅显示带有 roomid = somenumber 的消息(当前设置为 4) 数据可能如下所示: 这是 fireb
代码: io.sockets.to(findRoomID).emit('COMPLETE_MATCH', { start: "true" }); *findRoomID => bbbb 控制台: {
我是一名优秀的程序员,十分优秀!