gpt4 book ai didi

javascript - 套接字客户端返回变量

转载 作者:行者123 更新时间:2023-11-30 21:54:46 25 4
gpt4 key购买 nike

我有以下问题。如何从另一个 JS 文件向套接字返回一个新值?

App.js

io.on('connection', function (socket) {
socket.currentRoomId = 0;
socket.currentRoomName = "Loading...";
socket.currentRoomOwner = 1;
socket.currentRoomEntry = 1.1;
});

房间管理器.js

adapter.query(sql, function(err, rows) {
rows.forEach( (row) => {
socket.currentRoomId = parseInt(row.Id);
socket.currentRoomName = row.room_name;
socket.currentRoomOwner = row.room_owner;
socket.currentRoomEntry = row.room_entry;
});
});

问候。

最佳答案

你必须像这样发射聆听

RoomManager.js

adapter.query(sql, function(err, rows) {
rows.forEach( (row) => {
// emit setCurrentData with the row data
socket.emit('setCurrentData', { data: row });
});
});

App.js

io.on('connection', function (socket) {
socket.currentRoomId = 0;
socket.currentRoomName = "Loading...";
socket.currentRoomOwner = 1;
socket.currentRoomEntry = 1.1;

// listen to setCurrentData
socket.on('setCurrentData', function (row) {
socket.currentRoomId = parseInt(row.data.Id);
socket.currentRoomName = row.data.room_name;
socket.currentRoomOwner = row.data.room_owner;
socket.currentRoomEntry = row.data.room_entry;
});
});

关于javascript - 套接字客户端返回变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45677744/

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