GitHub Documentation Patreon donate button

SessionId is undefined when leaving and joining again.



  • Hi - i've been experimenting with Colyseus for a while, but when I have three tabs open, and close one, and then reopen it and rejoin, the other tabs are claiming that the SessionId is undefined. Is there something wrong with my code? I tried setting a property on the state called uuid which is basically the sessionid, but it still didn't work. Players is an empty array.

    Add player function (im using paperjs):

    function addPlayer(nickname,position,color,uid) {
        console.log("player added as",uid)
        var circle = new Path.Circle(position,20)
        circle.fillColor = color
        console.log(nickname + " has joined the server")
        players[uid] = {
            circle: circle,
            nickname: nickname
        }
        console.log(players[uid])
    } 
    

    State watching function thing:

        addlog("joined succesfully!")
        console.log(room)
        room.onMessage(function(msg) {
            addlog(msg)
        })
        room.state.players.onChange = function(plr,sesh) {
            // it exists and it's not us
            if(players[plr.uuid] && plr.uuid != room.sessionId && plr.uuid) {
                // players[sesh].circle.position.x = plr.x
                // players[sesh].circle.position.y = plr.y
                var thepoint = new Point(plr.x,plr.y)
                var tween = players[plr.uuid].circle.tweenTo(
                    { position: thepoint},
                    { duration: 100, start: false, easing: "linear"}
                );
                tween.start();
                console.log("started tween for",players[plr.uuid].nickname)
            }else{
                console.log("errorrorr")
                console.log(players[plr.uuid], room.sessionId, sesh)
            }
        }
        room.state.players.onAdd = function(plr,session) {
            if(plr.uuid == undefined) {
                alert("sessionid undefined")
            }
            console.log("user is being added!")
            if(plr.uuid != room.sessionId) {
                addPlayer(plr.nickname,new Point(plr.x,plr.y),plr.color,plr.uuid)
                console.log(plr,plr.uuid)
                addlog(players[plr.uuid].nickname + " joined!")
            }
        }
        room.state.players.onRemove = function(plr,session) {
            console.log("user is being removed!")
            if(players[plr.uuid] && plr.uuid) {
                players[plr.uuid].circle.remove()
                addlog(players[plr.uuid].nickname + " left.")
                players[plr.uuid] = undefined
            }
        }
    

    I'm really confused about why this is happening... any ideas?


 

GitHub Documentation Patreon donate button

© 2020 Endel Dreyer