1

What I'm trying to do is integrate my HTML5 canvas games into a single web interface.

When the button is pressed, the html game fills up <div id="game">. But I've only defined my node.js in my javascript file.

Now I can probably define it also in the HTML2 file, but it seems it will create 2 seperate connections.

Is there a way of doing this while having only 1 connection?

Simply put - 2 seperate HTML/javascript files with same exact connection

Thank you

HTML

<div id="game">
    <button id="gameFile">MyGame</button>
</div>

Javascript

var socket = io('http://192.168.0.5:2000');

socket.on('connect', function() {
    socket.emit('login', {

    });
});
$('#gameFile').click(function(event){
     document.getElementById("game").innerHTML='<object type="text/html" data="games/myGame/index.html" style="width:100%; height: 100%;" ></object>';
});

HTML2 (games/myGame/index.html)

<canvas id="myCanvas">
    Your browser does not support the HTML5 canvas tag :(
</canvas>
<script>
    socket.emit('msg', "2nd HTML connected");
</script>
5
  • 1
    You problem statement is conflicting. Are you going to have 1 web application with 2 views, or 2 separate web application? Commented Oct 6, 2016 at 7:17
  • Sorry. 2 seperate applications viewed at once. It's a separate html view embedded inside a general html. Commented Oct 6, 2016 at 7:21
  • 1
    Can't you use the sessionId to determine and join the same room? like, socket.on('connection', function (socket) {socket.join(socket.handshake.sessionID);}); ? Commented Oct 6, 2016 at 7:22
  • The 2nd separate html goes inside the <div id="game"> tag Commented Oct 6, 2016 at 7:22
  • @DavidR I think that is exactly what I'm looking for. Any chance you could give a small quick sample? I'm kind of an amateur Commented Oct 6, 2016 at 7:25

1 Answer 1

2

Possibly You can't just do like this.

You will need to connect the socket in case of any other window. However, you can sync them using the concept of ChatRooms on the server by simply adding all connection pools from a user in a Room.

####Edit###

You can simply join a socket connection to a Chatroom :

io.on('connection', function(socket){
  socket.join('some room');
});

Then Simply broadcast a message to that room members:

io.to('some room').emit('some event'):

You can simply use Room-names to be respective to some user identity.

Ref: http://socket.io/docs/rooms-and-namespaces/#

Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thank you for your answer. Any chance you can provide a quick sample with your answer so I better visually understand? Thank you
You can simply join a socket connection to a Chatroom : io.on('connection', function(socket){ socket.join('some room'); }); Then Simply broadcast a message to that room members: io.to('some room').emit('some event'): You can simply use Room-names to be respective to some user identity. Ref: socket.io/docs/rooms-and-namespaces/#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.