HTML5 Web Socket server/client implementation in Ruby
HTML5 Web Socket server/client implementation in Ruby.
For server, em-websocket ( https://github.com/igrigorik/em-websocket ) may be a better choice, especially if you want to use EventMachine.
Run sample Web Socket server (echo server) with:
$ ruby samples/echo_server.rb localhost 10081
Run sample Web Socket client and type something:
$ ruby samples/stdio_client.rb ws://localhost:10081
Ready
hoge
Sent: “hoge”
Received: “hoge”
Server:
server = WebSocketServer.new(:port => 10081, :accepted_domains => [“example.com”])
server.run() do |ws|
# The block is called for each connection.
# Checks requested path.
if ws.path == “/”
# Call ws.handshake() without argument first.
ws.handshake()
# Receives one message from the client as String.
while data = ws.receive()
puts(data)
# Sends the message to the client.
ws.send(data)
end
else
# You can call ws.handshake() with argument to return error status.
ws.handshake(“404 Not Found”)
end
end
Client:
client = WebSocket.new(“ws://example.com:10081/”)
client.send(“Hello”)
data = client.receive()
puts(data)
WebSocketServer class (server) accepts version hixie-75, hixie-76, hybi-07, hybi-10.
WebSocket class (client) speaks version hixie-76.
Google Chrome Dev Channel natively supports Web Socket. For other browsers, you can use an implementation using Flash:
http://github.com/gimite/web-socket-js/tree/master
The server supports the protocol defined in RFC 6455, draft versions hixie-75 and hixie-76.
The client speaks draft version hixie-76.
New BSD License.