Sha256: 730776ac298b2a002ff055dfa5b45e6213671b7ae5112dcf2e21e01ef72cc3e6

Contents?: true

Size: 989 Bytes

Versions: 3

Compression:

Stored size: 989 Bytes

Contents

require 'uuidtools'

module Alondra
  module Connections
    extend self

    def connections
      @connections ||= {}
    end

    def [](websocket)
      connections[websocket]
    end

    def []=(websocket, connection)
      connections[websocket] = connection
    end

    def delete(websocket)
      connections.delete websocket
    end
  end

  class Connection
    attr_reader :uuid
    attr_reader :websocket
    attr_reader :session
    attr_reader :channels

    def initialize(websocket, session = {})
      @session = session.symbolize_keys
      @websocket   = websocket
      @uuid = UUIDTools::UUID.random_create

      Connections[websocket] = self
    end

    def channels
      @channels ||= []
    end

    def receive(event_or_message)
      Log.info "sending: #{event_or_message.to_json}"
      websocket.send event_or_message.to_json
    end

    def destroy!
      channels.each { |c| c.unsubscribe self }
      Connections.delete self.websocket
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alondra-0.1.1 lib/alondra/connection.rb
alondra-0.1.0 lib/alondra/connection.rb
alondra-0.0.4 lib/alondra/connection.rb