1

我正在使用 Socket.io 编写游戏,但我遇到了问题,当服务器创建新的 img(新角色)时,我只能在一台计算机上看到它。在这种情况下,我想为每台连接的计算机看到相同数量的图像更好如果你看这个谢谢你的每一个帮助

服务器:

    var handler = function(req, res) {
    fs.readFile('./index.html', function (err, data) {
        if(err) throw err;
        res.writeHead(200);
        res.end(data);
    });
}
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
var port =  3250;

app.listen(port);
var postacie = [];
var ile=0;

// socket.io
io.sockets.on('connection', function (socket) {
    postacie[ile]=createChar();
    ile++;
    io.sockets.emit("tworz", postacie);


    socket.on("disconnect", function()
    {
        postacie.splice(0, 1);
        io.sockets.emit("tworz", postacie);
    })

});


function createChar()
{
    var postac = {
        src: "http://img703.imageshack.us/img703/1416/2st.gif",
        id: "character"
    }
    return postac;
}

客户:

<!DOCTYPE html>
<html>
    <head>
        <title>Real tie game</title>
        <script src="socket.io/socket.io.js"></script>
        <script type="text/javascript">
            window.onload = function() {

                var socket = io.connect('http://localhost:3250');

                socket.on("tworz", function(data)
                {
                    for (var i=0; i < data.length; i++) {
                     var element = document.createElement('img'); 
                    element.src = data[i].src;
                    element.id = data[i].id;

                    var body = document.getElementsByTagName('body')
                    body.appendChild(element); 
                    };

                })

            }
        </script>
    </head>
    <body>

    </body>
</html>

它看起来像这样 http://img42.imageshack.us/img42/4793/q10w.jpg

4

1 回答 1

0

我稍微重构了你的代码。希望它可以帮助您朝着正确的方向前进:

主要思想是使用 3 条基本消息:

  1. 加入——当新客户端连接服务器时,将有关新手的信息发送给所有其他客户端;
  2. list -- 当新客户端连接服务器时,向他发送已连接客户端的列表;
  3. leave -- 当有人断开服务器时发送断开客户端的 id

服务器:

var handler = function(req, res) {
    fs.readFile('./index.html', function (err, data) {
        if(err) throw err;
        res.writeHead(200);
        res.end(data);
    });
}

var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
var port =  3250;

app.listen(port);
var postacie = [];
var idGenerator = 0;

// socket.io
io.sockets.on('connection', function (socket) {
    var pId = idGenerator++;
    socket.emit("list", postacie);

    var postac = createChar(pId);
    postacie.push(postac);
    io.sockets.emit("join", postac);

    socket.on("disconnect", function()
    {
        var sId = "character" + pId;
        for (var i = 0,n=postacie.length; i < n; ++i) {
            var postac = postacie[i];
            if (postac.id == sId) {
                io.sockets.emit("leave", {id:postac.id});
                postacie.splice(i, 1);
                break;
            }
        }
    });

});


function createChar(id)
{
    var postac = {
        src: "http://img703.imageshack.us/img703/1416/2st.gif",
        id: "character" + id
    };
    return postac;
}

客户:

<!DOCTYPE html>
<html>
    <head>
        <title>Real tie game</title>
        <script src="/socket.io/socket.io.js"></script>
        <script type="text/javascript">
            window.onload = function() {

                var socket = io.connect('http://localhost:3250');

                socket.on("join", function(data)
                {
                    var element = document.createElement('img');
                    element.src = data.src;
                    element.id = data.id;

                    var body = document.getElementsByTagName('body')[0];
                    body.appendChild(element);

                });
                socket.on("list", function(data)
                {
                    for (var i=0; i < data.length; i++) {
                      var element = document.createElement('img');
                      element.src = data[i].src;
                      element.id = data[i].id;

                      var body = document.getElementsByTagName('body')[0];
                      body.appendChild(element);
                    };

                });
                socket.on("leave", function(data)
                {
                    var element = document.getElementById(data.id);
                    if (element)
                        element.parentNode.removeChild(element);
                });

            }
        </script>
    </head>
    <body>

    </body>
</html>
于 2013-06-29T07:54:01.870 回答