Sha256: 2fbe97637f9ff60d5c3f29072e9737190774881d0ee6df4ab5ff79c73f529bee

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

require 'websocket-eventmachine-client'

module Smash
  module CloudPowers
    module Synapse
      module WebSoc
        module SocClient
          def create_websoc_client(opts = {})
            ws = {}
            Thread.new(ws) do
              EM.run do
                ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://' + opts[:host]  + ':' + opts[:port])

                client_name = opts[:client] || 'default_client'

                instance_variable_set(:"@#{client_name}",ws)

                open_callback = opts[:on_open] || Proc.new do
                  puts "Connected"
                end

                ws.onopen &open_callback

                on_message_callback = opts[:on_message] || Proc.new do |msg, type|
                  puts "Received message: #{msg}"
                end

                ws.onmessage &on_message_callback

                on_error_callback = opts[:on_error] || Proc.new do |error|
                  puts "Error ==> #{error}"
                end

                ws.onerror &on_error_callback

                on_close_callback = opts[:on_close] || Proc.new do |code, reason|
                  puts "Disconnected with status code: #{code}"
                  puts "Disconnected with status message: #{reason}"
                end

                ws.onclose &on_close_callback

              end

            end
          end

        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cloud_powers-1.1.1 lib/cloud_powers/synapse/web_soc/soc_client.rb
cloud_powers-1.1.0 lib/cloud_powers/synapse/web_soc/soc_client.rb
cloud_powers-1.0.1 lib/cloud_powers/synapse/web_soc/soc_client.rb
cloud_powers-1.0.0 lib/cloud_powers/synapse/web_soc/soc_client.rb