Question

I want to write a web socket client in javascript and web socket server in ruby.

Where shall I start? are there any existing libraries to reduces my work?

I'm lost and confused googling. Please provide any links where to start, given that has knowledge on ruby, javascript, basic networking in ruby.

Was it helpful?

Solution

i currently using 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
}

for more info see another thread about ruby & websocket:

OTHER TIPS

As @intellidiot said, node.js could be the library you're looking for.

That code sample from their front page will tell you wether it's worth digging into it:

 /* 
  *     Here is an example of a simple TCP server 
  *     which listens on port 1337 
  *     and echoes whatever you send it: 
  */

var net = require('net');

var server = net.createServer(function (socket) {
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});

server.listen(1337, '127.0.0.1');

See their website and doc. You can also look for here.


Edit :

Of course this sample demonstrates server capabilities, but from this you can extrapolate to client capabilities involving the same kind of objects...

Here is a code sample from the socket.io-client README (socket.io-client is a node.js package) :

/*
 *    And now for the requested CLIENT code sample ;-)
 */

var socket = io.connect('http://domain.com');
socket.on('connect', function () {
    // socket connected
});
socket.on('custom event', function () {
    // server emitted a custom event
});
socket.on('disconnect', function () {
    // socket disconnected
});
socket.send('hi there');

Hope this helps clarify. Sorry my answer was not as straightforward as it should have been in the first place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top