GitHub | Documentation | Become a Patron

How to push a variable?



  • I have the following:

    class ChatRoom extends Room {

    constructor () {
    super();

    this.setState({
      players: {},
      messages: [],
      roomname: []
    });
    

    }

    In onJoin(client)...

    //it works if I do this, I can retrieve the string in Unity.
    this.state.roomname.push ('a room passed as string');

    var roomname = {
    roomName : 'a room passed as string'
    }

    //it doesn't work if I do this, I get a '1' as the result when i try to read the object.
    this.state.roomname.push (roomname);

    How do I push a variable?
    I'm thinking like in socket.io . I can send currentUser, in Unity I use a Json deserialiser to read the contents.

    var currentUser = {
    		msg:data.msg,
    		id:id,
    		position:data.position,
    	}
    socket.emit('move', currentUser);
    

    Can I do something similar using this.state.roomname.push(currentUser) ?



  • My solution to this is using JSON.stringify to parse the variable into a string. I'm hoping there is a better solution.

    var roomname = {
    roomName : 'a room passed as string'
    }
    var text = JSON.stringify (roomname);
    console.log("stringify text: " + text);

       this.state.roomname.push (text);