123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- var http = require('http');
- var zlib = require('zlib');
- var events = require('events');
- var util = require('util');
- var TalkingData = function() {
- this.emitter = new events.EventEmitter(this);
- }
- TalkingData.prototype.send = function(
- appID, // String, 是您在TalkingGame中创建游戏时获得的AppID,用来唯一标识您的游戏。
- OS, // String, 玩家此次充值时所用的设备系统平台,只支持格式要求限定的字符串。(ios, android, h5)
- accountID, // String, 玩家的账户ID(与客户端调用SDK的setAccount需传入玩家的accountId类似。)
- orderID, // String, 订单ID,唯一标识一次交易。
- currencyAmount, // Double, 充值金额;
- currencyType, // String, 充值货币类型(人民币CNY;美元USD;新台币TWD);
- virtualCurrencyAmount, // Double, 充值获得的虚拟币额度。
- chargeTime, // Long, 玩家充值发生的时间。
- paymentType, // String, 支付方式(如:支付宝、苹果iap、银联支付、爱贝支付聚合等)。
- gameServer){ // String, 玩家充值的区服。
- var options = {
- method: "POST",
- host: "api.talkinggame.com",
- port: 80,
- encoding : null,
- path: "/api/charge/"+appID,
- headers: { 'Accept-Encoding': 'gzip' }
- };
- var data = {
- 'msgID': orderID,
- 'status': "success",
- 'OS': OS,
- 'accountID': accountID,
- 'orderID': orderID,
- 'currencyAmount': currencyAmount,
- 'currencyType': currencyType,
- 'virtualCurrencyAmount': virtualCurrencyAmount,
- 'chargeTime': chargeTime,
- 'paymentType': paymentType,
- 'gameServer': gameServer,
- 'iapID': null,
- 'partner': null,
- 'gameVersion': null,
- 'level': null,
- 'mission': null,
- }
- var emitter = this.emitter
- zlib.gzip(JSON.stringify([data]), function (err, buffer) {
- var req = http.request(options, function(res) {
- res.on('data', function (body) {
- // ... do stuff with returned data
- zlib.gunzip(body, function(err, dezipped) {
- if (err == null) {
- var str = dezipped.toString('utf-8');
- console.log(str)
- var obj = JSON.parse(str)
- if (obj.code == 100) {
- emitter.emit('end', obj.dataStatus);
- return
- }
- // Make Error object.
- var code = obj.code
- if (code == 101) { err = new Error("TalkingData: 发送数据的IP未被允许(101)");}
- else if (code == 102) { err = new Error("TalkingData: 发送数据的IP未被允许(102)"); }
- else if (code == 103) { err = new Error("TalkingData: 数据不是json格式(103)"); }
- else if (code == 104) { err = new Error("TalkingData: 数据没有经过gzip压缩,解包失败(104)"); }
- else if (code == 105) { err = new Error("TalkingData: 服务器内部错误(105)"); }
- else if (code == 106) { err = new Error("TalkingData: 数据包中数量条数超过1000条限制(106)"); }
- else { err = new Error("TalkingData: 未知错误(" + obj.code + ")") }
- }
- // Error response
- emitter.emit('error', err)
- });
- });
- }).on('error', function(err){
- // Request error
- emitter.emit('error', err)
- });
- // send compressed data
- req.write(buffer);
- req.end();
- });
- return this
- }
- TalkingData.prototype.on = function(eventName, callback) {
- this.emitter.on(eventName, callback);
- return this
- }
- util.inherits(TalkingData, events.EventEmitter); //继承
- module.exports = TalkingData
|