Permalink
Browse files

server

  • Loading branch information...
1 parent 80780e2 commit 04bad5b709feec5cc2ddbe87bc5fd7461c1d2902 @meelash meelash committed May 9, 2012
Showing with 296 additions and 1 deletion.
  1. +15 −1 README.md
  2. +14 −0 answer.coffee
  3. +35 −0 answer.js
  4. +55 −0 main.coffee
  5. +97 −0 main.js
  6. +15 −0 question.coffee
  7. +37 −0 question.js
  8. +7 −0 server.coffee
  9. +21 −0 server.js
View
@@ -1,2 +1,16 @@
quora_clone
-===========
+===========
+
+http://192.168.1.111:3000
+
+client-side:
+socket.emit-
+ question_added (questionText)
+ answer_added (questionId, answerText)
+ answer_upvote (answerId)
+ question_followed (questionId)
+
+socket.on-
+ question_added (questionId, questionText)
+ answer_added (questionId, answer)
+ answer_upvoted (answerId)
View
@@ -0,0 +1,14 @@
+{EventEmitter} = require('events')
+
+class Answer extends EventEmitter
+ constructor:(@text)->
+ @votes = {}
+ @voteCount = 0
+
+ upvote:(userId)->
+ unless @votes[userId]?
+ @votes[userId] = 'up'
+ @voteCount++
+ @emit 'upvoted', @voteCount
+
+module.exports = Answer
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,55 @@
+{EventEmitter} = require('events')
+Question = require './question.js'
+Answer = require './answer.js'
+
+class User
+ constructor:(@socket)->
+ @id = socket.sessionId
+ @questions = []
+ GlobalEvents.on 'question_added', (questionId, questionText)->
+ @socket.emit 'question_added', questionId, questionText
+
+ @api =
+ question_added : @addQuestion
+ answer_added : @addAnswer
+ answer_upvote : @upvoteAnswer
+ question_followed : @followQuestion
+
+ addQuestion:(questionText)->
+ questionId = createUniqueId()
+ questions[questionId] = new Question questionText
+ @followQuestion questionId
+ GlobalEvents.emit 'question_added', questionId, questionText
+
+ addAnswer:(questionId, answerText)->
+ answerId = createUniqueId()
+ answers[answerId] = answer = new Answer answerText
+ question[questionId].addAnswer answer
+
+ upvoteAnswer:(answerId)->
+ answers[answerId].upvote(@id)
+
+ followQuestion:(questionId)->
+ @questions.push questionId
+ question = questions[questionId]
+ question.on 'answer_added', (answer)->
+ @socket.emit 'answer_added', questionId, answer
+ question.on 'answer_upvoted', (count)->
+ @socket.emit 'answer_upvoted', count
+
+
+
+questions = {}
+answers = {}
+
+uniqueId = 0
+createUniqueId = ->
+ uniqueId++
+
+
+class GlobalEvents extends EventEmitter
+
+GlobalEvents = new GlobalEvents
+
+
+module.exports = {User}
View
97 main.js

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,15 @@
+{EventEmitter} = require('events')
+Answer = require './answer.js'
+
+class Question extends EventEmitter
+ constructor:(@text)->
+ @answers = []
+
+ addAnswer:(answer)->
+ @answers.push answer
+ @emit 'answer_added', answer
+ answer.on 'upvoted', (count)=>
+ @emit 'answer_upvoted', count
+
+
+module.exports = Question
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,7 @@
+{User} = require './main.js'
+
+io = (require 'socket.io').listen(3000)
+
+io.sockets.on 'connection', (socket)->
+ user = new User socket
+ socket.on event, route for event, route of user.api
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 04bad5b

Please sign in to comment.