mirror of
https://github.com/sstent/node.git
synced 2026-01-27 23:51:45 +00:00
updated app
This commit is contained in:
108
node_modules/derby-examples/chat/src/chat/index.coffee
generated
vendored
Normal file
108
node_modules/derby-examples/chat/src/chat/index.coffee
generated
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
# Derby exposes framework features on this module
|
||||
{get, view, ready} = require('derby').createApp module
|
||||
|
||||
## ROUTES ##
|
||||
|
||||
NUM_USER_IMAGES = 10
|
||||
|
||||
get '/:room?', (page, model, {room}) ->
|
||||
# Redirect users to URLs that only contain letters, numbers, and hyphens
|
||||
return page.redirect '/lobby' unless room && /^[-\w ]+$/.test room
|
||||
_room = room.toLowerCase().replace /[_ ]/g, '-'
|
||||
return page.redirect "/#{_room}" if _room != room
|
||||
|
||||
# Render page if a userId is already stored in session data
|
||||
if userId = model.get '_session.userId'
|
||||
return getRoom page, model, room, userId
|
||||
|
||||
# Otherwise, select a new userId and initialize user
|
||||
model.async.incr 'configs.1.nextUserId', (err, userId) ->
|
||||
model.set '_session.userId', userId
|
||||
model.set "users.#{userId}",
|
||||
name: 'User ' + userId
|
||||
picClass: 'pic' + (userId % NUM_USER_IMAGES)
|
||||
getRoom page, model, room, userId
|
||||
|
||||
getRoom = (page, model, roomName, userId) ->
|
||||
model.subscribe "rooms.#{roomName}", 'users', (err, room, users) ->
|
||||
model.ref '_room', room
|
||||
|
||||
# setNull will set a value if the object is currently null or undefined
|
||||
model.setNull '_room.messages', []
|
||||
|
||||
# Any path name that starts with an underscore is private to the current
|
||||
# client. Nothing set under a private path is synced back to the server.
|
||||
model.set '_newComment', ''
|
||||
model.ref '_user', "users.#{userId}"
|
||||
model.fn '_numMessages', '_room.messages', (messages) -> messages.length
|
||||
|
||||
page.render()
|
||||
|
||||
|
||||
## CONTROLLER FUNCTIONS ##
|
||||
|
||||
ready (model) ->
|
||||
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
|
||||
displayTime = (time) ->
|
||||
time = new Date time
|
||||
hours = time.getHours()
|
||||
period = if hours < 12 then ' am, ' else ' pm, '
|
||||
hours = (hours % 12) || 12
|
||||
minutes = time.getMinutes()
|
||||
minutes = '0' + minutes if minutes < 10
|
||||
return hours + ':' + minutes + period + months[time.getMonth()] +
|
||||
' ' + time.getDate() + ', ' + time.getFullYear()
|
||||
|
||||
# Display times are only set client-side, since the timezone is not known
|
||||
# when performing server-side rendering
|
||||
for message, i in model.get '_room.messages'
|
||||
# _displayTime starts with an underscore so that its value is not
|
||||
# stored or sent to other clients
|
||||
model.set "_room.messages.#{i}._displayTime", displayTime message.time
|
||||
|
||||
|
||||
# Exported functions are exposed as a global in the browser with the same
|
||||
# name as the module that includes Derby. They can also be bound to DOM
|
||||
# events using the "x-bind" attribute in a template.
|
||||
model.set '_showReconnect', true
|
||||
exports.connect = ->
|
||||
# Hide the reconnect link for a second after clicking it
|
||||
model.set '_showReconnect', false
|
||||
setTimeout (-> model.set '_showReconnect', true), 1000
|
||||
model.socket.socket.connect()
|
||||
|
||||
exports.reload = -> window.location.reload()
|
||||
|
||||
|
||||
messages = document.getElementById 'messages'
|
||||
messageList = document.getElementById 'messageList'
|
||||
atBottom = true
|
||||
|
||||
# Derby emits pre mutator events after the model has been updated, but
|
||||
# before the view bindings have been updated
|
||||
model.on 'pre:push', '_room.messages', ->
|
||||
# Check to see if the page is scrolled to the bottom before adding
|
||||
# a new message
|
||||
bottom = messageList.offsetHeight
|
||||
containerHeight = messages.offsetHeight
|
||||
scrollBottom = messages.scrollTop + containerHeight
|
||||
atBottom = bottom < containerHeight || scrollBottom == bottom
|
||||
|
||||
# Regular model mutator events are emitted after both the model and view
|
||||
# bindings have been updated
|
||||
model.on 'push', '_room.messages', (message, len, isLocal) ->
|
||||
# Scoll page when adding a message or when another user adds a message
|
||||
# and the page is already at the bottom
|
||||
if isLocal || atBottom
|
||||
messages.scrollTop = messageList.offsetHeight
|
||||
# Update display time using local timezone
|
||||
model.set "_room.messages.#{len - 1}._displayTime", displayTime message.time
|
||||
|
||||
|
||||
exports.postMessage = ->
|
||||
model.push '_room.messages',
|
||||
userId: model.get '_session.userId'
|
||||
comment: model.get '_newComment'
|
||||
time: +new Date
|
||||
|
||||
model.set '_newComment', ''
|
||||
12
node_modules/derby-examples/chat/src/chat/inline.coffee
generated
vendored
Normal file
12
node_modules/derby-examples/chat/src/chat/inline.coffee
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
# Scripts required to properly render the page can be put in a file called
|
||||
# "inline.js". This file will be automatically inserted before the external
|
||||
# scripts are included.
|
||||
|
||||
messages = document.getElementById 'messages'
|
||||
messageList = document.getElementById 'messageList'
|
||||
commentInput = document.getElementById 'commentInput'
|
||||
do window.onresize = ->
|
||||
# Scroll the message list to the bottom
|
||||
messages.scrollTop = messageList.offsetHeight
|
||||
# Use HTML5 autofocus if supported. Otherwise, focus manually
|
||||
commentInput.focus() unless 'autofocus' of commentInput
|
||||
57
node_modules/derby-examples/chat/src/server/index.coffee
generated
vendored
Normal file
57
node_modules/derby-examples/chat/src/server/index.coffee
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
http = require 'http'
|
||||
path = require 'path'
|
||||
express = require 'express'
|
||||
gzippo = require 'gzippo'
|
||||
MongoStore = require('connect-mongo')(express)
|
||||
derby = require 'derby'
|
||||
chat = require '../chat'
|
||||
serverError = require './serverError'
|
||||
|
||||
|
||||
## SERVER CONFIGURATION ##
|
||||
|
||||
ONE_YEAR = 1000 * 60 * 60 * 24 * 365
|
||||
root = path.dirname path.dirname __dirname
|
||||
publicPath = path.join root, 'public'
|
||||
|
||||
(expressApp = express())
|
||||
.use(express.favicon())
|
||||
# Gzip static files and serve from memory
|
||||
.use(gzippo.staticGzip publicPath, maxAge: ONE_YEAR)
|
||||
|
||||
# Gzip dynamically rendered content
|
||||
.use(express.compress())
|
||||
|
||||
# Uncomment to add form data parsing support
|
||||
# .use(express.bodyParser())
|
||||
# .use(express.methodOverride())
|
||||
|
||||
# Derby session middleware creates req.model and subscribes to _session
|
||||
.use(express.cookieParser 'secret_sauce')
|
||||
.use(express.session
|
||||
cookie: {maxAge: ONE_YEAR}
|
||||
store: new MongoStore(db: 'derby-chat', collection: 'express-sessions')
|
||||
)
|
||||
.use(chat.session())
|
||||
|
||||
# The router method creates an express middleware from the app's routes
|
||||
.use(chat.router())
|
||||
.use(expressApp.router)
|
||||
.use(serverError root)
|
||||
|
||||
module.exports = server = http.createServer expressApp
|
||||
|
||||
|
||||
## SERVER ONLY ROUTES ##
|
||||
|
||||
expressApp.all '*', (req) ->
|
||||
throw "404: #{req.url}"
|
||||
|
||||
|
||||
## STORE SETUP ##
|
||||
|
||||
derby.use(require 'racer-db-mongo')
|
||||
|
||||
chat.createStore
|
||||
listen: server
|
||||
db: {type: 'Mongo', uri: 'mongodb://localhost/derby-chat'}
|
||||
18
node_modules/derby-examples/chat/src/server/serverError.coffee
generated
vendored
Normal file
18
node_modules/derby-examples/chat/src/server/serverError.coffee
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
derby = require 'derby'
|
||||
{isProduction} = derby.util
|
||||
|
||||
module.exports = (root) ->
|
||||
staticPages = derby.createStatic root
|
||||
|
||||
return (err, req, res, next) ->
|
||||
return next() unless err?
|
||||
|
||||
console.log(if err.stack then err.stack else err)
|
||||
|
||||
## Customize error handling here ##
|
||||
message = err.message || err.toString()
|
||||
status = parseInt message
|
||||
if status is 404
|
||||
staticPages.render '404', res, {url: req.url}, 404
|
||||
else
|
||||
res.send if 400 <= status < 600 then status else 500
|
||||
Reference in New Issue
Block a user