2012-11-18 85 views
2

我剛開始學習Node.js,因爲我發現它是創建推送通知服務器的正確工具。Node.js不推送所有通知

所以我得到了這個狡猾的腳本,在這裏我砍死在一起使用一些網絡教程:

// ------------------------------------------------------------ 
// 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; 

在for循環:

// 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個通知出來的5.

我做錯了嗎?

回答

1

蘋果檢查其側推消息複製,以防止垃圾郵件。
嘗試更改郵件內容。

+0

我明白了。我不知道,謝謝。 – Zhang