express.io icon indicating copy to clipboard operation
express.io copied to clipboard

Rooms example doesn't work

Open ssmereka opened this issue 11 years ago • 2 comments
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. '
    })

ssmereka avatar Apr 17 '14 03:04 ssmereka

+1 io: { route: [Function], broadcast: [Function] }

adimircolen avatar Jun 13 '14 21:06 adimircolen

+1 req.io.join does not exist

1j01 avatar Apr 14 '15 19:04 1j01