1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-01 14:20:07 +00:00
Tims-Chat/file/acp/be.bastelstu.chat.nodePush/lib/server.coffee

73 lines
1.9 KiB
CoffeeScript
Raw Normal View History

2012-04-27 13:42:44 +00:00
###
# node.js Pushserver for Tims Chat.
#
# @author Tim Düsterhus
2013-01-19 19:36:40 +00:00
# @copyright 2010-2013 Tim Düsterhus
2012-04-27 13:42:44 +00:00
# @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
2013-01-19 19:36:40 +00:00
# @package be.bastelstu.chat
2012-04-30 18:51:27 +00:00
# @subpackage nodePush
2012-04-27 13:42:44 +00:00
###
2012-04-30 18:51:27 +00:00
process.title = 'nodePush - Tims Chat'
2012-04-27 13:42:44 +00:00
io = require 'socket.io'
2012-04-27 17:33:28 +00:00
net = require 'net'
2012-04-27 20:13:27 +00:00
fs = require 'fs'
2012-04-27 13:42:44 +00:00
2012-10-16 10:09:01 +00:00
config = require('../config.js')
2012-04-27 13:42:44 +00:00
log = (message) ->
2013-01-19 19:36:40 +00:00
console.log '[be.bastelstu.chat.nodePush] '+message
2012-04-27 13:42:44 +00:00
class Server
constructor: () ->
2012-05-04 13:35:06 +00:00
if process.cwd().substring(process.cwd().length - 3) isnt 'lib'
console.error 'Please run me via bin/run.sh'
process.exit 1
2012-04-27 13:42:44 +00:00
log 'Starting Pushserver for Tims Chat'
2012-04-27 17:33:28 +00:00
log 'PID is ' + process.pid
2012-04-27 13:42:44 +00:00
log 'Using port: ' + config.port
2012-04-27 17:33:28 +00:00
@initUnixSocket()
@initSocketIO()
2012-10-14 15:58:13 +00:00
process.on 'exit', @shutdown.bind @
process.on 'uncaughtException', @shutdown.bind @
process.on 'SIGINT', @shutdown.bind @
process.on 'SIGTERM', @shutdown.bind @
setInterval (() ->
@socket.sockets.emit 'newMessage'
).bind(@), 60e3
2012-04-27 17:33:28 +00:00
initSocketIO: () ->
2012-04-30 18:51:27 +00:00
log 'Initializing socket.io'
2012-04-27 13:42:44 +00:00
@socket = io.listen config.port
2012-04-27 14:20:41 +00:00
@socket.set 'log level', 1
@socket.set 'browser client etag', true
@socket.set 'browser client minification', true
@socket.set 'browser client gzig', true
@socket.configure 'development', (() ->
@socket.set 'log level', 3
@socket.set 'browser client etag', false
@socket.set 'browser client minification', false
).bind(@)
2012-04-27 17:33:28 +00:00
initUnixSocket: () ->
log 'Initializing Unix-Socket'
socket = net.createServer ((c) ->
2012-04-27 19:50:41 +00:00
setTimeout (() ->
@socket.sockets.emit 'newMessage'
).bind(@), 20
2012-04-27 17:33:28 +00:00
c.end()
).bind(@)
socket.listen process.cwd() + '/../data.sock'
2012-04-27 20:13:27 +00:00
fs.chmod process.cwd() + '/../data.sock', '777'
2012-10-14 15:58:13 +00:00
shutdown: () ->
return unless fs.existsSync process.cwd() + '/../data.sock'
log 'Shutting down'
fs.unlinkSync process.cwd() + '/../data.sock'
process.exit()
2012-04-27 13:42:44 +00:00
new Server()