0.01
No commit activity in last 3 years
No release in over 3 years
Rev-WebSocket is a WebSocket server implementation based on Rev, a high performance event-driven I/O library for Ruby. This library conforms to WebSocket draft-75 and draft-76.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0.3.2
>= 1.2.7
 Project Readme

Rev-WebSocket¶ ↑

Rev-WebSocket is a WebSocket server implementation based on Rev, a high performance event-driven I/O library for Ruby.

This library conforms to WebSocket draft-75 and draft-76.

Installation¶ ↑

gem install rev-websocket

Examples¶ ↑

  • examples/echo.rb - receives a message from clients and just echos.

  • examples/shoutchat.rb - simple chat application.

  • examples/rpc.rb - pushes messages to browsers from programs separated from the web socket server.

See examples/README.md for details.

Simple echo server¶ ↑

require 'rubygems'
require 'rev/websocket'

class MyConnection < Rev::WebSocket
  def on_open
    puts "WebSocket opened"
    send_message("Hello, world!")
  end

  def on_message(data)
    puts "WebSocket data received: '#{data}'"
    send_message("echo: #{data}")
  end

  def on_close
    puts "WebSocket closed"
  end
end

host = '0.0.0.0'
port = '8080'

server = Rev::WebSocketServer.new(host, port, MyConnection)
server.attach(Rev::Loop.default)

Rev::Loop.default.run

Publisher/Subscriber-style message routing¶ ↑

require 'rubygems'
require 'rev/websocket'

class PubSub
  def initialize
    @subscriber = {}
    @seqid = 0
  end

  def subscribe(&block)
    sid = @seqid += 1
    @subscriber[sid] = block
    return sid
  end

  def unsubscribe(key)
    @subscriber.delete(key)
  end

  def publish(data)
    @subscriber.each_value {|block|
      block.call(data)
    }
  end
end

$pubsub = PubSub.new

class MyConnection < Rev::WebSocket
  def on_open
    puts "WebSocket opened"
    @sid = $pubsub.subscribe {|data|
      send_message data
    }
  end

  def on_message(data)
    puts "WebSocket data received, broadcasting: '#{data}'"
    $pubsub.publish(data)
  end

  def on_close
    puts "WebSocket closed"
    $pubsub.unsubscribe(@sid)
  end
end

host = '0.0.0.0'
port = ARGV[0] || 8080

server = Rev::WebSocketServer.new(host, port, MyConnection)
server.attach(Rev::Loop.default)

puts "start on #{host}:#{port}"

Rev::Loop.default.run

Learn more¶ ↑

License¶ ↑

Copyright © 2010 FURUHASHI Sadayuki

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.