ruby中的socket.io和eventmachine

我正在尝试一个非常基本的服务器/客户端演示.我在客户端(浏览器中的用户)和服务器的eventmachine Echo示例中使用socket.io.理想情况下,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

解决方法

您的客户端代码正在尝试使用websockets协议连接到服务器.但是,您的服务器代码不接受websockets连接 – 它只是在做HTTP.

一种选择是使用事件机器websockets插件

https://github.com/igrigorik/em-websocket

EventMachine.run {

    EventMachine::WebSocket.start(:host => "0.0.0.0",:port => 8080) do |ws|
        ws.onopen {
          puts "WebSocket connection open"

          # publish message to the client
          ws.send "Hello Client"
        }

        ws.onclose { puts "Connection closed" }
        ws.onmessage { |msg|
          puts "Recieved message: #{msg}"
          ws.send "Pong: #{msg}"
        }
    end
}

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...