开发者

socket.io and eventmachine in ruby

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


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

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
}


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.


I would look into Plezi.

Your server side echo code could look something like this:

require 'plezi'

class EchoCtrl
    def index
        redirect_to 'http://www.websocket.org/echo.html'
    end
    def on_message data
        # to broadcast the data add:
        # broadcast :_send_message, data
        _send_message data
    end
    def _send_message data
        response << data
    end
end

listen 

# you can add, a socket.io route for JSON with socket.io
route '/socket.io', EchoCtrl
route '/', EchoCtrl

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜