- 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')
{SocketIoMessageHandler} = require './lib/clientproxy'
proxy = require './lib/clientproxy'
crypto = require 'crypto'
io = require('socket.io').listen(3019)
sio = new SocketIoMessageHandler(io)
proxy.listen(3019)

View File

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

View File

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

View File

@ -2,34 +2,16 @@ postal = require('postal')
redisrpc = require './redispubsub'
crypto = require 'crypto'
io = require('socket.io').listen(3009)
io.sockets.on('connection', (socket) ->
console.log("Connected")
socket.on('login', (data) ->
console.log("User login #{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')
)
receiveMessage = (callback) ->
postal.subscribe({
channel: replyTo.channel,
topic: replyTo.topic,
callback: (msg, envelope) ->
callback( msg.err, msg.data )
})
sendMessage = (data, callback) ->
exports.sendMessage = (data, callback) ->
replyTo = {
channel: 'model.data',
topic: 'get.' + crypto.randomBytes(16).toString('hex')