- don't use classes

This commit is contained in:
Richard Alam 2014-04-13 17:16:37 -07:00
parent 8d50ea3e7f
commit df9ac02e79
4 changed files with 54 additions and 75 deletions

View File

@ -1,9 +1,9 @@
postal = require('postal') postal = require('postal')
{SocketIoMessageHandler} = require './lib/clientproxy' proxy = require './lib/clientproxy'
crypto = require 'crypto' crypto = require 'crypto'
io = require('socket.io').listen(3019) proxy.listen(3019)
sio = new SocketIoMessageHandler(io)

View File

@ -1,42 +1,39 @@
{Controller} = require './controller' socketio = require('socket.io')
controller = require './controller'
log = require './bbblogger' log = require './bbblogger'
MESSAGE = "message" MESSAGE = "message"
class SocketIoMessageHandler io = null
constructor: (io) ->
@io = io
@controller = new Controller(@)
@initialize()
initialize: () -> exports.listen = (app) ->
@io.sockets.on('connection', (socket) -> io = socketio.listen(app)
console.log("Client has connected.") io.sockets.on('connection', (socket) ->
log.debug("LOG: Client has connected.") console.log("Client has connected.")
socket.on('message', (jsonMsg) -> log.debug("LOG: Client has connected.")
log.debug("Received message #{jsonMsg}") socket.on('message', (jsonMsg) ->
handleMessage(socket, @controller, jsonMsg) log.debug("Received message #{jsonMsg}")
) handleMessage(socket, jsonMsg)
socket.on('disconnect', () ->
handleClientDisconnected socket
)
) )
socket.on('disconnect', () ->
exports.SocketIoMessageHandler = SocketIoMessageHandler handleClientDisconnected socket
)
)
handleClientDisconnected = (socket) -> handleClientDisconnected = (socket) ->
log.debug("LOG: Client has disconnected.") log.debug("LOG: Client has disconnected.")
handleMessage = (socket, controller, message) -> handleMessage = (socket, message) ->
if message.name? if message.name?
handleValidMessage(socket, controller, message) handleValidMessage(socket, message)
else else
log.error({error: "Invalid message", message: JSON.stringify(message)}) log.error({error: "Invalid message", message: JSON.stringify(message)})
handleValidMessage = (socket, controller, message) -> handleValidMessage = (socket, message) ->
switch message.name switch message.name
when 'authenticateMessage' when 'authenticateMessage'
handleLoginMessage socket, controller, message handleLoginMessage socket, message
when 'Sandra' when 'Sandra'
connectJackNumber 22 connectJackNumber 22
when 'Toby' when 'Toby'
@ -44,15 +41,15 @@ handleValidMessage = (socket, controller, message) ->
else else
console.log('I am sorry, your call cannot be connected') console.log('I am sorry, your call cannot be connected')
handleLoginMessage = (socket, controller, data) -> handleLoginMessage = (socket, data) ->
controller.processLoginMessage(data, (err, result) -> controller.processLoginMessage(data, (err, result) ->
if (err) if (err)
message = {name: "authenticationReply", error: err} message = {name: "authenticationReply", error: err}
sendMessageToClient message sendMessageToClient socket, message
else else
message = {name: "authenticationReply", data: result} message = {name: "authenticationReply", data: result}
sendMessageToClient message sendMessageToClient socket, message
) )
sendMessageToClient = (socket, message) -> sendMessageToClient = (socket, message) ->
socket.emit(MESSAGE, JSON.stringify(message)) socket.emit(MESSAGE, JSON.stringify(message))
@ -61,3 +58,9 @@ sendMessageToClient = (socket, message) ->

View File

@ -1,22 +1,16 @@
bus = require './messagebus' bus = require './messagebus'
logger = require './bbblogger' log = require './bbblogger'
class Controller exports.processLoginMessage = (data, callback) ->
constructor: (clientProxy) -> bus.sendMessage(data, (err, result) ->
@proxy = clientProxy if (err)
errLog = {message: "Authentication Failure", reason: err, data: data}
processLoginMessage = (data, callback) -> log.error(JSON.stringify(errLog))
bus.sendMessage(data, (err, result) -> callback(err, null)
if (err) else
errLog = {message: "Authentication Failure", reason: err, data: data} console.log("SUCCESS: #{result}")
log.error(JSON.stringify(errLog)) if result.error?
callback(err, null) callback(result.error, null)
else else
console.log("SUCCESS: #{result}") callback(null, result.data)
if result.error? )
callback(result.error, null)
else
callback(null, result.data)
)
exports.Controller = Controller

View File

@ -2,34 +2,16 @@ postal = require('postal')
redisrpc = require './redispubsub' redisrpc = require './redispubsub'
crypto = require 'crypto' crypto = require 'crypto'
io = require('socket.io').listen(3009) receiveMessage = (callback) ->
postal.subscribe({
io.sockets.on('connection', (socket) -> channel: replyTo.channel,
console.log("Connected") topic: replyTo.topic,
socket.on('login', (data) -> callback: (msg, envelope) ->
console.log("User login #{data}") callback( msg.err, msg.data )
sendMessage(data, (err, result) -> })
if (err)
console.log("ERROR: #{err}")
else
console.log("SUCCESS: #{result}")
socket.emit('authenticated', JSON.stringify(result))
)
)
socket.on('event', (data) ->
console.log("Server received event.")
)
socket.on('disconnect', () ->
console.log("Server disconnect event.")
)
socket.emit('ready')
)
sendMessage = (data, callback) -> exports.sendMessage = (data, callback) ->
replyTo = { replyTo = {
channel: 'model.data', channel: 'model.data',
topic: 'get.' + crypto.randomBytes(16).toString('hex') topic: 'get.' + crypto.randomBytes(16).toString('hex')