README.md in websocket-eventmachine-server-1.0.0 vs README.md in websocket-eventmachine-server-1.0.1
- old
+ new
@@ -1,15 +1,16 @@
# WebSocket Server for Ruby
WebSocket-EventMachine-Server is Ruby WebSocket server based on EventMachine.
+- [Autobahn tests](http://imanel.github.com/websocket-ruby/autobahn/server)
+- [Docs](http://rdoc.info/github/imanel/websocket-eventmachine-server/master/frames)
+
## Why another WebSocket server?
-There are multiple Ruby WebSocket servers, each with different quirks and errors. Most commonly used em-websocket is unfortunately slow and have multiple bugs(see Autobahn tests below). This library was created to fix most of them.
+There are multiple Ruby WebSocket servers, each with different quirks and errors. Most commonly used em-websocket is unfortunately slow and have multiple bugs(see Autobahn tests above). This library was created to fix most of them.
-[Autobahn tests](http://imanel.github.com/websocket-ruby/autobahn/server)
-
## Installation
``` bash
gem install websocket-eventmachine-server
```
@@ -21,64 +22,192 @@
```
## Simple server example
```ruby
-EventMachine.run {
+EM.run do
- WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => 8080) do |ws|
- ws.onopen {
- puts "WebSocket connection open"
+ WebSocket::EventMachine::Server.start(:host => "0.0.0.0", :port => 8080) do |ws|
+ ws.onopen do
+ puts "Client connected"
+ end
- # publish message to the client
- ws.send "Hello Client"
- }
+ ws.onmessage do |msg, type|
+ puts "Received message: #{msg}"
+ ws.send msg, :type => type
+ end
- ws.onclose { puts "Connection closed" }
- ws.onmessage { |msg|
- puts "Recieved message: #{msg}"
- ws.send "Pong: #{msg}"
- }
+ ws.onclose do
+ puts "Client disconnected"
end
-}
+ end
+
+end
```
-## Secure server
+## Options
-It is possible to accept secure wss:// connections by passing :secure => true when opening the connection. Safari 5 does not currently support prompting on untrusted SSL certificates therefore using signed certificates is highly recommended. Pass a :tls_options hash containing keys as described in http://eventmachine.rubyforge.org/EventMachine/Connection.html#M000296
+Following options can be passed to WebSocket::EventMachine::Server initializer:
-For example,
+- `[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
-WebSocket::EventMachine::Server.start({
- :host => "0.0.0.0",
- :port => 443,
- :secure => true,
- :tls_options => {
- :private_key_file => "/private/key",
- :cert_chain_file => "/ssl/certificate"
- }
-}) do |ws|
-...
+ws.onopen do
+ puts "Client connected"
end
```
-## Running behind an SSL Proxy/Terminator, like Stunnel
+### onclose
-The :secure_proxy => true option makes it possible to run correctly when behind a secure SSL proxy/terminator like [Stunnel](http://www.stunnel.org/). When setting :secure_proxy => true, any reponse from the em-websocket which contains the websocket url will use the wss:// url scheme. None of the traffic is encrypted.
+Called after client closed connection.
-This option is necessary when using websockets with an SSL proxy/terminator on Safari 5.1.x or earlier, and also on Safari in iOS 5.x and earlier. Most versions of Chrome, Safari 5.2, and Safari in iOS 6 do not appear to have this problem.
+Example:
-For 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
-WebSocket::EventMachine::Server.start({
- :host => "0.0.0.0",
- :port => 8080,
- :secure_proxy => true
-}) do |ws|
-...
+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.