Created
June 10, 2014 09:03
-
-
Save s10wen/b3cb5eac430b7b5cc86d to your computer and use it in GitHub Desktop.
websocket express.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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