server.js | |
---|---|
Application Server | |
This is our single | |
dependenciesThe server won't do everything for us out of the box, a few packages
are required to make this process a little easier. All of these can
be installed with | var express = require('express')
, http = require('http')
, app = express()
, server = http.createServer(app)
, io = require('socket.io')
, io = io.listen(server)
, port = 9090 |
Socket listeners | |
connectionA new | io.sockets.on('connection', function(socket) { |
Someone connected | socket.broadcast.emit('connected', socket.id)
|
Re-broadcast the message | socket.on('message', function(data) {
socket.broadcast.emit('message', {
id: socket.id
, msg: data
})
}) |
Someone has disconnected | socket.on('disconnect', function() {
io.sockets.emit('disconnected', socket.id)
})
}) |
Express setup | |
Configure the | app.configure(function() {
app.use(express.static(__dirname + '/public'))
app.set('view engine', 'jade')
}) |
Routes | |
Create a route handler for the application, in this example we will
only need a single route, | app.get('/', function(req, res) {
res.render('index.jade')
}) |
Start server | |
Tell the | server.listen(port, function() {
console.log('Application listening on port: ' + port)
})
|