Giter Club home page Giter Club logo

websocket-eventmachine-client's Introduction

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

Options

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

  • [String] :host - IP or host of server to connect
  • [Integer] :port - Port of server to connect
  • [String] :uri - Full URI for server(optional - use instead of host/port combination)
  • [Integer] :version - Version of WebSocket to use. Default: 13
  • [Hash] :headers - HTTP headers to use in the handshake. Example: {'Cookie' => 'COOKIENAME=Value'}
  • [Boolean] :ssl - Force SSL/TLS regardless of URI scheme or port

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:

  • [Integer] code - status code
  • [String] reason - optional reason for closure

Example:

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

onmessage

Called when client receive message.

Parameters:

  • [String] message - content of message
  • [Symbol] type - type is type of message(:text or :binary)

Example:

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

onerror

Called when client discovers error.

Parameters:

  • [String] error - error reason.

Example:

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

onping

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

Parameters:

  • [String] message - message for ping request.

Example:

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

onpong

Called when client receive pong response.

Parameters:

  • [String] message - message for pong response.

Example:

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

send

Sends message to server.

Parameters:

  • [String] message - message that should be sent to server
  • [Hash] params - params for message(optional)
    • [Symbol] :type - type of message. Valid values are :text, :binary(default is :text)

Example:

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

close

Closes connection and optionally send close frame to server.

Parameters:

  • [Integer] code - code of closing, according to WebSocket specification(optional)
  • [String] data - data to send in closing frame(optional)

Example:

ws.close

ping

Sends ping request.

Parameters:

  • [String] data - data to send in ping request(optional)

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:

  • [String] data - data to send in pong request(optional)

Example:

ws.pong 'Hello'

License

The MIT License - Copyright (c) 2012 Bernard Potocki

websocket-eventmachine-client's People

Contributors

danknox avatar drbig avatar imanel avatar jaredatron avatar shpakvel avatar sumo-mbryant avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.