express.io
express.io copied to clipboard
Rooms example doesn't work
trafficstars
I know it says all your examples work, but I think this one has a small error.
The following is code from the rooms example:
Server
app = require('express.io')()
app.http().io()
// Setup the ready route, join room and broadcast to room.
app.io.route('ready', function(req) {
req.io.join(req.data)
req.io.room(req.data).broadcast('announce', {
message: 'New client in the ' + req.data + ' room. '
})
})
// Send the client html.
app.get('/', function(req, res) {
res.sendfile(__dirname + '/client.html')
})
app.listen(7076)
Client
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
io = io.connect()
room = prompt('type a room name')
// Emit ready event with room name.
io.emit('ready', room)
// Listen for the announce event.
io.on('announce', function(data) {
$('body').append('<p>'+data.message+ new Date().toString()+'</p>')
})
</script>
The client is expecting to receive an announce message from the server, however the server is setup to broadcast to everyone but the client. To fix, change this line:
req.io.room(req.data).broadcast('announce', {
message: 'New client in the ' + req.data + ' room. '
})
to this:
app.io.room(req.data).broadcast('announce', {
message: 'New client in the ' + req.data + ' room. '
})
+1 io: { route: [Function], broadcast: [Function] }
+1 req.io.join does not exist