class EventdClient

Attributes

socket[RW]

em-websocket socket object

Public Class Methods

new(socket, server = nil) click to toggle source

Initializer

Attributes

  • socket

  • server

Calls superclass method EventdObject::new
# File lib/eventd/eventd_client.rb, line 28
    def initialize(socket, server = nil)
            super()
            @socket = socket
@server = server
            setup_socket unless @socket == nil
    end

Public Instance Methods

broadcast(channel, data = nil) click to toggle source

Send a broadcast to other clients

Attributes

  • channel

  • data

# File lib/eventd/eventd_client.rb, line 84
def broadcast(channel, data = nil)
  if @server.broadcast_channel_allowed? channel
    @server.clients.each do |client| client.emit channel, data end
  else
    self.emit 'error', "Broadcasts are not allowed on channel: #{channel}"
  end
end
element(selector) click to toggle source

Instantiate a new EventdElement

Attributes

  • selector

# File lib/eventd/eventd_client.rb, line 97
def element(selector)
      EventdElement.new(selector, self)
end
emit(channel, data = nil, local = false) click to toggle source

Send an emission to the client side library

Attributes

  • channel

  • data

  • local

Calls superclass method EventdObject#emit
# File lib/eventd/eventd_client.rb, line 69
      def emit(channel, data = nil, local = false)
              super(channel, data)

              if not local
                      emission = { 'channel' => channel, 'priority' => 'normal', 'data' => data }
                      @socket.send emission.to_json
              end
end
setup_socket() click to toggle source

Handle required events from the socket

# File lib/eventd/eventd_client.rb, line 37
def setup_socket
        @socket.onopen do |handshake|
                self.emit('connect', handshake, true)
        end

        @socket.onclose do
                self.emit('disconnect', nil, true)
        end

        @socket.onmessage do |message|
                begin
                        emission = JSON.parse message

                        if emission['broadcast']
                                self.broadcast emission['channel'], emission['data']
                        end

                        self.emit emission['channel'], emission['data']
                        self.emit('message', message, true)
                rescue JSON::ParserError
                        self.emit('message', message, true)
                end
        end
end