talkingdata.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var http = require('http');
  2. var zlib = require('zlib');
  3. var events = require('events');
  4. var util = require('util');
  5. var TalkingData = function() {
  6. this.emitter = new events.EventEmitter(this);
  7. }
  8. TalkingData.prototype.send = function(
  9. appID, // String, 是您在TalkingGame中创建游戏时获得的AppID,用来唯一标识您的游戏。
  10. OS, // String, 玩家此次充值时所用的设备系统平台,只支持格式要求限定的字符串。(ios, android, h5)
  11. accountID, // String, 玩家的账户ID(与客户端调用SDK的setAccount需传入玩家的accountId类似。)
  12. orderID, // String, 订单ID,唯一标识一次交易。
  13. currencyAmount, // Double, 充值金额;
  14. currencyType, // String, 充值货币类型(人民币CNY;美元USD;新台币TWD);
  15. virtualCurrencyAmount, // Double, 充值获得的虚拟币额度。
  16. chargeTime, // Long, 玩家充值发生的时间。
  17. paymentType, // String, 支付方式(如:支付宝、苹果iap、银联支付、爱贝支付聚合等)。
  18. gameServer){ // String, 玩家充值的区服。
  19. var options = {
  20. method: "POST",
  21. host: "api.talkinggame.com",
  22. port: 80,
  23. encoding : null,
  24. path: "/api/charge/"+appID,
  25. headers: { 'Accept-Encoding': 'gzip' }
  26. };
  27. var data = {
  28. 'msgID': orderID,
  29. 'status': "success",
  30. 'OS': OS,
  31. 'accountID': accountID,
  32. 'orderID': orderID,
  33. 'currencyAmount': currencyAmount,
  34. 'currencyType': currencyType,
  35. 'virtualCurrencyAmount': virtualCurrencyAmount,
  36. 'chargeTime': chargeTime,
  37. 'paymentType': paymentType,
  38. 'gameServer': gameServer,
  39. 'iapID': null,
  40. 'partner': null,
  41. 'gameVersion': null,
  42. 'level': null,
  43. 'mission': null,
  44. }
  45. var emitter = this.emitter
  46. zlib.gzip(JSON.stringify([data]), function (err, buffer) {
  47. var req = http.request(options, function(res) {
  48. res.on('data', function (body) {
  49. // ... do stuff with returned data
  50. zlib.gunzip(body, function(err, dezipped) {
  51. if (err == null) {
  52. var str = dezipped.toString('utf-8');
  53. console.log(str)
  54. var obj = JSON.parse(str)
  55. if (obj.code == 100) {
  56. emitter.emit('end', obj.dataStatus);
  57. return
  58. }
  59. // Make Error object.
  60. var code = obj.code
  61. if (code == 101) { err = new Error("TalkingData: 发送数据的IP未被允许(101)");}
  62. else if (code == 102) { err = new Error("TalkingData: 发送数据的IP未被允许(102)"); }
  63. else if (code == 103) { err = new Error("TalkingData: 数据不是json格式(103)"); }
  64. else if (code == 104) { err = new Error("TalkingData: 数据没有经过gzip压缩,解包失败(104)"); }
  65. else if (code == 105) { err = new Error("TalkingData: 服务器内部错误(105)"); }
  66. else if (code == 106) { err = new Error("TalkingData: 数据包中数量条数超过1000条限制(106)"); }
  67. else { err = new Error("TalkingData: 未知错误(" + obj.code + ")") }
  68. }
  69. // Error response
  70. emitter.emit('error', err)
  71. });
  72. });
  73. }).on('error', function(err){
  74. // Request error
  75. emitter.emit('error', err)
  76. });
  77. // send compressed data
  78. req.write(buffer);
  79. req.end();
  80. });
  81. return this
  82. }
  83. TalkingData.prototype.on = function(eventName, callback) {
  84. this.emitter.on(eventName, callback);
  85. return this
  86. }
  87. util.inherits(TalkingData, events.EventEmitter); //继承
  88. module.exports = TalkingData