WebSocketSequentialClient
This ruby gem library allows you to access a WebSocket server in a sequential way instead of an event-driven way.
Features
-
Provides access to a WebSocket server in a sequential way.
Many gem libraries for WebSocket client use event-driven style.
This style has many advantages, but it's not so easy to understand.
This gem library provides a simple and sequential access to a WebSocket server.
-
Independent from eventmachine and other frameworks.
You can use this gem library with/without eventmachine and other frameworks.
Eventmachine is a great and useful library, but it uses some global functions, such as EM.run
, EM.stop
and so on, so it might conflicts with your environment.
This gem library does not depends on any frameworks, so you can use it in your environment.
If you need performance and/or scalability, you should use the other great libraries, such as em-websocket.
Installation
Add this line to your application's Gemfile:
gem 'websocket_sequential_client'
And then execute:
$ bundle
Or install it yourself as:
$ gem install websocket_sequential_client
Usage
Call send
and recv
to communicate with the server.
require "websocket_sequential_client"
WebSocketSequentialClient::WebSocket.open "ws://server-url" do |ws|
ws.send "Hello"
puts ws.recv
end
Note: recv
method is a blocking method. It will wait until a message is sent from the server.
More examples
Send and receive text and binary messages.
require "websocket_sequential_client"
ws = WebSocketSequentialClient::WebSocket.new "ws://server-url"
ws.send "Hello"
ws.send_text "World"
ws.send [0,1,2,3].pack("C*")
ws.send "example".b
ws.send_binary "binary message"
msg = ws.recv
ws.close
Ping and pong.
ping
and pong
frames are sent in background, so you need not to handle these frames.
require "websocket_sequential_client"
ws = WebSocketSequentialClient::WebSocket.new "ws://server-url"
ws.close
ws = WebSocketSequentialClient::WebSocket.new "ws://server-url", ping: false
ws.close
ws = WebSocketSequentialClient::WebSocket.new "ws://server-url", ping: { interval: 10 }
ws.close
You can send additional headers with the request of HTTP handshake.
require "websocket_sequential_client"
ws = WebSocketSequentialClient::WebSocket.new "ws://server-url", headers: { "Cookie" => "value1" }
ws.close
Versions
- 1.0.1
Fix handling of optional parameters. - 1.0.0
Initial release