如何在本机cocos2djs应用程序中使用socket.io

时间:2016-02-14 12:03:06

标签: android sockets cocos2d-js

我的项目在网络浏览器中是正常的,HTML5版本正常工作。

但是通过命令编译到apk后cocos运行-p android',socket.io无用服务器端无法接收任何信息。

我知道有些事情是不合适的,但我找不到具体的解决方案。请帮帮我。高兴

server.js如下:

var io = require('socket.io').listen(3000);                                        
console.log('Server on port 3000.');                                            


var player = new Array();
var player_data = {
    name: "",
    kill: 0,
    dead: 0
} 

io.sockets.on('connection', function (socket){                          
console.log("come in");

socket.on('join_success', function (name){
        console.log("name is"+name);
        player_data.name=name;
        player.push(player_data);
        io.sockets.emit("updateAllPlayerData", player);
    });
}); 

app.js如下

var basicLayer = cc.Layer.extend({
ctor: function(){
    this._super();
    var size = cc.director.getWinSize();
    var ball = new cc.Sprite(res.png1);
    ball.x = 100;
    ball.y = 100;
    var ui = new GameUI;

    sio_client= SocketIO.connect("http://localhost:3000");

    sio_client.on("updateAllPlayerData", function(data){
        alert(data);
        playerNum = data.length;
        for(var i=0; i<data.length; i++){
            playerName.push(data[i].name);
            killNum.push(data[i].kill);
            deadNum.push(data[i].dead);
        }
        ui.updateInfo();
    });

    sio_client.on("connect",function(){
        console.log("connect_success ");
        var name=prompt("input your name","");
        while(name==null || name==""){
            name=prompt("input your name","");
        }
        sio_client.emit("join_success", name);
    });


    this.addChild(ball, 10, 100);
    this.addChild(ui,2);
    return true;
}
});

0 个答案:

没有答案