Socket.io 启动输出杀死了 Node.js 守护进程的 Upstart 脚本

发布于 2024-12-02 15:35:14 字数 1756 浏览 1 评论 0原文

我有一个 Node 脚本,它使用 Socket.io 向连接的客户端广播消息。我想使用 Upstart 脚本将其作为后台进程运行。我可以使用 sudo start my_server 启动守护进程,该进程将启动并运行。然而,服务器会被 Socket.io 的标准输出“info - (Socket.io started)”立即停止。

这是在读取 Socket.io 配置之前执行的。 Socket.io 文档 显示我可以将“log”选项传递给监听方法以保留sys.log 将输出发送到 stdout。但我似乎无法阻止最初的“信息 - (Socket.io 启动)”的输出。我什至尝试注释掉模块中对记录器的调用,但它已被缓存。

是否有人成功创建了 node/socket.io 守护进程或抑制了 socket.io 的默认输出?下面是我的 Upstart 脚本和 Node.js 的代码。

使用 socket.io 的新贵脚本

description "node.js server"
author      "[email protected]"

start on started mountall
stop on shutdown

respawn
respawn limit 99 5

script
   export HOME="/username"

   exec sudo -u username sh -c "/usr/local/bin/node path/to/server.js >> /var/log/node.log 2?&1"
end script

server.js

var http = require('http'),
url = require('url'),
fs = require('fs'),

//create server and attach socket io
server = http.createServer(function(req, res){
        res.writeHead(200,{ 'Content-Type': 'text/html' }); 
        res.end('<h1>Hello Socket Lover!</h1>');
      }),
io = require('socket.io').listen(server, function(message){});

//configure socket.io logging so it is minimal
io.configure(function(){
io.set('log level', 1);
io.set('transports', [
    'websocket',
    'flashsocket',
    'htmlfile',
    'xhr-polling',
    'jsonp-polling'
    ]);
});

//instruct our server to listen to port 8080    
server.listen(8080);

io.sockets.on('connection', function (socket) {
   socket.emit('message', { eventType: "connected" });
});

我很感激任何答案或解决方案。

I have a Node script that uses Socket.io to broadcast messages to connected clients. I want to run it as a background process with an Upstart script. I can start the daemon with sudo start my_server and the process will start and run. However, the server is stopped immediately by Socket.io's standard output 'info - (Socket.io started)'.

This is executed, before the Socket.io configuration is read. Socket.io documentation shows that I can pass a 'log' option to the listen method to keep sys.log from sending output to stdout. But I can't seem to keep that initial 'info - (Socket.io started)' from being output. I even tried to comment out that call to logger in the module but it is cached.

Has anyone successfully created a node/socket.io daemon or suppressed socket.io's default output? Below is the code for my Upstart script and my node.js.

upstart script

description "node.js server"
author      "[email protected]"

start on started mountall
stop on shutdown

respawn
respawn limit 99 5

script
   export HOME="/username"

   exec sudo -u username sh -c "/usr/local/bin/node path/to/server.js >> /var/log/node.log 2?&1"
end script

server.js using socket.io

var http = require('http'),
url = require('url'),
fs = require('fs'),

//create server and attach socket io
server = http.createServer(function(req, res){
        res.writeHead(200,{ 'Content-Type': 'text/html' }); 
        res.end('<h1>Hello Socket Lover!</h1>');
      }),
io = require('socket.io').listen(server, function(message){});

//configure socket.io logging so it is minimal
io.configure(function(){
io.set('log level', 1);
io.set('transports', [
    'websocket',
    'flashsocket',
    'htmlfile',
    'xhr-polling',
    'jsonp-polling'
    ]);
});

//instruct our server to listen to port 8080    
server.listen(8080);

io.sockets.on('connection', function (socket) {
   socket.emit('message', { eventType: "connected" });
});

I appreciate any answers or solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虫児飞 2024-12-09 15:35:14

该语句位于 /usr/local/lib/node_modules/socket.io/lib/manager.js 的 socket.io 0.7.9 第 117 行(如果您不使用全局 npm 安装,那么它将位于你已经安装了 npm 模块)。打开文件,转到第 117 行并将其注释掉。然后清理缓存(npm cache clean $HOME/.npm/socket.io)并重新启动,它应该消失了。

That statement is in /usr/local/lib/node_modules/socket.io/lib/manager.js on line 117 for socket.io 0.7.9 (if you aren't using a global npm install then it'll be in wherever you've installed your npm modules). Open the file, go to line 117 and comment it out. Then clean the cache (npm cache clean $HOME/.npm/socket.io) and restart and it should be gone.

生寂 2024-12-09 15:35:14

我收到了来自 Socket.io 贡献者 tedah 的消息。他对这个问题的回答是通过监听器方法将选项传递给manager.js...

options.logger = {
   info: self.emit.bind(self, 'info'),
   error: self.emit.bind(self, 'error'),
   warn: self.emit.bind(self, 'error'),
   debug: new Function()
 };

var io = socketIo.listen(port, options);

其中options.logger.info等绑定到容器的(自身)emit方法和选项。 logger.debug 被忽略。

I received a message from tedah, a Socket.io contributor. His answer this problem was to pass options to the manager.js through the listener method as such…

options.logger = {
   info: self.emit.bind(self, 'info'),
   error: self.emit.bind(self, 'error'),
   warn: self.emit.bind(self, 'error'),
   debug: new Function()
 };

var io = socketIo.listen(port, options);

Where options.logger.info etc are bound to the container's (self) emit methods and options.logger.debug is ignored.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文