centOS下通过NPM安装和配置node.js+socket.io+connect实现在线聊天的详细教程

2011-09-29 00:00:00 by 【6yang】, 2928 visits, 收藏 | 返回
  1. 0.新建目录node
  2. cd /opt
  3. mkdir node
  4.  
  5. 1.安装nodejs
  6. yum install gcc-c++ openssl-devel
  7. tar -xzvf joyent-node-v0.4.7-94-ge505a12.tar.gz
  8. cd joyent-node-e505a12
  9. ./configure
  10. make
  11. make install
  12.  
  13. 检查安装,HELLOWORD[可选]:
  14. var http = require('http');
  15. http.createServer(function (req, res) {
  16.   res.writeHead(200, {'Content-Type': 'text/plain'});
  17.   res.end('Hello Node.jsn');
  18. }).listen(8124, "192.168.1.254");
  19. console.log('Server running at http://192.168.1.254:8124/');
  20.  
  21. 2.安装NPM
  22.  
  23. //-----------------------------安装其他模块
  24.  (node.js配置脚本目录,安装下面任意模块会自动保存到当前目录生成的"node_modules"文件夹下)
  25.  
  26. 3.安装socket.io
  27. 安装最新版本:npm install socket.io
  28. 安装v0.6.x版本:npm install socket.io@0.6.18 
  29.  
  30.  
  31. 检查安装[可选]:
  32. // 加载HTTP模块(来启动服务器)和Socket.io模块
  33. var http = require('http'), io = require('socket.io');
  34.  
  35. // 在8080端口启动服务器
  36. var server = http.createServer(function(req, res){ 
  37.  
  38.         // 发送HTML的headers和message
  39.         res.writeHead(200,{ 'Content-Type': 'text/html' }); 
  40.         res.end('<h1>Hello Socket Lover!</h1>');
  41. });
  42. server.listen(8080);
  43.  
  44. // 创建一个Socket.IO实例,把它传递给服务器
  45. var socket = io.listen(server);
  46.  
  47. // 添加一个连接@@@@@@
  48. socket.on('connection', function(client){ 
  49.         
  50.         // 成功!现在开始监听接收到的消息
  51.         client.on('message',function(event){ 
  52.                 console.log('Received message from client!',event);
  53.         });
  54.         client.on('disconnect',function(){
  55.                 clearInterval(interval);
  56.                 console.log('Server has disconnected');
  57.         });
  58.  
  59. });
  60. 4.安装connect
  61. npm install connect
  62.  
  63. 5.后台运行
  64. nohup node server.js &
  65. 或者nohup node server.js > /dev/null &
  66.  
  67. 6.出现地址占用解决办法
  68. ps aux|grep node
  69. kill -9 PID
  70.  
  71. 7.[测试环境中用]使用spludo来运行server.js,当server.js有更改时自动重启server.js进程
  72. 复制run_dev_server.js与server.js同级目录
  73. 运行node run_dev_server.js即可
  74.  
  75. 8.使用forever来守护node进程
  76. (1)
  77. npm install forever
  78. npm install forever -g
  79. (2)在server.js同一目录下新建forever.js,配置如下:
  80. var forever = require('forever');
  81.  
  82. var child = new (forever.Forever)('server.js',{
  83.     //'max' : 100000000,        // Sets the maximum number of times a given script should run
  84.     'forever' : true,             // Indicates that this script should run forever 
  85.     'silent' : true,             // Silences the output from stdout and stderr in the parent process
  86.     'logFile' : 'log.log',   // Path to log output from forever process (when in daemon)
  87.     'pidFile' : 'pid.log',   // Path to put pid information for the process(es) started
  88.     'outFile' : 'out.log',   // Path to log output from child stdout
  89.     'errFile' : 'err.log',   // Path to log output from child stderr
  90.     'command' : 'node',           // Binary to run (default: 'node')
  91.     'options' : []   // Additional arguments to pass to the script
  92. });
  93.  
  94. //进程退出
  95. child.on('exit', function(){
  96. console.log('child has exited');
  97. });
  98. child.start();
  99. (3)运行forever start forever.js
  100. (4)停止运行forever stopall
分享到:
share

    图片原图

    loading

    loading