Last active
November 4, 2015 17:53
-
-
Save bcomnes/3b14bb8b371876860631 to your computer and use it in GitHub Desktop.
socket.io example
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Websockets!!!11</title> | |
| </head> | |
| <body> | |
| <button name="button">Click me</button> | |
| <script src="/socket.io/socket.io.js"></script> | |
| <script> | |
| var socket = io(); | |
| socket.on('news', function (data) { | |
| console.log(data); | |
| socket.emit('my other event', { my: 'data' }); | |
| }); | |
| var button = document.querySelector('button') | |
| console.log(button) | |
| button.addEventListener('click', function(e) { | |
| console.log('button clicked') | |
| socket.emit('input', {type: 'button'}) | |
| }) | |
| </script> | |
| </body> | |
| </html> |
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
| var app = require('express')(); | |
| var server = require('http').Server(app); | |
| var io = require('socket.io')(server); | |
| server.listen(3000); | |
| app.get('/', function (req, res) { | |
| res.sendfile(__dirname + '/index.html'); | |
| }); | |
| io.on('connection', function (socket) { | |
| socket.emit('news', { hello: 'world' }); | |
| socket.on('my other event', function (data) { | |
| console.log(data); | |
| }); | |
| socket.on('input', function (data) { | |
| console.log(data) | |
| }) | |
| }); |
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
| { | |
| "name": "rpc", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1", | |
| "start": "nodemon index.js" | |
| }, | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "express": "^4.13.3", | |
| "socket.io": "^1.3.7" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment