Home

Awesome

WebSocket Client for Ruby

WebSocket-EventMachine-Client is Ruby WebSocket client based on EventMachine.

Installation

gem install websocket-eventmachine-client

or in Gemfile

gem 'websocket-eventmachine-client'

Simple client example

EM.run do

  ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://localhost:8080')

  ws.onopen do
    puts "Connected"
  end

  ws.onmessage do |msg, type|
    puts "Received message: #{msg}"
  end

  ws.onclose do |code, reason|
    puts "Disconnected with status code: #{code}"
  end

  EventMachine.next_tick do
    ws.send "Hello Server!"
  end

end

UNIX Domain client

You can connect to a local UNIX domain socket instead of a remote TCP socket using connect_unix_domain:

EM.run do
  ws = WebSocket::EventMachine::Client.connect_unix_domain('/var/run/wss.sock')
  # . . .
end

You can optionally specify the :version, :headers, and :ssl options to the method.

Options

Following options can be passed to WebSocket::EventMachine::Client initializer:

Methods

Following methods are available for WebSocket::EventMachine::Client object:

onopen

Called after successfully connecting.

Example:

ws.onopen do
  puts "Client connected"
end

onclose

Called after closing connection.

Parameters:

Example:

ws.onclose do |code, reason|
  puts "Client disconnected with status code: #{code} and reason: #{reason}"
end

onmessage

Called when client receive message.

Parameters:

Example:

ws.onmessage do |msg, type|
  puts "Received message: #{msg} or type: #{type}"
end

onerror

Called when client discovers error.

Parameters:

Example:

ws.onerror do |error|
  puts "Error occured: #{error}"
end

onping

Called when client receive ping request. Pong request is sent automatically.

Parameters:

Example:

ws.onping do |message|
  puts "Ping received: #{message}"
end

onpong

Called when client receive pong response.

Parameters:

Example:

ws.onpong do |message|
  puts "Pong received: #{message}"
end

send

Sends message to server.

Parameters:

Example:

ws.send "Hello Server!"
ws.send "binary data", :type => :binary

close

Closes connection and optionally send close frame to server.

Parameters:

Example:

ws.close

ping

Sends ping request.

Parameters:

Example:

ws.ping 'Hi'

pong

Sends pong request. Usually there should be no need to send this request, as pong responses are sent automatically by client.

Parameters:

Example:

ws.pong 'Hello'

Support

If you like my work then consider supporting me:

Donate with Bitcoin

Donate with Ethereum

License

(The MIT License)

Copyright © 2012 Bernard Potocki

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.