Class: Farcall::WebsocketJsonClientTransport

Inherits:
Transport
  • Object
show all
Defined in:
lib/farcall/wsclient_transport.rb

Overview

Websocket client transport using JSON encodeing. Works with ruby threads, pure ruby, runs everywhere. Use if like any thoer Farcall::Transport, for example:

in your Gemfile

gem 'websocket-client-simple'

in the code

wst = Farcall::WebsocketJsonClientTransport.new 'ws://icodici.com:8080/test'
i = Farcall::Interface.new transport: wst
result = i.authenticate(, password) # remote call via interface...

Instance Attribute Summary

Attributes inherited from Transport

#on_abort, #on_close, #on_data_received

Instance Method Summary collapse

Methods inherited from Transport

#close, #closed?, create, #receive_data

Constructor Details

#initialize(ws_url) ⇒ WebsocketJsonClientTransport

Create transport connected to the specified websocket url. Constructor blocks until connected, or raise error if connection can’t be established. Transport uses JSON encodgin over standard websocket protocol.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/farcall/wsclient_transport.rb', line 25

def initialize ws_url
  # The stranges bug around in the WebSocket::Client (actually in his eventemitter)
  me = self

  is_open = Semaphore.new
  @ws     = WebSocket::Client::Simple.connect(ws_url)

  @ws.on(:open) {
    # if me != self
    #   puts "\n\n\nSelf is set to wrong in the callback in #{RUBY_VERSION}\n\n\n"
    # end
    # puts "client is open"
    is_open.set
  }

  @ws.on(:message) { |m|
    # puts "ws client received #{JSON.parse m.data}"
    me.on_data_received and me.on_data_received.call(JSON.parse m.data)
    # puts "and sent"
  }
  @ws.on(:close) { close }
  is_open.wait_set
end

Instance Method Details

#send_data(data) ⇒ Object

:nodoc:



50
51
52
# File 'lib/farcall/wsclient_transport.rb', line 50

def send_data data
  @ws.send JSON[data]
end