简体   繁体   English

socket.io:如何确保设置值

[英]socket.io: how to ensure a value is set

I have code that is relying on other code to be set before proceding. 我有代码依赖于在程序之前设置的其他代码。 How can I limit the dom to wait until stuff has been sent from the server before continuing? 如何限制dom等待直到从服务器发送内容才继续? I have tried a while statement, and I end up with "call stack exceeded" exception. 我尝试了一个while语句,最终得出“超出调用堆栈”的异常。 I have also tried setTimeOut, and while render() will function, I will still get an exception within render stating that selfId isn't defined yet. 我也试过setTimeOut,而render()将起作用,我仍然会在render中得到一个异常,说明selfId还没有定义。 How can I get around this stuff? 我该如何解决这些问题?

Example: Client: 示例:客户:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
        <link href="client/main.css" rel="stylesheet"</link>
    </head>
    <body>
        <div id="game">
            <!--GAMEBOARD-->
            <canvas id="ctx"></canvas>
            <!--CHATUI-->
            <div id="chat" class="chat">
                <div id="messages" class="messages">
                    <div>Welcome to Simon's Game Server</div>
                </div>
                <input id="chatinput" class="chatinput"></input>
            </div>
            <div id="fps"></div>
            <!--ADDITIONALUI-->
        </div>
        <script>
            var socket = io();
            //game loop
            var lastTime;
            function gameLoop() {

            }

            //init
            var selfId = null;   //How do i set this variable when the first user connects?  This is my question
            socket.on('setSelf', function (data) { //this is my attempt, but it is not working for the first user.
                console.log('Setting selfId to: ' + data.id);
                selfId = data.id;
                gameLoop();
            });

        </script>
    </body>
</html>

Server: 服务器:

var express = require('express');
var app = express();
var server = require('http').Server(app);
var port = 1338;
var io = require('socket.io')(server);

//routes
app.get('/', function (req, res) {
    res.sendFile(__dirname + '/client/index.html');
});
app.use('/client', express.static(__dirname + '/client'));

server.listen(port);


var UNIQUEID = 0;
io.sockets.on('connection', function (socket) {
    socket.myid = UNIQUEID;
    UNIQUEID++;
    socket.emit('setSelf', { id: socket.myid }); 
});

In Javascript, you can't "Wait" or "Sleep" until something happens. 在Javascript中,在发生某些事情之前,您不能“等待”或“睡眠”。 It doesn't work that way. 它不起作用。 Instead, you register some sort of event handler and when that happens you trigger the code in question. 相反,您注册了某种事件处理程序,当发生这种情况时,您会触发相关代码。 In your case, you can do that like this: 在你的情况下,你可以这样做:

var selfId = null;
socket.on('setSelf', function (data) {
     selfId = data.id;
     // don't call gameLoop until you get here and selfId is set
     gameLoop();
});

function gameLoop() {
    render();
    //....
    playerState();
    //
    //etc ....
    //
}

Also, on the server, you should not be overwriting socket.id as socket.io coins its own id and uses that in its own housekeeping. 另外,在服务器上,你不应该覆盖socket.id因为socket.io会将自己的id硬币化,并在自己的内务中使用它。 If you want to store something as a property on the socket object, I'd suggest you create your own unique, non-conflicting property name such as .myId instead. 如果您想在socket对象上存储某些属性,我建议您创建自己唯一的,不冲突的属性名称,例如.myId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM