2014-04-14 08:16:37 +08:00
|
|
|
socketio = require('socket.io')
|
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
config = require '../config'
|
2014-04-14 07:39:18 +08:00
|
|
|
log = require './bbblogger'
|
|
|
|
|
|
|
|
MESSAGE = "message"
|
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
moduleDeps = ["Controller"]
|
2014-04-14 07:39:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
module.exports = class ClientProxy
|
2014-04-14 07:39:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
constructor: ->
|
|
|
|
config.modules.wait moduleDeps, =>
|
|
|
|
@controller = config.modules.get("Controller")
|
2014-04-14 07:39:18 +08:00
|
|
|
|
2014-04-24 06:27:43 +08:00
|
|
|
# Listens for events on the websocket and does something when they are triggered.
|
2014-04-24 04:18:25 +08:00
|
|
|
listen: (app) ->
|
2014-04-24 06:27:43 +08:00
|
|
|
@io = socketio.listen(app)
|
|
|
|
@io.set('log level', 1)
|
|
|
|
@io.sockets.on 'connection', (socket) =>
|
2014-04-25 05:33:59 +08:00
|
|
|
log.debug({ client: socket.id }, "Client has connected.")
|
2014-04-24 04:18:25 +08:00
|
|
|
socket.on 'message', (jsonMsg) =>
|
2014-05-30 03:55:32 +08:00
|
|
|
log.debug({ message: jsonMsg }, "Received message")
|
2014-04-24 04:18:25 +08:00
|
|
|
@_handleMessage(socket, jsonMsg)
|
|
|
|
socket.on 'disconnect', =>
|
|
|
|
@_handleClientDisconnected socket
|
2014-04-14 07:39:18 +08:00
|
|
|
|
2014-04-24 06:27:43 +08:00
|
|
|
# Sends a message in `data` to all the clients that should receive it.
|
|
|
|
sendToClients: (data, callback) ->
|
2014-05-21 02:54:20 +08:00
|
|
|
#log.debug({ data: data }, "Sending to client")
|
2014-05-09 05:32:09 +08:00
|
|
|
|
2014-04-24 06:27:43 +08:00
|
|
|
# the channel can be the user_id (send to one user only) or the meeting_id
|
|
|
|
# (send to everyone in the meeting)
|
|
|
|
channel = data?.payload?.user_id or data?.payload?.meeting_id
|
|
|
|
|
|
|
|
# if the data has "header":{"name":"some_event_name"} use that name
|
|
|
|
# otherwise look for "name":"some_event_name" in the top level of the data
|
|
|
|
eventName = data?.header?.name or data?.name
|
|
|
|
|
2014-04-25 05:33:59 +08:00
|
|
|
# clients = @io.sockets.clients(channel)
|
|
|
|
# console.log "Found", clients?.length, "clients for the channel", channel
|
|
|
|
|
2014-05-21 02:54:20 +08:00
|
|
|
#log.debug({ channel: channel, eventName: eventName, message: data, clientCount: clients?.length },
|
|
|
|
# "Sending message to websocket clients")
|
|
|
|
|
2014-04-25 05:33:59 +08:00
|
|
|
# TODO: if `channel` is undefined, it should not send the message,
|
|
|
|
# instead if is sending to all users
|
|
|
|
@io.sockets.in(channel).emit(eventName, data)
|
2014-04-24 06:27:43 +08:00
|
|
|
callback?()
|
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
_handleClientDisconnected: (socket) ->
|
2014-05-30 23:57:18 +08:00
|
|
|
console.log "\ntrying to disconnect"
|
2014-05-30 03:55:32 +08:00
|
|
|
|
|
|
|
#if socket.userId?
|
|
|
|
# log.info("User [#{socket.userId}] has disconnected.")
|
2014-04-14 07:39:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
_handleMessage: (socket, message) ->
|
|
|
|
if message.header?.name?
|
|
|
|
@_handleValidMessage(socket, message)
|
2014-04-14 07:39:18 +08:00
|
|
|
else
|
2014-04-24 06:27:43 +08:00
|
|
|
log.error({ message: message }, "Invalid message.")
|
2014-04-24 04:18:25 +08:00
|
|
|
|
2014-05-26 22:24:56 +08:00
|
|
|
_handleValidMessage: (socket, message) =>
|
2014-04-24 04:18:25 +08:00
|
|
|
switch message.header.name
|
2014-05-16 02:43:17 +08:00
|
|
|
when 'validate_auth_token'
|
2014-04-24 04:18:25 +08:00
|
|
|
@_handleLoginMessage socket, message
|
2014-05-26 22:24:56 +08:00
|
|
|
when 'send_public_chat_message'
|
|
|
|
@controller.sendingChat message
|
2014-05-30 03:55:32 +08:00
|
|
|
when 'user_leaving_request'
|
|
|
|
@controller.sendingUsersMessage message
|
2014-04-24 04:18:25 +08:00
|
|
|
else
|
2014-04-24 06:27:43 +08:00
|
|
|
log.error({ message: message }, 'Unknown message name.')
|
2014-04-24 04:18:25 +08:00
|
|
|
|
|
|
|
_handleLoginMessage: (socket, data) ->
|
2014-05-21 02:54:20 +08:00
|
|
|
@controller.processAuthMessage(data, (err, result) ->
|
2014-04-24 04:18:25 +08:00
|
|
|
if err?
|
2014-04-25 05:33:59 +08:00
|
|
|
log.debug({ message: result }, "Sending authentication not OK to user and disconnecting socket")
|
|
|
|
sendMessageToClient(socket, result)
|
2014-04-24 04:18:25 +08:00
|
|
|
socket.disconnect()
|
|
|
|
else
|
2014-04-25 05:33:59 +08:00
|
|
|
log.debug({ userChannel: result.payload.user_id, meetingChannel: result.payload.meeting_id },
|
|
|
|
"Subscribing a user to his channels")
|
|
|
|
socket.join(result.payload.user_id)
|
|
|
|
socket.join(result.payload.meeting_id)
|
|
|
|
|
|
|
|
# assign the userId to this socket. This way we can
|
2014-04-24 04:18:25 +08:00
|
|
|
# locate this socket using the userId.
|
2014-04-25 05:33:59 +08:00
|
|
|
socket.userId = result?.payload?.user_id
|
|
|
|
|
|
|
|
log.debug({ message: result }, "Sending authentication OK reply to user")
|
|
|
|
sendMessageToClient(socket, result)
|
2014-05-21 02:54:20 +08:00
|
|
|
)
|
2014-04-14 07:39:18 +08:00
|
|
|
|
|
|
|
sendMessageToClient = (socket, message) ->
|
2014-04-25 05:33:59 +08:00
|
|
|
socket.emit(MESSAGE, message)
|