游戏后台搭建(基于cocoscreator+nodejs+linux-阿里云)

后台搭建

环境
  • 阿里云linux云服务器(需要为新端口 添加安全组 许可访问)
  • node.js
  • cocoscreator
需求
  • 全服排行榜存储
  • 用户账号
  • 运营数据(评估游戏品质)
    • 七日留存统计
    • 日活统计
    • 激励广告展示统计,预计广告收入

一 使用nodejs构建http服务

  • 安装nodejs
    https://www.runoob.com/nodejs/nodejs-install-setup.html
    ps:设置软连接时,ls第一个参数为nodejs包下node的路径,设置后可通过进入/usr/local/bin/ 目录使用ls查看,是否有效(蓝色字体显示)

  • 新建httpcreate.js文件,用于运行创建http服务
    代码

      // 创建一个简单的http服务器程序
      // 1、加载http模块
      var http = require('http');
       
      // 2、创建一个http服务对象
      var server = http.createServer();
       
      console.log('create http server');
      // 3、监听用户的请求事件(request事件)
      // 回调函数中有两个参数
      // request 对象 包含用户请求报文中所有内容,通过request对象可以获取所有用户提交过来的数据
      // response 对象 用来向用户响应一些数据,当服务器要向客户端响应数据的时候必须使用response对象
      server.on('request', function (req, res) {
        var response = {
            'info' : 'Hello world'
        };
        if (req.url === '/' || req.url === '/rank') {
          // 读取index.html文件
          response.info = "rank";
        } else if (req.url === '/report') {
          // 读取login.html文件
          response.info = "report";
        }
        
        res.write(JSON.stringify(response));
        // 对于每一个请求,服务器必须结束响应,否则,客户端(浏览器)会一直等待服务器响应结束
        res.end();
        console.log('answer');
      });
    
      // 4、启动服务
      //第二个参数 设置监听方式可设置为tcp4
      //server.listen(8181, "0.0.0.0",function () {
      //  console.log('http listen 8181 statr');
      //});
    
      server.listen(8181,function () {
        console.log('http listen 8181 statr');
      });
    
  • linux下运行文件
    node httpcreate.js
    创建监听成功可看到打印 “http listen 8181 statr”

  • 浏览器中打开连接(阿里云域名:8181)
    此时可看到http服务传来的数据

参考
https://blog.csdn.net/sleepwalker_1992/article/details/83059042

ps:

开启服务后 ctrl+c可结束服务
而使用ctrl+z,会退出服务,但不会结束监听8181端口,此时再运行文件开启服务会报错:events:167;
此时可调用 netstat -tlnp|grep 8181 (netstat -nap|grep 查看所有)查看端口监听(tcp6也可监听tcp4请求)
再使用 kill -9 (监听id) 命令,结束监听。

二 cocoscreator请求与接收

三 数据库交互

四 数据上传与分析

你可能感兴趣的:(游戏框架探索)