UNPKG

1.5 kBtext/coffeescriptView Raw
1require 'colors'
2_proxy = require('json-proxy')
3_ = require 'lodash'
4_router = require './router'
5_utils = require './utils'
6_initialize = require './initialize'
7
8#作为一个中间件提供服务
9module.exports = (app, server, startServer)->
10 #集成代理
11 cfgProxy = _utils.config.proxy || {}
12 cfgProxy.headers = _.extend cfgProxy.headers || {}, headers: 'X-Forwarded-User': 'Silky'
13 app.use _proxy.initialize(proxy: cfgProxy)
14
15 #监听路由
16 _router(app)
17
18 #启动服务,如果是第三方调用,则可能不需要启动服务器
19 return if not startServer
20 app.set 'port', _utils.options.port || _utils.config.port || 14422
21 server.on 'error', (err) ->
22 if err.code is 'EADDRINUSE'
23 console.log "端口冲突,请使用其它端口".red
24 return process.exit(1)
25
26 console.log "Silky发生严重错误".red
27 console.log err.message.red
28
29 server.listen app.get('port')
30 console.log "Port -> #{app.get('port')}"
31 console.log "Workbench -> #{_utils.options.workbench}"
32 console.log "Environment -> #{_utils.options.env}"
33 console.log "Please visit http://localhost:#{app.get('port')}"
34
35 ###
36 #监听socket的事件
37 io = require('socket.io').listen(server, log: false)
38 io.sockets.on 'connection', (socket)->
39 event = 'page:change'
40 listener = ()-> socket.emit event, null
41 #收到页面变更的事件后,通知客户端
42 _utils.addListener event, listener
43
44 socket.on 'disconnect', (socket)-> _utils.removeListener event, listener
45 ###
\No newline at end of file