用另一个javascript文件nodejs发送和获取socket.io的数据并expression

比方说,我有一个JavaScript文件需要与其他JavaScript文件进行通信。 所以我可以在不同的JavaScript文件中使用这个数据。

在这种情况下,我有一个game_server.js 。 在gameserver.js我有两个variables,我想在gamecore.js

 var host // host of the game var client // client that had joined the game 

我想将它们发送到app.js中的socket.io,然后在game_core.js使用这个variables。 所以我可以有关于主机和客户端的数据。

在游戏类中,我想要这样的东西

 game_core.prototype.getPlayerInformation = function(data) { this.host = data.host this.client = data.client } 

从服务器端获取信息到客户端,最好的方法是通过socket.io,但我真的不知道如何

另外在game_server脚本中还有一个游戏实例

game_server.createGame = function(player){

  //Create a new game instance var thegame = { id : UUID(), //generate a new id for the game player_host:player, //so we know who initiated the game player_client:null, //nobody else joined yet, since its new player_count:1 //for simple checking of state }; 

game_core ,声明了一个游戏的实例

 var game_core = function(game_instance) { //Store the instance, if any this.instance = game_instance; } 

所以应该可以得到player_hostplayer_client

server.js

 var app = require('express')() , server = require('http').createServer(app) , io = require('socket.io').listen(server); server.listen(80); var Game_core = function(){} Game_core.prototype.getPlayerInformation = function(data) { this.host = data.host this.client = data.client return {host: this.host, client: this.client} } var game_core = new Game_core() io.sockets.on('connection', function (socket) { socket.emit('login', game_core.getPlayerInformation); }); 

client.js

 <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('login', function(data){ console.log(data); // {host: xx, client: xx} }) </script>