diff --git a/build.php b/build.php index 203f246..6289256 100755 --- a/build.php +++ b/build.php @@ -25,6 +25,7 @@ if (file_exists('file.tar')) unlink('file.tar'); if (file_exists('template.tar')) unlink('template.tar'); if (file_exists('acptemplate.tar')) unlink('acptemplate.tar'); + foreach (glob('file/acp/be.bastelstu.wcf.chat.serverPush/lib/*.js') as $nodeFile) unlink($nodeFile); foreach (glob('file/js/*.js') as $jsFile) unlink($jsFile); foreach (glob('file/style/*.css') as $cssFile) unlink($cssFile); if (file_exists('be.bastelstu.wcf.chat.tar')) unlink('be.bastelstu.wcf.chat.tar'); @@ -39,6 +40,11 @@ passthru('coffee -cb '.escapeshellarg($coffeeFile), $code); if ($code != 0) exit($code); } +foreach (glob('file/acp/be.bastelstu.wcf.chat.serverPush/lib/*.coffee') as $coffeeFile) { + echo $coffeeFile."\n"; + passthru('coffee -cb '.escapeshellarg($coffeeFile), $code); + if ($code != 0) exit($code); +} echo << +# @package be.bastelstu.wcf.chat +### +process.title = 'Tims Chat Pushserver' + +io = require 'socket.io' +net = require 'net' + +config = require('../config.js').config + +log = (message) -> + console.log '[be.bastelstu.wcf.chat.serverPush] '+message + +class Server + constructor: () -> + log 'Starting Pushserver for Tims Chat' + log 'PID is ' + process.pid + log 'Using port: ' + config.port + + @initUnixSocket() + @initSocketIO() + initSocketIO: () -> + @socket = io.listen config.port + + @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(@) + initUnixSocket: () -> + log 'Initializing Unix-Socket' + socket = net.createServer ((c) -> + @socket.sockets.emit 'newMessage' + c.end() + ).bind(@) + + socket.listen process.cwd() + '/../data.sock' + +new Server() \ No newline at end of file diff --git a/file/acp/be.bastelstu.wcf.chat.serverPush/package.json b/file/acp/be.bastelstu.wcf.chat.serverPush/package.json new file mode 100644 index 0000000..6140264 --- /dev/null +++ b/file/acp/be.bastelstu.wcf.chat.serverPush/package.json @@ -0,0 +1,19 @@ +{ + "name" : "be.bastelstu.wcf.chat.serverPush", + "description" : "Server-Pushing for Tims Chat", + "homepage" : "https://github.com/wbbaddons/Tims-Chat", + "keywords" : ["chat"], + "author" : "Tim Düsterhus ", + "contributors" : [ + ], + "dependencies" : { + "socket.io" : "0.8.7" + }, + "devDependencies": { + "jshint" : "*" + }, + "engines" : { "node" : ">=0.6.0", + "npm" : ">=1.0" + }, + "version" : "1.0.0" +} diff --git a/file/js/be.bastelstu.WCF.Chat.coffee b/file/js/be.bastelstu.WCF.Chat.coffee index d941791..9fbac47 100644 --- a/file/js/be.bastelstu.WCF.Chat.coffee +++ b/file/js/be.bastelstu.WCF.Chat.coffee @@ -7,16 +7,24 @@ # @package be.bastelstu.wcf.chat ### -window.consoleMock = console -window.consoleMock ?= +window.console ?= log: () ->, warn: () ->, error: () -> -(($, window, console) -> +(($, window, windowConsole) -> window.be ?= {} be.bastelstu ?= {} be.bastelstu.WCF ?= {} + console = + log: (message) -> + windowConsole.log '[be.bastelstu.WCF.Chat] '+message + warn: (message) -> + windowConsole.warn '[be.bastelstu.WCF.Chat] '+message + error: (message) -> + windowConsole.error '[be.bastelstu.WCF.Chat] '+message + + be.bastelstu.WCF.Chat = # Tims Chat stops loading when this reaches zero # TODO: We need an explosion animation @@ -44,12 +52,16 @@ window.consoleMock ?= events: newMessage: $.Callbacks() userMenu: $.Callbacks() + + # socket.io + socket: null + pe: getMessages: null refreshRoomList: null fish: null init: () -> - console.log '[be.bastelstu.WCF.Chat] Initializing' + console.log 'Initializing' @bindEvents() @events.newMessage.add $.proxy @notify, @ @@ -57,8 +69,9 @@ window.consoleMock ?= @pe.getMessages = new WCF.PeriodicalExecuter $.proxy(@getMessages, @), @config.reloadTime * 1e3 @refreshRoomList() @getMessages() + @initPush() - console.log '[be.bastelstu.WCF.Chat] Finished initializing - Shields at 104 percent' + console.log 'Finished initializing - Shields at 104 percent' ### # Autocompletes a username ### @@ -124,7 +137,7 @@ window.consoleMock ?= firstChars = @autocompleteValue.substring(@autocompleteValue.lastIndexOf(' ')+1) - console.log '[be.bastelstu.WCF.Chat] Autocompleting "' + firstChars + '"' + console.log 'Autocompleting "' + firstChars + '"' return if firstChars.length is 0 # Insert name and increment offset @@ -221,7 +234,7 @@ window.consoleMock ?= ### freeTheFish: () -> return if $.wcfIsset 'fish' - console.warn '[be.bastelstu.WCF.Chat] Freeing the fish' + console.warn 'Freeing the fish' fish = $ '
' + WCF.String.escapeHTML('><((((\u00B0>') + '
' fish.css position: 'absolute' @@ -261,12 +274,12 @@ window.consoleMock ?= @handleUsers(data.users) , @) error: $.proxy((jqXHR, textStatus, errorThrown) -> - console.error '[be.bastelstu.WCF.Chat] Battle Station hit - shields at ' + (--@shields / 3 * 104) + ' percent' + console.error 'Battle Station hit - shields at ' + (--@shields / 3 * 104) + ' percent' if @shields is 0 @pe.refreshRoomList.stop() @pe.getMessages.stop() @freeTheFish() - console.error '[be.bastelstu.WCF.Chat] We got destroyed, but could free our friend the fish before he was killed as well. Have a nice life in freedom!' + console.error 'We got destroyed, but could free our friend the fish before he was killed as well. Have a nice life in freedom!' alert 'herp i cannot load messages' , @) beforeSend: $.proxy(() -> @@ -318,7 +331,7 @@ window.consoleMock ?= # Move the user to the correct position if element[0] - console.log '[be.bastelstu.WCF.Chat] Moving User: "' + user.username + '"' + console.log 'Moving User: "' + user.username + '"' element = element.detach() if user.awayStatus? element.addClass 'timsChatAway' @@ -330,7 +343,7 @@ window.consoleMock ?= $('#timsChatUserList').append element # Insert the user else - console.log '[be.bastelstu.WCF.Chat] Inserting User: "' + user.username + '"' + console.log 'Inserting User: "' + user.username + '"' li = $ '
  • ' li.attr 'id', id li.addClass 'timsChatUser' @@ -360,12 +373,30 @@ window.consoleMock ?= # Remove users that were not found $('.timsChatUser').each () -> if typeof foundUsers[$(@).attr('id')] is 'undefined' - console.log '[be.bastelstu.WCF.Chat] Removing User: "' + $(@).data('username') + '"' + console.log 'Removing User: "' + $(@).data('username') + '"' $(@).remove(); $('#toggleUsers .badge').text(users.length); ### + # Initializes Server-Push + ### + initPush: () -> + if typeof window.io isnt 'undefined' + console.log 'Initializing socket.io' + @socket = io.connect @config.socketIOPath + @socket.on 'connect', $.proxy((data) -> + console.log 'Connected on socket.io' + @pe.getMessages.stop() + , @) + @socket.on 'disconnect', $.proxy((data) -> + console.log 'Losing connection to socket.io' + @pe.getMessages = new WCF.PeriodicalExecuter $.proxy(@getMessages, @), @config.reloadTime * 1e3 + , @) + @socket.on 'newMessage', $.proxy((data) -> + @getMessages() + , @) + ### # Inserts text into our input. # # @param string text @@ -412,7 +443,7 @@ window.consoleMock ?= # Refreshes the room-list. ### refreshRoomList: () -> - console.log '[be.bastelstu.WCF.Chat] Refreshing the roomlist' + console.log 'Refreshing the roomlist' $('#toggleRooms a').addClass 'ajaxLoad' $.ajax $('#toggleRooms a').data('refreshUrl'), @@ -435,7 +466,7 @@ window.consoleMock ?= @changeRoom $ event.target , @ - console.log '[be.bastelstu.WCF.Chat] Found ' + data.length + ' rooms' + console.log 'Found ' + data.length + ' rooms' , @) ### # Handles submitting of messages. @@ -504,4 +535,4 @@ window.consoleMock ?= $.ajax @config.unloadURL, type: 'POST' async: false -)(jQuery, @, consoleMock) +)(jQuery, @, console) diff --git a/file/lib/data/chat/message/ChatMessageEditor.class.php b/file/lib/data/chat/message/ChatMessageEditor.class.php index 53c10fd..b105ce2 100644 --- a/file/lib/data/chat/message/ChatMessageEditor.class.php +++ b/file/lib/data/chat/message/ChatMessageEditor.class.php @@ -16,6 +16,22 @@ class ChatMessageEditor extends \wcf\data\DatabaseObjectEditor { */ protected static $baseClass = '\wcf\data\chat\message\ChatMessage'; + /** + * Notify the Push-Server. + */ + public static function create(array $parameters = array()) { + try { + if (CHAT_SOCKET_IO_PATH) { + if (file_exists(WCF_DIR.'acp/be.bastelstu.wcf.chat.serverPush/data.sock')) { + $sock = stream_socket_client('unix://data.sock', $errno, $errstr, 1); + fclose($sock); + } + } + } + catch (\Exception $e) { } + + return parent::create($parameters); + } /** * Removes old messages. diff --git a/option.xml b/option.xml index b491edd..75d9a78 100644 --- a/option.xml +++ b/option.xml @@ -61,6 +61,12 @@ 1 9 + diff --git a/package.xml b/package.xml index 3286ce7..61073c7 100644 --- a/package.xml +++ b/package.xml @@ -5,7 +5,7 @@ 0 1 - 3.0.0 Alpha 12 + 3.0.0 Alpha 13 2011-11-26 com.woltlab.wcf.message diff --git a/template/chat.tpl b/template/chat.tpl index 388fd5a..975da17 100755 --- a/template/chat.tpl +++ b/template/chat.tpl @@ -126,7 +126,8 @@ // populate config be.bastelstu.WCF.Chat.config = { reloadTime: {@CHAT_RELOADTIME}, - unloadURL: '{link controller='Chat' action='Leave'}{/link}' + unloadURL: '{link controller='Chat' action='Leave'}{/link}', + socketIOPath: '{@CHAT_SOCKET_IO_PATH|encodeJS}' } WCF.Language.addObject({ 'wcf.chat.query': '{lang}wcf.chat.query{/lang}', @@ -155,7 +156,7 @@ $('#timsChatCopyright').click(function (event) { event.preventDefault(); if ($.wcfIsset('timsChatCopyrightDialog')) return WCF.showDialog('timsChatCopyrightDialog', { title: 'Tims Chat{if CHAT_SHOW_VERSION} {$chatVersion}{/if}' }); - var container = $('
    '); + var container = $('
    '); container.load('{link controller='Chat' action='Copyright'}{/link}', function() { $('body').append(container); WCF.showDialog('timsChatCopyrightDialog', { title: 'Tims Chat{if CHAT_SHOW_VERSION} {$chatVersion}{/if}' }); diff --git a/template/chatJavascriptInclude.tpl b/template/chatJavascriptInclude.tpl index 61b033b..fc1515c 100644 --- a/template/chatJavascriptInclude.tpl +++ b/template/chatJavascriptInclude.tpl @@ -1,2 +1,3 @@ - \ No newline at end of file + +{if CHAT_SOCKET_IO_PATH}{/if}