使用android中的node.js在两个用户之间创建私人聊天

时间:2014-11-12 04:39:11

标签: javascript android node.js socket.io

我想使用node.js和socket.io创建简单的android应用程序来创建聊天消息。我创建了node.js,但问题是它是向连接到该服务器的所有用户发送消息。我只想在两个用户之间进行私聊。为此,我想创建node.js,我是新手,所以请帮助我。这是我连接的所有用户的node.js文件。

var io  = require('socket.io');  
var server = io.listen(4732);

var macbook_socket = undefined;

server.sockets.on('connection', function(socket) {  
var ipAddress = socket.handshake.address.address;
    socket.emit('chat', {name: ipAddress, text: 'joined chat'});
    //socket.emit('helo', {msg: 'welcome'});
    socket.on('user', function(data) {
        if (data.type == "client") {
            //saving socket
            macbook_socket = socket;
        }
    });


    socket.on("next", function(data) {
        console.log("received next");
        if (democlientsocket != undefined) {
            democlientsocket.emit("next", {});
        }           
    });
    socket.on('chat1', function(data){
            socket.broadcast.emit('chat1', {name: data.name, text: data.text});
      });
    socket.on("prev", function(data) {
        console.log("received prev");
        if (democlientsocket != undefined) {
            democlientsocket.emit("prev", {});
        }
    });
});

0 个答案:

没有答案