1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-23 21:50:09 +00:00
Tims-Chat/file/acp/be.bastelstu.chat.nodePush/lib/server.coffee

69 lines
1.7 KiB
CoffeeScript
Raw Normal View History

2012-04-27 13:42:44 +00:00
###
# node.js Pushserver for Tims Chat.
#
2013-04-05 19:23:08 +00:00
# @author Tim Düsterhus
# @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
2013-02-02 15:57:52 +00:00
config = require '../config.js'
2012-04-27 13:42:44 +00:00
log = (message) ->
2013-02-02 15:57:52 +00:00
console.log "[be.bastelstu.chat.nodePush] #{message}"
2012-04-27 13:42:44 +00:00
class Server
constructor: () ->
log 'Starting Pushserver for Tims Chat'
2013-02-02 15:57:52 +00:00
log "PID is #{process.pid}"
log "Using port: #{config.port}"
2012-04-27 13:42:44 +00:00
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 @
2013-04-05 19:06:13 +00:00
setInterval =>
@socket.sockets.emit 'newMessage'
2013-04-05 19:06:13 +00:00
, 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
2013-02-02 15:57:52 +00:00
@socket.set 'browser client gzip', true
2012-04-27 14:20:41 +00:00
2013-04-05 19:06:13 +00:00
@socket.configure 'development', =>
2012-04-27 14:20:41 +00:00
@socket.set 'log level', 3
@socket.set 'browser client etag', false
@socket.set 'browser client minification', false
2012-04-27 17:33:28 +00:00
initUnixSocket: () ->
log 'Initializing Unix-Socket'
2013-04-05 19:06:13 +00:00
socket = net.createServer (c) =>
setTimeout =>
2012-04-27 19:50:41 +00:00
@socket.sockets.emit 'newMessage'
2013-04-05 19:06:13 +00:00
, 20
2012-04-27 17:33:28 +00:00
c.end()
2013-04-05 19:23:08 +00:00
socket.listen "#{__dirname}/../data.sock"
fs.chmod "#{__dirname}/../data.sock", '777'
2012-10-14 15:58:13 +00:00
shutdown: () ->
2013-04-05 19:23:08 +00:00
return unless fs.existsSync "#{__dirname}/../data.sock"
2012-10-14 15:58:13 +00:00
log 'Shutting down'
2013-04-05 19:23:08 +00:00
fs.unlinkSync "#{__dirname}/../data.sock"
2012-10-14 15:58:13 +00:00
process.exit()
2012-04-27 13:42:44 +00:00
new Server()