new SessionService(opts)
Session service maintains the internal session for each client connection.
Session service is created by session component and is only
available in frontend servers. You can access the service by
`app.get('sessionService')` or `app.sessionService` in frontend servers.
Parameters:
| Name | Type | Description |
|---|---|---|
opts |
Object | constructor parameters |
Methods
-
bind()
-
Bind the session with a user id.
-
create(sid, frontendId, socket) → {Session}
-
Create and return internal session.
Parameters:
Name Type Description sidInteger uniqe id for the internal session frontendIdString frontend server in which the internal session is created socketObject the underlying socket would be held by the internal session Returns:
Session -
forEachBindedSession(cb)
-
Iterate all the binded session in the session service.
Parameters:
Name Type Description cbfunction callback function to fetch session -
forEachSession(cb)
-
Iterate all the session in the session service.
Parameters:
Name Type Description cbfunction callback function to fetch session -
get(id) → {Session}
-
Get session by id.
Parameters:
Name Type Description idNumber The session id Returns:
Session -
getByUid(uid) → {Array}
-
Get sessions by userId.
Parameters:
Name Type Description uidNumber User id associated with the session Returns:
Array - list of session binded with the uid -
getClientAddressBySessionId(sid) → {Object}
-
Get client remote address by session id.
Parameters:
Name Type Description sidNumber session id Returns:
Object - remote address of client -
getSessionsCount()
-
Get sessions' quantity in specified server.
-
import()
-
Import the key/value into session.
-
importAll()
-
Import new value for the existed session.
-
kick(uid, cb)
-
Kick all the session offline under the user id.
Parameters:
Name Type Description uidNumber user id asscociated with the session cbfunction callback function -
kickBySessionId(sid, cb)
-
Kick a user offline by session id.
Parameters:
Name Type Description sidNumber session id cbfunction callback function -
remove(sid)
-
Remove session by key.
Parameters:
Name Type Description sidNumber The session id -
sendMessage(sid, msg)
-
Send message to the client by session id.
Parameters:
Name Type Description sidString session id msgObject message to send -
sendMessageByUid(uid, msg)
-
Send message to the client by user id.
Parameters:
Name Type Description uidString userId msgObject message to send -
unbind()
-
Unbind a session with the user id.