1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2025-01-23 02:10:41 +00:00

69 lines
1.7 KiB
CoffeeScript
Raw Normal View History

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