使用Rails应用程序桥接一个简单的Node.js和Socket.io聊天应用程序(在Heroku上)

我有一个在Heroku上运行的基本Node.js和Socket.io聊天应用程序,我想要集成到我的主rails网站。 我知道这样做的方法是有两个独立的Heroku应用程序 – 一个用于rails,一个用于Node.js.

它似乎并不像将客户端html从节点应用程序移动到rails应用程序那样简单(在’io.connect();’中为其提供其他应用程序的URL)。

聊天应用服务器似乎自动调用客户端index.html自己的应用程序,并且不允许外部源连接到它。 删除执行此操作的代码(在下面标记)不会使其工作。

我是Node.js和Socket.io的新手,我希望这对于专业人士来说可能是一个相对简单的修复。

我相信我在这里的function适用于Liam Kaufman的优秀rails / node.js / socket.io示例 – 他的node.js服务器代码在这里: https : //github.com/liamks/Chatty-Node-Server/斑点/主/聊天server.js

我试过嘲笑我的应用程序的代码就像他的代码,但还没有能够使它工作。 他似乎使用’http’服务器,而我使用’快速’服务器 – 我想知道这是否相关。

任何帮助将不胜感激。

更新 :好的,所以奇怪的转变事件,感谢redhotvengeance的回复下面我已经有了这个工作 – 服务器在heroku和我的客户端html和javascript连接到它。 太棒了 – 下面的代码。 但问题是,客户端html文件仅在Rails应用程序之外才会连接!! 即在我的桌面上! 我把它放在rails应用程序的公共/文件夹或我本地主机的视图中的那一刻,我什么都没得到! 这毫无意义。 我检查它不是因为我的资产管道中的任何其他随机错误的javascript冲突只是创建一个新的rails应用程序并将html文件放在公共/文件夹中 – 再没有 – 只是一个无法连接的死html页面。 有谁知道这里可能会发生什么? Rails是否有一些安全function可以阻止连接到外部服务器或什么?

更新2 :我被告知这与“同源政策”有关,我遇到了麻烦。 它有什么办法吗? 似乎利亚姆没有这个问题。

客户:

   var socket = io.connect('http://calm-sands-3826.herokuapp.com'); // on connection to server, ask for user's name with an anonymous callback socket.on('connect', function(){ // call the server-side function 'adduser' and send one parameter (value of prompt) socket.emit('adduser', prompt("What's your name?")); }); // listener, whenever the server emits 'updatechat', this updates the chat body socket.on('updatelog', function (username, data) { $('#log').append(''+username + ': ' + data + '
'); }); // listener, whenever the server emits 'updateusers', this updates the username list socket.on('updateusers', function(data) { $('#users').empty(); $.each(data, function(key, value) { $('#users').append('
' + key + '
'); }); });
USERS

服务器:

 var port = process.env.PORT || 5001; var io = require('socket.io').listen(parseInt(port)); io.configure(function(){ io.set("transports", ["xhr-polling"]); io.set("polling duration", 10); io.set("close timeout", 10); io.set("log level", 1); }) // usernames which are currently connected to the chat var usernames = {}; io.sockets.on('connection', function (socket) { // when the client emits 'adduser', this listens and executes socket.on('adduser', function(username){ // we store the username in the socket session for this client socket.username = username; // add the client's username to the global list usernames[username] = username; // echo to client they've connected socket.emit('updatelog', 'SERVER', 'you have connected'); // echo globally (all clients) that a person has connected socket.broadcast.emit('updatelog', 'SERVER', username + ' has connected'); // update the list of users in chat, client-side io.sockets.emit('updateusers', usernames); }); // when the user disconnects.. perform this socket.on('disconnect', function(){ // remove the username from global usernames list delete usernames[socket.username]; // update list of users in chat, client-side io.sockets.emit('updateusers', usernames); // echo globally that this client has left socket.broadcast.emit('updatelog', 'SERVER', socket.username + ' has disconnected'); }); }); 

如果您要做的是将Rails应用程序中的页面连接到运行socket.io的单独Node.js应用程序,则跳过完全设置Express。 您不希望实际从Node应用程序提供页面,只需将用户连接到socket.io服务器即可。

假设Heroku上的Node.js应用程序被称为: my-awesome-socket-app

my-awesome-socket-app

 var io = require('socket.io').listen(parseInt(process.env.PORT)); io.configure(function () { io.set("transports", ["xhr-polling"]); io.set("polling duration", 10); }); io.sockets.on('connection', function (socket) { socket.on('disconnect', function () { io.sockets.emit('user disconnected'); }); }); 

然后,在Rails页面中,您要连接到socket.io服务器: