jam-cloud/websocket-gateway/lib/jam_websockets/client_context.rb

42 lines
1.0 KiB
Ruby
Raw Normal View History

2012-10-07 03:38:38 +00:00
module JamWebsockets
class ClientContext
2020-12-27 23:58:31 +00:00
attr_accessor :user, :client, :msg_count, :session, :client_type, :sent_bad_state_previously, :active, :updated_at
2014-04-30 03:01:28 +00:00
def initialize(user, client, client_type)
@user = user
2012-10-02 05:03:08 +00:00
@client = client
2014-04-30 03:01:28 +00:00
@client_type = client_type
2012-10-02 05:03:08 +00:00
@msg_count = 0
@session = nil
@sent_bad_state_previously = false
2016-01-19 00:41:53 +00:00
@active = true
2020-12-27 23:58:31 +00:00
@updated_at = Time.now
2014-04-30 03:01:28 +00:00
client.context = self
end
2020-12-27 23:58:31 +00:00
def stale?(stale_time)
return Time.now - @updated_at > stale_time
end
2012-10-02 05:03:08 +00:00
def to_s
return "Client[user:#{@user} client:#{@client.client_id} msgs:#{@msg_count} session:#{@session} client_type:#{@client_type} channel_id: #{@client.channel_id}]"
2012-10-02 05:03:08 +00:00
end
2014-04-30 03:01:28 +00:00
def to_json
{user_id: @user.id, client_id: @client.client_id, msg_count: @msg_count, client_type: @client_type, socket_id: @client.channel_id}.to_json
2014-04-30 03:01:28 +00:00
end
def hash
@client.hash
end
def ==(o)
o.class == self.class && o.client == @client
end
alias_method :eql?, :==
end
end