Skip to content

Commit

Permalink
Update to Socket.IO 1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorccu committed Jun 3, 2014
1 parent 27d9014 commit 41661c9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"angular-animate": "~1.3.0-beta.8",
"angular-touch": "~1.3.0-beta.8",
"se7en-bootstrap-3": "[email protected]:stf/se7en-bootstrap-3.git#46b7dd0792cfdc66bf01e351cd680de88740d60c",
"socket.io-client": "~0.9.16",
"socket.io-client": "~1.0.4",
"lodash": "~2.4.1",
"oboe": "~1.14.7",
"fa-borderlayout": "[email protected]:gunther-brunner/fa-borderlayout.git",
Expand Down
74 changes: 37 additions & 37 deletions lib/roles/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var Promise = require('bluebird')
var httpProxy = require('http-proxy')
var _ = require('lodash')
var request = Promise.promisifyAll(require('request'))
var proxyaddr = require('proxy-addr')

var logger = require('../util/logger')
var pathutil = require('../util/pathutil')
Expand All @@ -34,9 +35,13 @@ module.exports = function(options) {
var log = logger.createLogger('app')
, app = express()
, server = http.createServer(app)
, io = socketio.listen(server)
, io = socketio.listen(server, {
serveClient: false
, transports: ['websocket']
})
, channelRouter = new events.EventEmitter()
, proxy = httpProxy.createProxyServer()
, sessionMiddleware

proxy.on('error', function(err) {
log.error('Proxy had an error', err.stack)
Expand All @@ -46,11 +51,6 @@ module.exports = function(options) {
app.set('views', pathutil.resource('app/views'))
app.set('strict routing', true)
app.set('case sensitive routing', true)
app.set('trust proxy', true)

io.set('log level', 1)
io.set('browser client', false)
io.set('transports', ['websocket'])

if (!options.disableWatch) {
app.use('/static/build', webpack({
Expand Down Expand Up @@ -78,7 +78,7 @@ module.exports = function(options) {
app.use(serveFavicon(pathutil.resource(
'bower_components/stf-graphics/logo/exports/STF-128.png')))

app.use(cookieSession({
app.use(sessionMiddleware = cookieSession({
name: options.ssid
, keys: [options.secret]
}))
Expand Down Expand Up @@ -230,45 +230,45 @@ module.exports = function(options) {
})
})

io.set('authorization', (function() {
var session = Promise.promisify(cookieSession({
name: options.ssid
, keys: [options.secret]
}))
return function(handshake, accept) {
var res = Object.create(null)
session(handshake, res)
.then(function() {
var token = handshake.session.jwt
if (token) {
return dbapi.loadUser(token.email)
.then(function(user) {
if (user) {
handshake.user = user
accept(null, true)
}
else {
accept(null, false)
}
})
io.use(function(socket, next) {
var req = socket.request
, res = Object.create(null)
sessionMiddleware(req, res, next)
})

io.use(function(socket, next) {
var req = socket.request
// This is similar to what Express does behind the scenes
req.ip = proxyaddr(req, app.get('trust proxy fn'))
next()
})

io.use(function(socket, next) {
var req = socket.request
, token = req.session.jwt
if (token) {
return dbapi.loadUser(token.email)
.then(function(user) {
if (user) {
req.user = user
next()
}
else {
accept(null, false)
next(new Error('Invalid user'))
}
})
.catch(function() {
accept(null, false)
})
.catch(next)
}
})())
else {
next(new Error('Missing authorization token'))
}
})

io.on('connection', function(socket) {
var channels = []
, user = socket.handshake.user
, ip = socket.handshake.headers['x-forwarded-for'] ||
socket.handshake.address.address
, user = socket.request.user

socket.emit('socket.ip', ip)
socket.emit('socket.ip', socket.request.ip)

function joinChannel(channel) {
channels.push(channel)
Expand Down
5 changes: 3 additions & 2 deletions res/app/components/stf/socket/socket-service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var io = require('socket.io')

module.exports = function SocketFactory($rootScope, VersionUpdateService) {
var socket = io.connect(null, {
reconnect: false
var socket = io('/', {
reconnection: false
, transports: ['websocket']
})

socket.scoped = function($scope) {
Expand Down
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
, 'angular-growl': 'angular-growl-v2/build/angular-growl.js'
, 'angular-growl-css': 'angular-growl-v2/build/angular-growl.min.css'
, 'localforage': 'localforage/dist/localforage.js'
, 'socket.io': 'socket.io-client/dist/socket.io'
, 'socket.io': 'socket.io-client/socket.io.js'
, 'oboe': 'oboe/dist/oboe-browser'
, 'ng-file-upload-shim5': 'ng-file-upload/angular-file-upload-html5-shim'
, 'ng-file-upload-main': 'ng-file-upload/angular-file-upload'
Expand Down Expand Up @@ -63,6 +63,7 @@ module.exports = {
, { test: /ui-bootstrap-tpls\.js/, loader: 'script'}
, { test: /dialogs\.js/, loader: 'script'}
, { test: /bluebird\.js/, loader: 'imports?require=>undefined'}
, { test: /socket\.io\.js/, loader: 'imports?require=>undefined'}
]
, noParse: [
// pathutil.resource('bower_components')
Expand Down

0 comments on commit 41661c9

Please sign in to comment.