chr*_*ian 3 ruby-on-rails heroku node.js socket.io
我有一个在Heroku上运行的基本Node.js和Socket.io聊天应用程序,我想要集成到我的主rails网站.我知道这样做的方法是有两个独立的Heroku应用程序 - 一个用于rails,一个用于Node.js.
它似乎并不像将客户端html从节点应用程序移动到rails应用程序那样简单(在'io.connect();'中为其提供其他应用程序的URL).
聊天应用服务器似乎自动调用客户端index.html自己的应用程序,并且不允许外部源连接到它.删除执行此操作的代码(在下面标记)不会使其工作.
我是Node.js和Socket.io的新手,我希望这对于专业人士来说可能是一个相对简单的修复.
我相信我在这里的功能适用于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是否有一些安全功能可以阻止与外部服务器或其他东西的连接?
更新2:我被告知这与"同源政策"有关,我遇到了麻烦.它有什么办法吗?似乎利亚姆没有这个问题.
客户:
<script src="http://calm-sands-3826.herokuapp.com/socket.io/socket.io.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
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('<b>'+username + ':</b> ' + data + '<br>');
});
// 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('<div>' + key + '</div>');
});
});
</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
<b>USERS</b>
<div id="users"></div>
</div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
<div id="log"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
服务器:
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');
});
});
Run Code Online (Sandbox Code Playgroud)
如果您要做的是将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');
});
});
Run Code Online (Sandbox Code Playgroud)
然后,在Rails页面中,您要连接到socket.io服务器:
<script src="http://my-awesome-socket-app.herokuapp.com/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://my-awesome-socket-app.herokuapp.com');
socket.on('connect', function (data) {
console.log('connected!');
});
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4200 次 |
| 最近记录: |