无法使用socket.io

时间:2017-02-23 00:10:03

标签: javascript node.js socket.io

我开始在socket.io和节点js。 我正在尝试制作一款多人“摇滚剪刀”游戏。 这是我的server.js

var app = require('express')(),
server = require('http').createServer(app)
io = require('socket.io').listen(server),
fs = require('fs');

app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});

function GameServer(){
this.p1 = ""
this.p2 = "";
this.choice1 = "";
this.choice2 = "";
this.countPlayer = 0;
}

GameServer.prototype = {
addPlayer: function(player){
    if(this.countPlayer < 1)
        this.p1 = player.name;
    else
        this.p2 = player.name;
},
addChoice: function(player){
    if(this.p1 == player.name)
        this.choice1 = player.choice;
    else if(this.p2 == player.name)
        this.choice2 = player.choice;
},
getData: function(data){
    //var gameData = {};
    if(this.p1 =="")
        this.p1 = data.p1;
    else if(this.p1 !="" && this.p2=="")
        this.p2 = data.p2;        
    if(this.choice1 =="")
        this.choice1 = data.choice1;
    else if(this.choice1 !="" && this.choice2=="")
        this.choice2 = data.choice2;
}
}

var game = new GameServer();

/* Connection events */
io.on('connection', function(client) {
client.on('ClientInfoGame', function(player){
    console.log('User '+player.name+' connected');
    console.log('he chose '+player.choice);
    game.getData(player);
});
console.log("on passe au emit")
client.emit('ServerInfoGame', {p1:game.p1,p2:game.p2,choice1:game.choice1,choice2:game.choice})
});

server.listen(8888); 

这是我的index.html和我的javascript代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Super Chat temps réel !</title>
        <style>
            #zone_chat strong {
                color: white;
            background-color: black;
            padding: 2px;
        }
    </style>
</head> 
<body>
    <h1>JANKEN GAME</h1>
    <form action="/" method="post">
        <input type="submit" id="r" value="Rock" />
        <input type="submit" id="p" value="Paper" />
        <input type="submit" id="s" value="Sissor" />
    </form>
    <h2 class="result"> Make a choice</h2> 
    <section id="zone_chat">
    </section>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
    function player(nom,choix,socket){
        this.name=nom;
        this.choice = choix;
        this.opponentChoice=""
        this.socket=socket
    }
    player.prototype.sendDataToServer = function() {
        //we send data from client to server
        //var data = this;
        this.socket.emit("ClientInfoGame",{name:this.name,choice:this.choice});
    };
    player.prototype.getDataFromServer = function() {
        //we get data from server 
        this.socket.on("ServerInfoGame",function(dataFromServer){
            var data = dataFromServer;
            if(data.p1 == this.name)
                this.opponentChoice = data.choice2;
            else if (data.p2 == this.name)
                this.opponentChoice = data.choice1;
        })
    };
    player.prototype.winnerIs = function() {
        console.log("opponnent choice ..."+ this.opponentChoice)

        if(this.choice == "Rock" && this.opponentChoice == "Rock" || this.choice == "Paper" && this.opponentChoice == "Paper" || this.choice == "Sissor" && this.opponentChoice == "Sissor" )
            return "Draaaaww , try again";   
        else if(this.choice == "Rock" && this.opponentChoice == "Sissor" || this.choice == "Paper" && this.opponentChoice == "Rock" || this.choice == "Sissor" && this.opponentChoice == "Paper" )
            return " YOU WIIIIIINNN ";  
        else if(this.opponentChoice == "Rock" && this.choice == "Sissor" || this.opponentChoice == "Paper" && this.choice == "Rock" || this.opponentChoice == "Sissor" && this.choice == "Paper" )
            return " YOU LOOOOOOSE ";
    };
    function end(p){
        $('h1.result').empty(); 
        $('h1.result').text(p.winnerIs()).html();
    }

        var choice = "";
        $('#r').on('click', function(){choice = "Rock"; p.choice = choice;})
        $('#p').on('click', function(){choice = "Paper"; p.choice = choice;})
        $('#s').on('click', function(){choice = "Sissor"; p.choice = choice;})
        var pseudo = prompt('What's your name?');
        // Connexion à socket.io
        var socket = io.connect('http://localhost:8888');
        var p = new player(pseudo,choice,socket);
        //document.title = pseudo + ' - ' + document.title;
//            socket.emit('choice', choice)
        //socket.emit("infoPlayer",p)
        p.sendDataToServer();
        p.getDataFromServer();            
        end(p);
    </script>
</body>

在我的客户端(index.html),我创建一个具有名称和选择属性(摇滚,纸张或剪刀)的玩家对象。 当玩家点击一个按钮时,它会改变选择值,然后将玩家对象发送到服务器。

在我的服务器端,我从每个客户端获取值,将其添加到我的gameServer对象并将其发送给所有客户端,然后客户端将应用游戏逻辑来确定谁输赢。

当我与用户连接时,我的服务器上有一条日志消息,但我在浏览器上收到了此消息。 “不能POST /”。 我不知道为什么我得到这个,在我的服务器上我的gameServer属性是空的,我不明白为什么我从客户端发出的数据没有保存在我的服务器中。

你有什么想法吗?

0 个答案:

没有答案