如何动态更新socket.io回调而不重新启动服务器? [英] How do I dynamically update socket.io callbacks without restarting the server?

查看:906
本文介绍了如何动态更新socket.io回调而不重新启动服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个目录的文件,说是以下面的格式:

If I have a directory of files that say are in the following format:

module.exports =
  'add': (socket, data...) ->
    console.log 'words:add handler'.rainbow, data...
    socket.emit 'talkback', 'hahahha'

如何包含这些文件,并且更改后,更新所有连接的socket.io客户端以使用新的回调。

How do I include those files and when they are changed, update all connected socket.io clients to use the new callbacks.

如果文件名是 words.controller.coffee ,我想回调为 words:add

If the filename is words.controller.coffee, I'd like the callback to be words:add.

因此,每次新的套接字连接时,如何使已经加载的每个文件绑定到套接字。当文件更改时,它应该停止侦听该文件,然后开始使用新绑定进行侦听。

So every time a new socket connects, how do I make each file that's already been loaded bind to the socket. And when a file changes, it should stop listening on that file, then start listening with the new binds.

推荐答案

利用一个我写的 loaddir 。它遍历一个目录并监视它,并回调一些默认值,如 basePath 。在这种情况下,对于 words.controller.coffee ,基本路径为 words.controller

So this answer utilizes a library I wrote called loaddir. It walks through a directory and watches it and calls back with some defaults like basePath. In this case, for words.controller.coffee, the base path is words.controller

cs = require('coffee-script')
loaddir = require('loaddir')
handlers = {}
sockets = []

io.sockets.on 'connection', (socket) =>

  # keep track of currently connected sockets
  sockets.push socket
  socket.on 'disconnect', =>
    if -1 isnt (index = sockets.indexOf socket)
      sockets.splice index, 1
    console.log 'disconnected'

  # bind the currently loaded files ( we pass the socket along as well )
  _.each handlers, (event, event_name) =>
    socket.on event_name, -> event socket, arguments...

# Watches the directory for changes
loaddir

  path: ROOT + 'sockets' # this is our directory

  callback: ->

    # @basePath is provided by loaddir, in this case 'words.controller'
    module_name = @baseName.split('.')[0]

    # unbind the sockets that are currently connected
    _.each handlers, (event, nested_event_name) =>

      # only unbind for this file
      # nested_event_name == 'words:add'
      if nested_event_name.split(':')[0] == module_name
        _.each sockets, (socket) =>
          socket.removeListener nested_event_name, event
        delete handlers[nested_event_name]

    # Eval because we don't want to restart the server each time
    # @fileContents is provided by loaddir, we use coffeescript to compile it
    module_handlers = eval 'var module = {}; ' + (cs.compile @fileContents) + ' module.exports'

    # bind the currently connected sockets with this files events
    _.each module_handlers, (event, event_name) =>
      # event_name == 'add'
      handlers[module_name + ':' + event_name] = event
      _.each sockets, (socket) =>
        socket.on module_name + ':' + event_name, -> event socket, arguments...

这篇关于如何动态更新socket.io回调而不重新启动服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆