我有以下问题。如何从另一个JS文件中向套接字返回一个新值?
App.js
io.on('connection', function (socket) {
socket.currentRoomId = 0;
socket.currentRoomName = "Loading...";
socket.currentRoomOwner = 1;
socket.currentRoomEntry = 1.1;
});
RoomManager.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;
});
});
问候。
答案 0 :(得分:0)
您必须emit
和listen
这样,
<强> 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;
});
});