I'm developing an application, which has two functions, join room and create room. When user creates a room, a random hash code(e.g.123) will be generated, which will attach to the URL and create a room with the hash code named(e.g.123) in socket.io. This is successful so far. However, I am fail to boardcast the updated number of clients in the same room. User can click and join a room with hash code(that generated when create a room) provided. When the second user join the room, I would like to update all clients inside the room, which are the one who create room and the one who join the room. However, I can't pass the data to the client. The console states set heartbeat timeout for client XXX something like that.
io.on('connection', function(socket){
var roomCode = "";
socket.on('createRoom',function(data){
roomCode = data;
//create route for the hash URL
app.get('/'+roomCode, function(req, res){
socket.join(roomCode); //join room
res.render('room',{roomNum:roomCode});
});
//response redirect to roomList.ejs
socket.emit('redirect', "https://chess-ivanhoe0523.c9users.io:8081/"+roomCode);
});
socket.on('joinRoom',function(data){
roomCode = data;
socket.join(roomCode);
socket.to(roomCode).emit('PlayerNum','2');
socket.emit('redirect', "https://chess-ivanhoe0523.c9users.io:8081/"+roomCode);
// socket.emit('my PlayerNum', 2);
});
})
app.js
<html>
<head>
<title>Chess Prototype</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="css/index.css">
<!--<script src="../socket.io/socket.io.js"></script>-->
<script src="/socket.io/socket.io.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/index.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Enter room number</h4>
</div>
<div class="modal-body">
<input type="text" class="form-control" id="roomNum">
<br>
<button type="button" class="btn btn-default" onClick="joinRoom()">Join</button>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="character-box">
<h1>Welcome</h1>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Join Game</button>
<button type="button" class="btn btn-success" onClick="createRoomReq()">Create Game</button>
<!--</a>-->
<script>
var socket = io.connect();
function randomHash()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function createRoomReq(){
var randomCode = randomHash();
// alert(randomCode);
socket.emit('createRoom', randomCode);
}
function joinRoom(){
var roomNum = document.getElementById("roomNum").value;
socket.emit('joinRoom', roomNum);
}
socket.on('redirect', function(destination) {
window.location.href = destination;
});
</script>
</a>
</div>
</div>
</div>
</body>
</html>
lobby.ejs
<html>
<head><title>A room</title></head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on('connectToRoom',function(data){
document.body.innerHTML = '';
document.write(data);
});
socket.on('PlayerNum',function(data){
console.log("player num received from client: "data);
document.getelementById("num").innerHTML = data;
});
</script>
<body>
You are in Room <%= roomNum %>
<br>
Num of Players in the room:
<div id="num"></div>
</body>
</html>
room.ejs
via Ivanhoe Cheung
No comments:
Post a Comment