Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit f3cc030c8702c53ab6e33424bfd706cac4fe5fd4 @imanel committed Dec 20, 2012
@@ -0,0 +1,3 @@
+Gemfile.lock
+autobahn
+pkg/*.gem
@@ -0,0 +1,5 @@
+# Changelog
+
+## 1.0.0
+
+- initial release
@@ -0,0 +1,5 @@
+source "http://rubygems.org"
+
+gemspec
+
+gem 'rake'
214 README.md
@@ -0,0 +1,214 @@
+# WebSocket Client for Ruby
+
+WebSocket-EventMachine-Client is Ruby WebSocket client based on EventMachine.
+
+- [Docs](http://rdoc.info/github/imanel/websocket-eventmachine-client/master/frames)
+
+## Installation
+
+``` bash
+gem install websocket-eventmachine-client
+```
+
+or in Gemfile
+
+``` ruby
+gem 'websocket-eventmachine-client'
+```
+
+## Simple client example
+
+```ruby
+EM.run do
+
+ ws = WebSocket::EventMachine::Server.connect(:host => "0.0.0.0", :port => 8080)
+
+ ws.onopen do
+ puts "Connected"
+ end
+
+ ws.onmessage do |msg, type|
+ puts "Received message: #{msg}"
+ end
+
+ ws.onclose do
+ puts "Disconnected"
+ end
+
+ ws.send "Hello Server!"
+
+end
+```
+
+## Options
+
+Following options can be passed to WebSocket::EventMachine::Server initializer:
+
+- `[String] :host` - IP on which server should accept connections. '0.0.0.0' means all.
+- `[Integer] :port` - Port on which server should accept connections.
+- `[Boolean] :secure` - Enable secure WSS protocol. This will enable both SSL encryption and using WSS url and require `tls_options` key.
+- `[Boolean] :secure_proxy` - Enable secure WSS protocol over proxy. This will enable only using WSS url and assume that SSL encryption is handled by some kind proxy(like [Stunnel](http://www.stunnel.org/))
+- `[Hash] :tls_options` - Options for SSL(according to [EventMachine start_tls method](http://eventmachine.rubyforge.org/EventMachine/Connection.html#start_tls-instance_method))
+ - `[String] :private_key_file` - URL to private key file
+ - `[String] :cert_chain_file` - URL to cert chain file
+
+## Methods
+
+Following methods are available for WebSocket::EventMachine::Server object:
+
+### onopen
+
+Called after client is connected.
+
+Example:
+
+```ruby
+ws.onopen do
+ puts "Client connected"
+end
+```
+
+### onclose
+
+Called after client closed connection.
+
+Example:
+
+```ruby
+ws.onclose do
+ puts "Client disconnected"
+end
+```
+
+### onmessage
+
+Called when server receive message.
+
+Parameters:
+
+- `[String] message` - content of message
+- `[Symbol] type` - type is type of message(:text or :binary)
+
+Example:
+
+```ruby
+ws.onmessage do |msg, type|
+ puts "Received message: #{msg} or type: #{type}"
+end
+```
+
+### onerror
+
+Called when server discovers error.
+
+Parameters:
+
+- `[String] error` - error reason.
+
+Example:
+
+```ruby
+ws.onerror do |error|
+ puts "Error occured: #{error}"
+end
+```
+
+### onping
+
+Called when server receive ping request. Pong request is sent automatically.
+
+Parameters:
+
+- `[String] message` - message for ping request.
+
+Example:
+
+```ruby
+ws.onping do |message|
+ puts "Ping received: #{message}"
+end
+```
+
+### onpong
+
+Called when server receive pong response.
+
+Parameters:
+
+- `[String] message` - message for pong response.
+
+Example:
+
+```ruby
+ws.onpong do |message|
+ puts "Pong received: #{message}"
+end
+```
+
+### send
+
+Sends message to client.
+
+Parameters:
+
+- `[String] message` - message that should be sent to client
+- `[Hash] params` - params for message(optional)
+ - `[Symbol] :type` - type of message. Valid values are :text, :binary(default is :text)
+
+Example:
+
+```ruby
+ws.send "Hello Client!"
+ws.send "binary data", :type => :binary
+```
+
+### close
+
+Closes connection and optionally send close frame to client.
+
+Parameters:
+
+- `[Integer] code` - code of closing, according to WebSocket specification(optional)
+- `[String] data` - data to send in closing frame(optional)
+
+Example:
+
+```ruby
+ws.close
+```
+
+### ping
+
+Sends ping request.
+
+Parameters:
+
+- `[String] data` - data to send in ping request(optional)
+
+Example:
+
+```ruby
+ws.ping 'Hi'
+```
+
+### pong
+
+Sends pong request. Usually there should be no need to send this request, as pong responses are sent automatically by server.
+
+Parameters:
+
+- `[String] data` - data to send in pong request(optional)
+
+Example:
+
+``` ruby
+ws.pong 'Hello'
+```
+
+## Migrating from EM-WebSocket
+
+This library is compatible with EM-WebSocket, so only thing you need to change is running server - you need to change from EM-WebSocket to WebSocket::EventMachine::Server in your application and everything will be working.
+
+## License
+
+The MIT License - Copyright (c) 2012 Bernard Potocki
@@ -0,0 +1,16 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+# require 'rspec/core/rake_task'
+
+# RSpec::Core::RakeTask.new do |t|
+# t.rspec_opts = ["-c", "-f progress"]
+# t.pattern = 'spec/**/*_spec.rb'
+# end
+
+# task :default => :spec
+
+desc "Run autobahn tests for server"
+task :autobahn do
+ system('wstest --mode=fuzzingclient --spec=autobahn.json')
+end
@@ -0,0 +1,53 @@
+require File.expand_path('../../lib/websocket-eventmachine-server', __FILE__)
+
+require 'cgi'
+
+EM.epoll
+EM.run do
+
+ host = 'ws://localhost:9001'
+ agent = "WebSocket-Ruby (Ruby #{RUBY_VERSION})"
+ cases = 0
+ skip = []
+
+ ws = WebSocket::EventMachine::Client.connect(:uri => "#{host}/getCaseCount")
+
+ ws.onmessage do |msg, type|
+ puts "$ Total cases to run: #{msg}"
+ cases = msg.to_i
+ end
+
+ ws.onclose do
+
+ run_case = lambda do |n|
+
+ if n > cases
+ puts "$ Requesting report"
+ ws = WebSocket::EventMachine::Client.connect(:uri => "#{host}/updateReports?agent=#{CGI.escape agent}")
+ ws.onclose do
+ EM.stop
+ end
+
+ elsif skip.include?(n)
+ EM.next_tick { run_case.call(n+1) }
+
+ else
+ puts "$ Test number #{n}"
+ ws = WebSocket::EventMachine::Client.connect(:uri => "#{host}/runCase?case=#{n}&agent=#{CGI.escape agent}")
+
+ ws.onmessage do |msg, type|
+ puts "Received #{msg}(#{type})"
+ ws.send(msg, :type => type)
+ end
+
+ ws.onclose do |msg|
+ puts("Closing: #{msg}") if msg
+ run_case.call(n + 1)
+ end
+ end
+ end
+
+ run_case.call(1)
+ end
+
+end
@@ -0,0 +1,42 @@
+require File.expand_path('../../lib/websocket-eventmachine-server', __FILE__)
+
+EM.epoll
+EM.run do
+
+ trap("TERM") { stop }
+ trap("INT") { stop }
+
+ ws = WebSocket::EventMachine::Client.connect(:host => "localhost", :port => 9001);
+
+ ws.onopen do
+ puts "Connected"
+ ws.send "Hello"
+ end
+
+ ws.onmessage do |msg, type|
+ puts "Received message: #{msg}"
+ ws.send msg, :type => type
+ end
+
+ ws.onclose do
+ puts "Disconnected"
+ end
+
+ ws.onerror do |e|
+ puts "Error: #{e}"
+ end
+
+ ws.onping do |msg|
+ puts "Receied ping: #{msg}"
+ end
+
+ ws.onpong do |msg|
+ puts "Received pong: #{msg}"
+ end
+
+ def stop
+ puts "Terminating connection"
+ EventMachine.stop
+ end
+
+end
@@ -0,0 +1 @@
+require File.expand_path('../websocket/eventmachine/client', __FILE__)
Oops, something went wrong.

0 comments on commit f3cc030

Please sign in to comment.