2011-11-06 12:21:27 -08:00
|
|
|
fs = require 'fs'
|
|
|
|
redis = require 'redis'
|
2013-05-01 12:51:59 -07:00
|
|
|
log = require './log.js'
|
2012-03-28 19:13:30 -07:00
|
|
|
|
2013-08-12 12:39:03 -07:00
|
|
|
REDIS_SERVER = if process.env.NODE_ENV == 'development' then 'localhost' else 'db_redis_pubsub'
|
2012-12-24 11:53:00 -08:00
|
|
|
SECURE = !!process.env.NODE_SSL
|
2013-05-01 12:37:26 -07:00
|
|
|
# client = redis.createClient 6379, REDIS_SERVER
|
2011-11-06 12:21:27 -08:00
|
|
|
|
2013-03-17 00:07:41 -07:00
|
|
|
# RedisStore = require 'socket.io/lib/stores/redis'
|
|
|
|
# rpub = redis.createClient 6379, REDIS_SERVER
|
|
|
|
# rsub = redis.createClient 6379, REDIS_SERVER
|
|
|
|
# rclient = redis.createClient 6379, REDIS_SERVER
|
2013-03-15 17:55:58 -07:00
|
|
|
|
|
|
|
|
2012-12-24 11:53:00 -08:00
|
|
|
if SECURE
|
|
|
|
privateKey = fs.readFileSync('./config/certificates/newsblur.com.key').toString()
|
|
|
|
certificate = fs.readFileSync('./config/certificates/newsblur.com.crt').toString()
|
2015-06-02 11:24:38 -07:00
|
|
|
# ca = fs.readFileSync('./config/certificates/intermediate.crt').toString()
|
2012-12-24 11:53:00 -08:00
|
|
|
io = require('socket.io').listen 8889
|
|
|
|
key: privateKey
|
|
|
|
cert: certificate
|
2015-06-02 11:24:38 -07:00
|
|
|
# ca: ca
|
2012-12-24 11:53:00 -08:00
|
|
|
else
|
|
|
|
io = require('socket.io').listen 8888
|
|
|
|
|
2012-04-10 09:54:43 -07:00
|
|
|
io.configure 'production', ->
|
|
|
|
io.set 'log level', 1
|
2012-05-03 13:43:18 -07:00
|
|
|
io.enable 'browser client minification'
|
|
|
|
io.enable 'browser client etag'
|
|
|
|
io.enable 'browser client gzip'
|
|
|
|
|
2012-04-10 09:54:43 -07:00
|
|
|
|
|
|
|
io.configure 'development', ->
|
|
|
|
io.set 'log level', 2
|
|
|
|
|
2013-03-17 00:07:41 -07:00
|
|
|
# io.set 'store', new RedisStore
|
|
|
|
# redisPub : rpub
|
|
|
|
# redisSub : rsub
|
|
|
|
# redisClient : rclient
|
2013-03-15 17:55:58 -07:00
|
|
|
|
2011-11-06 12:21:27 -08:00
|
|
|
io.sockets.on 'connection', (socket) ->
|
2013-09-06 16:25:32 -07:00
|
|
|
ip = socket.handshake.headers['X-Forwarded-For'] || socket.handshake.address.address
|
2013-05-01 13:22:03 -07:00
|
|
|
|
2012-04-03 22:01:12 -07:00
|
|
|
socket.on 'subscribe:feeds', (@feeds, @username) ->
|
2013-05-01 13:25:14 -07:00
|
|
|
log.info @username, "Connecting (#{feeds.length} feeds, #{ip})," +
|
2013-05-01 12:51:59 -07:00
|
|
|
" (#{io.sockets.clients().length} users on) " +
|
|
|
|
" #{if SECURE then "(SSL)" else "(non-SSL)"}"
|
2013-04-08 10:23:26 -07:00
|
|
|
|
|
|
|
if not @username
|
|
|
|
return
|
|
|
|
|
2013-08-12 11:52:29 -07:00
|
|
|
socket.on "error", (err) ->
|
|
|
|
console.log " ---> Error (socket): #{err}"
|
2012-03-28 16:13:17 -07:00
|
|
|
socket.subscribe?.end()
|
2012-03-28 19:13:30 -07:00
|
|
|
socket.subscribe = redis.createClient 6379, REDIS_SERVER
|
2013-08-12 11:03:42 -07:00
|
|
|
socket.subscribe.on "error", (err) ->
|
|
|
|
console.log " ---> Error: #{err}"
|
2013-08-12 11:22:10 -07:00
|
|
|
socket.subscribe.end()
|
2013-08-12 11:11:30 -07:00
|
|
|
socket.subscribe.on "connect", =>
|
|
|
|
socket.subscribe.subscribe @feeds
|
|
|
|
socket.subscribe.subscribe @username
|
2013-03-15 17:55:58 -07:00
|
|
|
|
2012-04-10 10:09:46 -07:00
|
|
|
socket.subscribe.on 'message', (channel, message) =>
|
2013-05-01 12:51:59 -07:00
|
|
|
log.info @username, "Update on #{channel}: #{message}"
|
2012-10-22 16:25:36 -07:00
|
|
|
if channel == @username
|
|
|
|
socket.emit 'user:update', channel, message
|
|
|
|
else
|
|
|
|
socket.emit 'feed:update', channel, message
|
2013-03-15 17:55:58 -07:00
|
|
|
|
2011-11-06 12:21:27 -08:00
|
|
|
socket.on 'disconnect', () ->
|
|
|
|
socket.subscribe?.end()
|
2013-05-01 12:51:59 -07:00
|
|
|
log.info @username, "Disconnect (#{@feeds?.length} feeds, #{ip})," +
|
2013-04-13 18:59:51 -07:00
|
|
|
" there are now #{io.sockets.clients().length-1} users. " +
|
2013-03-15 17:55:58 -07:00
|
|
|
" #{if SECURE then "(SSL)" else "(non-SSL)"}"
|
2013-08-12 11:52:29 -07:00
|
|
|
|
|
|
|
io.sockets.on 'error', (err) ->
|
|
|
|
console.log " ---> Error (sockets): #{err}"
|