ruby 中的 socket.io 和 eventmachine
我正在尝试一个非常基本的服务器/客户端演示。我在客户端(浏览器中的用户)上使用 socket.io,在服务器上使用 eventmachine Echo 示例。理想情况下,socket.io 应该向服务器发送请求,服务器将打印接收到的数据。不幸的是,有些东西没有按照我的预期工作。
源代码粘贴在这里:
socket = new io.Socket('localhost',{
port: 8080
});
socket.connect();
$(function(){
var textBox = $('.chat');
textBox.parent().submit(function(){
if(textBox.val() != "") {
//send message to chat server
socket.send(textBox.val());
textBox.val('');
return false;
}
});
socket.on('message', function(data){
console.log(data);
$('#text').append(data);
});
});
这是 ruby 代码:
require 'rubygems'
require 'eventmachine'
require 'evma_httpserver'
class Echo < EM::Connection
def receive_data(data)
send_data(data)
end
end
EM.run do
EM.start_server '0.0.0.0', 8080, Echo
end
I am trying out a very basic server/client demo. I am using socket.io on the client(a user in a browser) and eventmachine Echo example for server. Ideally socket.io should send a request to server and server will print the received data. Unfortunately, something is not working as I expect it to.
Source is pasted here:
socket = new io.Socket('localhost',{
port: 8080
});
socket.connect();
$(function(){
var textBox = $('.chat');
textBox.parent().submit(function(){
if(textBox.val() != "") {
//send message to chat server
socket.send(textBox.val());
textBox.val('');
return false;
}
});
socket.on('message', function(data){
console.log(data);
$('#text').append(data);
});
});
and here is ruby code:
require 'rubygems'
require 'eventmachine'
require 'evma_httpserver'
class Echo < EM::Connection
def receive_data(data)
send_data(data)
end
end
EM.run do
EM.start_server '0.0.0.0', 8080, Echo
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的客户端代码正在尝试使用 websockets 协议连接到服务器。但是,您的服务器代码不接受 websockets 连接 - 它只执行 HTTP。
一种选择是使用事件机 websockets 插件:
https://github.com/igrigorik/em-websocket
You client code is trying to connect to a server using the websockets protocol. However, your server code isn't accepting websockets connections - it's only doing HTTP.
One option is to use the event machine websockets plugin:
https://github.com/igrigorik/em-websocket
我会考虑使用Cramp。它是一个支持 Websockets 的异步框架,构建于 EventMachine 之上。我已经尝试过这些示例,并且不得不承认该 API 看起来优雅且干净。
I'd look into using Cramp. It's an async framework with websockets support, built on top of EventMachine. I've played around with the samples and I have to admit that the API looks elegant and clean.
我会研究 Plezi。
您的服务器端回显代码可能如下所示:
只需在 IRB 中键入它,一旦使用
exit
命令退出 IRB,回显服务器就会开始运行。Plezi 使用和支持 Websockets、HTTP Streaming 和 RESTful HTTP 请求确实很有趣,因此很容易依靠长拉取并提供静态内容以及实时更新。
Plezi 还内置了对 Redis 的支持,因此可以跨进程和机器推送数据。
I would look into Plezi.
Your server side echo code could look something like this:
just type it in IRB and the echo server will start running once you exit IRB using the
exit
command.Plezi is really fun to work with and support Websockets, HTTP Streaming and RESTful HTTP requests, so it's easy to fall back on long-pulling and serve static content as well as real-time updates.
Plezi also has built in support for Redis, so it's possible to push data across processes and machines.