Class: Citrus::Common::Service::SessionService::Session

Inherits:
Object
  • Object
show all
Includes:
Utils::EventEmitter
Defined in:
lib/citrus/common/service/session_service.rb

Overview

Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::EventEmitter

#emit, #on, #once

Constructor Details

#initialize(sid, frontend_id, socket, service) ⇒ Session

Create a new session

Parameters:

  • sid (Integer)
  • frontend_id (String)
  • socket (Object)
  • service (Object)


288
289
290
291
292
293
294
295
296
297
# File 'lib/citrus/common/service/session_service.rb', line 288

def initialize sid, frontend_id, socket, service
  @id = sid
  @frontend_id = frontend_id
  @uid = nil
  @settings = {}

  @socket = socket
  @session_service = service
  @state = :state_inited
end

Instance Attribute Details

#frontend_idObject (readonly)

Returns the value of attribute frontend_id.



280
281
282
# File 'lib/citrus/common/service/session_service.rb', line 280

def frontend_id
  @frontend_id
end

#idObject (readonly)

Returns the value of attribute id.



280
281
282
# File 'lib/citrus/common/service/session_service.rb', line 280

def id
  @id
end

#session_serviceObject (readonly)

Returns the value of attribute session_service.



280
281
282
# File 'lib/citrus/common/service/session_service.rb', line 280

def session_service
  @session_service
end

#settingsObject (readonly)

Returns the value of attribute settings.



280
281
282
# File 'lib/citrus/common/service/session_service.rb', line 280

def settings
  @settings
end

#uidObject (readonly)

Returns the value of attribute uid.



280
281
282
# File 'lib/citrus/common/service/session_service.rb', line 280

def uid
  @uid
end

Instance Method Details

#bind(uid) ⇒ Object

Bind the session with the uid

Parameters:

  • uid (String)


307
308
309
310
# File 'lib/citrus/common/service/session_service.rb', line 307

def bind uid
  @uid = uid
  emit :bind, uid
end

#closed(reason = '') ⇒ Object

Closed callback for the session which would disconnect client in next tick

Parameters:

  • reason (String) (defaults to: '')


352
353
354
355
356
357
358
359
360
361
362
# File 'lib/citrus/common/service/session_service.rb', line 352

def closed reason=''
  return if @state == :state_closed

  @state = :state_closed
  @service.remove @id

  emit :closed, to_frontend_session, reason
  @socket.emit :closing, reason

  EM.next_tick { @socket.disconnect }
end

#get(key) ⇒ Object

Get value from the session

Parameters:

  • key (String)


331
332
333
# File 'lib/citrus/common/service/session_service.rb', line 331

def get key
  @settings[key]
end

#send(msg) ⇒ Object

Send message to the session

Parameters:

  • msg (Hash)


338
339
340
# File 'lib/citrus/common/service/session_service.rb', line 338

def send msg
  @socket.send msg
end

#send_batch(msgs) ⇒ Object

Send message to the session in batch

Parameters:

  • msgs (Array)


345
346
347
# File 'lib/citrus/common/service/session_service.rb', line 345

def send_batch msgs
  @socket.send_batch msgs
end

#set(key, value) ⇒ Object

Set value for the session

Parameters:

  • key (String)
  • value (Hash)


324
325
326
# File 'lib/citrus/common/service/session_service.rb', line 324

def set key, value
  @settings[key] = value
end

#to_frontend_sessionObject

Export current session as frontend session



300
301
302
# File 'lib/citrus/common/service/session_service.rb', line 300

def to_frontend_session
  FrontendSession.new self
end

#unbind(uid) ⇒ Object

Unbind the session with the uid

Parameters:

  • uid (String)


315
316
317
318
# File 'lib/citrus/common/service/session_service.rb', line 315

def unbind uid
  @uid = nil
  emit :unbind, uid
end