Nodejs服务器和android客户端

时间:2014-09-09 07:37:05

标签: android node.js

我有一个简单的nodejs服务器:

"use strict";

var http = require("http");

var server = http.createServer(processRequest).listen(3001, "192.168.1.4");

function processRequest(request, response) {

    response.writeHead(200, {
        "Content-Type": "text/html"
    });

    response.write("Simple HTML Page");

    response.end();

}


// Buradan sonrası yeni
var io = require("socket.io").listen(server);

io.sockets.on('connection', function (socket) {
    sendMessage(socket);
    console.log("User Connected");

    socket.on("disconnect", function () {

        console.log("User Disconnected");
    });
});

function sendMessage(socket) {

    setTimeout(function () {
        socket.emit("test");

        sendMessage(socket);
    }, 5000);
}

我想通过nodejs从mysql服务器(远程)获取数据并向android手机广播。当新数据在db中添加时,手机会显示新数据。 我如何与Android客户端通信?

1 个答案:

答案 0 :(得分:0)

您需要使用GCM

流程将是这样的:

             JSON Package
node Server --------------> GCM Server ---> Android Device
                                                   |
                               (OS notifies app)   |
                                                   V
                                App Connects to node and fetches the data