2012-11-18 2 views
2

푸시 알림 서버를 만드는 작업에 적합한 도구로 Node.js를 배우기 시작했습니다.Node.js가 모든 알림을 푸시하지 않음

// try sending multiple copies of the notification in one stream 
for(var j = 0; j < 5; j++) 
{ 
    var writable = stream.write(buffer); // write push notification to socket stream (send it out) 
} 

내가 중 최대 2 알림을 수신 할 경우에만 할 수 있었다 : for 루프에서

// ------------------------------------------------------------ 
// HTTP Server stack using Node.js 
// ------------------------------------------------------------ 

// import the HTTP module that ships with Node.js 
var http = require("http"); 
var url = require("url"); 

// push notification helper modules 
var crypto = require("crypto"); 
var tls = require("tls"); 
var fs = require("fs"); 
var stream = null; 

process.chdir(__dirname); 

//connect(function(){}); 

function connect(next) 
{ 
    filepath = 'cert_and_key_dev.pem'; 
    var certPem = fs.readFileSync(filepath, encoding='ascii'); 
    var keyPem = fs.readFileSync(filepath, encoding='ascii'); 
    var options = { key: keyPem, cert: certPem }; 

    var apnshost_dev = 'gateway.sandbox.push.apple.com'; 
    var apnsport = 2195; 

    stream = tls.connect(apnsport, apnshost_dev, options, function() { 
     // connected 
     console.log('I am connected to APNS, WOO HOO!'); 
     //next(!stream.authorized, stream); 

     pushTest(); 
    }); 
} 

function pushTest() 
{ 
    var pushnd = { aps: { alert: 'This is a test' }, customParam: { foo: 'bar' } } // aps is required 
    var hextoken = '...my push token here....'; 

    // construct the protocol data unit 
    var payload = JSON.stringify(pushnd); 
    var payloadlen = Buffer.byteLength(payload, 'utf-8'); // encoded UTF-8 string length, max 255 bytes 
    var tokenlen = 32; 
    var buffer = new Buffer(1 + 4 + 4 + 2 + tokenlen + 2 + payloadlen); 
    var i = 0; 
    buffer[i++] = 1; // command 
    var msgid = 0xbeefcace; // message identifier, can be left 0 
     buffer[i++] = msgid >> 24 & 0xFF; 
    buffer[i++] = msgid >> 16 & 0xFF; 
     buffer[i++] = msgid >> 8 & 0xFF; 
    buffer[i++] = msgid & 0xFF; 

    // expiry in epoch seconds (1 hour) 
    var seconds = Math.round(new Date().getTime()/1000) + 1*60*60; // expire in epoch seconds (1 hour) 
    buffer[i++] = seconds >> 24 & 0xFF; 
    buffer[i++] = seconds >> 16 & 0xFF; 
    buffer[i++] = seconds >> 8 & 0xFF; 
    buffer[i++] = seconds & 0xFF; 

    buffer[i++] = tokenlen >> 8 & 0xFF; // token length 
    buffer[i++] = tokenlen & 0xFF; 
    token = hextobin(hextoken); 
    token.copy(buffer, i, 0, tokenlen); 
    i += tokenlen; 

    buffer[i++] = payloadlen >> 8 & 0xFF; // payload length 
    buffer[i++] = payloadlen & 0xFF; 

    payload = Buffer(payload); 
    payload.copy(buffer, i, 0, payloadlen); 

    var j = 0; 

    // try sending multiple copies of the notification in one stream 
    for(var j = 0; j < 5; j++) 
    { 
     var writable = stream.write(buffer); // write push notification to socket stream (send it out) 
    } 

    console.log('Test notification sent!'); 

    // Handling error messages 
    stream.on('data', function(data) { 
     var command = data[0] & Ox0FF // always 8 
     var status = data[1] & 0x0FF // error code 
     var msgid = (data[2] << 24) + (data[3] << 16) + (data[4] << 8) + (data[5]); 

     console.log(command + ':' + status + ':' + msgid); 
    }); 
} 

function hextobin(hexstr) 
{ 
    buf = new Buffer(hexstr.length/2); 

    for(var i = 0; i < hexstr.length/2; i++) 
    { 
     buf[i] = (parseInt(hexstr[i * 2], 16) << 4) + (parseInt(hexstr[i * 2 + 1], 16)); 
    } 

    return buf; 
} 

function start(route, handle) 
{ 
    function onRequest(request, response) 
    { 
     // using url module to handle routing and mapping 
     // of each request 
     var pathname = url.parse(request.url).pathname; 
     console.log("Request for " + pathname + " received."); 
     route(handle, pathname, response, request); 

     /* 
     request.setEncoding("utf8"); 


     // POST data callback function for each chunk of data 
     request.addListener("data", function(postDataChunk) { 
      postData += postDataChunk; 
      console.log("Received POST data chunk '" + 
      postDataChunk + "' ."); 
     }); 

     // POST data callback function for completion of data download 
     request.addListener("end", function() { 
      route(handle, pathname, response, postData); 
     }); 

     // depedency injective response object to allow 
     // request handler to use it 
     route(handle, pathname, response); 
     */ 
    } 

    http.createServer(onRequest).listen(8888); 
    console.log("Server has started."); 
} 

exports.start = start; 
exports.connect = connect; 

:

그래서 나는 약간의 인터넷 튜토리얼을 사용하여 함께 해킹 여기 사기 스크립트를 가지고 the 5

내가 잘못하고 있니?

답변

1

Apple은 스팸을 방지하기 위해 푸시 메시지 복제가 있는지 확인합니다.
메시지 내용을 변경하십시오.

+0

알 수 있습니다. 고마워. 고마워. – Zhang

관련 문제