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
|
|
|
|
2019-04-13 15:28:56 -04:00
|
|
|
DEV = process.env.NODE_ENV == 'development'
|
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
|
|
|
|
2019-04-13 15:28:56 -04:00
|
|
|
|
|
|
|
log.debug "Starting NewsBlur unread count server..."
|
|
|
|
if !DEV and !process.env.NODE_ENV
|
|
|
|
log.debug "Specify NODE_ENV=<development,production>"
|
|
|
|
return
|
|
|
|
else if DEV
|
|
|
|
log.debug "Running as development server"
|
|
|
|
else
|
|
|
|
log.debug "Running as production server"
|
|
|
|
|
|
|
|
|
2012-12-24 11:53:00 -08:00
|
|
|
if SECURE
|
2016-11-29 18:47:44 -08:00
|
|
|
privateKey = fs.readFileSync('/srv/newsblur/config/certificates/newsblur.com.key').toString()
|
|
|
|
certificate = fs.readFileSync('/srv/newsblur/config/certificates/newsblur.com.crt').toString()
|
2015-06-02 11:24:38 -07:00
|
|
|
# ca = fs.readFileSync('./config/certificates/intermediate.crt').toString()
|
2016-02-05 15:26:10 -08:00
|
|
|
options =
|
2016-11-29 18:30:12 -08:00
|
|
|
port: 8889
|
2012-12-24 11:53:00 -08:00
|
|
|
key: privateKey
|
|
|
|
cert: certificate
|
2016-11-29 18:47:44 -08:00
|
|
|
app = require('https').createServer options
|
2016-11-30 15:26:42 -08:00
|
|
|
io = require('socket.io')(app, path: "/v2/socket.io")
|
2016-11-29 18:47:44 -08:00
|
|
|
app.listen options.port
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug "Listening securely on port #{options.port}"
|
2012-12-24 11:53:00 -08:00
|
|
|
else
|
2016-11-29 18:47:44 -08:00
|
|
|
options =
|
|
|
|
port: 8888
|
|
|
|
app = require('http').createServer()
|
2016-11-30 15:26:42 -08:00
|
|
|
io = require('socket.io')(app, path: "/v2/socket.io")
|
2016-11-29 18:47:44 -08:00
|
|
|
app.listen options.port
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug "Listening on port #{options.port}"
|
2016-11-30 15:26:42 -08:00
|
|
|
|
|
|
|
# io.set('transports', ['websocket'])
|
2012-04-10 09:54:43 -07:00
|
|
|
|
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
|
|
|
|
2016-11-29 18:30:12 -08:00
|
|
|
io.on 'connection', (socket) ->
|
|
|
|
ip = socket.handshake.headers['X-Forwarded-For'] || socket.handshake.address
|
|
|
|
|
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})," +
|
2016-11-29 18:30:12 -08:00
|
|
|
" (#{io.engine.clientsCount} connected) " +
|
2013-05-01 12:51:59 -07:00
|
|
|
" #{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) ->
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug "Error (socket): #{err}"
|
2016-11-30 12:47:20 -08:00
|
|
|
socket.subscribe?.quit()
|
2012-03-28 19:13:30 -07:00
|
|
|
socket.subscribe = redis.createClient 6379, REDIS_SERVER
|
2016-12-05 14:18:48 -08:00
|
|
|
socket.subscribe.on "error", (err) =>
|
2016-12-05 14:17:10 -08:00
|
|
|
log.info @username, "Error: #{err} (#{@feeds.length} feeds)"
|
2016-12-05 13:35:05 -08:00
|
|
|
socket.subscribe?.quit()
|
2013-08-12 11:11:30 -07:00
|
|
|
socket.subscribe.on "connect", =>
|
2016-12-05 13:35:05 -08:00
|
|
|
log.info @username, "Connected (#{@feeds.length} feeds, #{ip})," +
|
|
|
|
" (#{io.engine.clientsCount} connected) " +
|
|
|
|
" #{if SECURE then "(SSL)" else "(non-SSL)"}"
|
2013-08-12 11:11:30 -07:00
|
|
|
socket.subscribe.subscribe @feeds
|
2016-12-13 16:29:42 -08:00
|
|
|
feeds_story = @feeds.map (f) -> "#{f}:story"
|
|
|
|
socket.subscribe.subscribe feeds_story
|
2013-08-12 11:11:30 -07:00
|
|
|
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
|
2016-12-13 16:29:42 -08:00
|
|
|
else if channel.indexOf(':story') >= 0
|
|
|
|
socket.emit 'feed:story:new', channel, message
|
2012-10-22 16:25:36 -07:00
|
|
|
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', () ->
|
2016-11-30 12:47:20 -08:00
|
|
|
socket.subscribe?.quit()
|
2013-05-01 12:51:59 -07:00
|
|
|
log.info @username, "Disconnect (#{@feeds?.length} feeds, #{ip})," +
|
2016-11-29 18:30:12 -08:00
|
|
|
" there are now #{io.engine.clientsCount} 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) ->
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug "Error (sockets): #{err}"
|