Skip to content

Instantly share code, notes, and snippets.

@s10wen
Created June 10, 2014 09:03
Show Gist options
  • Save s10wen/b3cb5eac430b7b5cc86d to your computer and use it in GitHub Desktop.
Save s10wen/b3cb5eac430b7b5cc86d to your computer and use it in GitHub Desktop.
websocket express.io
# client.html
```html
<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>
```
# app.js
```js
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)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment