Class: EmFarcall::WsServerEndpoint
- Defined in:
- lib/farcall/em_wsserver_endpoint.rb
Overview
Farcall websocket client, based on the websocket-client-simple gem. We do not put it into dependencies as it is not always needed, so add to your Gemfile first:
gem 'websocket-client-simple'
and do not forget to require eventmachine before requiring the farcall. Now you can use it with EM:
require 'eventmachine'
require 'farcall'
# ....
endpoint = nil
EM::WebSocket.run(params) do |ws|
ws.onopen { |handshake|
# Check handshake.path, handshake query
# for example to select the provider, then connect Farcall to websocket:
endpoint = EmFarcall::WsServerEndpoint.new ws, provider: WsProvider.new
}
end
now we can use it as usual: remote can call provder method, and we can call remote:
endpoint.remote.do_something( times: 4) { |result|
}
We do not include it into gem dependencies as it uses EventMachine which is not needed under JRuby and weight alot (the resto of Farcall plays well with jruby and MRI threads)
Due to event-driven nature of eventmachine, WsServerEndpoint uses special version of Endpoint and WsProvider which are code compatible with regular farcall classes except for the callback-style calls where appropriate.
Instance Attribute Summary
Attributes inherited from Endpoint
Instance Method Summary collapse
-
#initialize(websocket, **kwargs) ⇒ WsServerEndpoint
constructor
Create endpoint with the already opened websocket instance.
- #pack(data) ⇒ Object
- #unpack(data) ⇒ Object
Methods inherited from Endpoint
#call, #close, #error, #on, #on_command, #on_remote_call, #remote
Constructor Details
#initialize(websocket, **kwargs) ⇒ WsServerEndpoint
Create endpoint with the already opened websocket instance. Note that all the handshake should be done prior to construct endpoint (e.g. you may want to have different endpoints for different paths and arguments)
See Endpoint for methods to call remote interface and process remote requests.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/farcall/em_wsserver_endpoint.rb', line 52 def initialize websocket, **kwargs @input = EM::Channel.new @output = EM::Channel.new super(@input, @output, **kwargs) websocket. { |data| @input << unpack(data) } @output.subscribe { |data| websocket.send(pack data) } end |
Instance Method Details
#pack(data) ⇒ Object
69 70 71 |
# File 'lib/farcall/em_wsserver_endpoint.rb', line 69 def pack data JSON[data] end |
#unpack(data) ⇒ Object
65 66 67 |
# File 'lib/farcall/em_wsserver_endpoint.rb', line 65 def unpack data JSON.parse data end |