通过套接字客户端返回variables

我有以下问题。 我怎样才能从另一个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; }); }); 

问候。

Solutions Collecting From Web of "通过套接字客户端返回variables"

你将不得不像这样emitlisten

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