简体   繁体   中英

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? I have tried a while statement, and I end up with "call stack exceeded" exception. 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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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