Created
February 15, 2013 21:08
-
-
Save lanceharper/4963554 to your computer and use it in GitHub Desktop.
dabble with socket.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
| var app = require('express')(); | |
| app.set('port', process.env.PORT || 80); | |
| var server = require('http').createServer(app) | |
| , io = require('socket.io').listen(server); | |
| server.listen(app.get('port')); | |
| app.get('/', function (req, res) { | |
| res.sendfile(__dirname + '/index.html'); | |
| }); | |
| io.sockets.on('connection', function (socket) { | |
| socket.emit('news', { hello: 'world' }); | |
| socket.on('my other event', function (data) { | |
| console.log(data); | |
| }); | |
| socket.on('message', function(message) { | |
| socket.emit('server-message', message); | |
| }); | |
| }); |
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
| <html> | |
| <head> | |
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
| <script src="/socket.io/socket.io.js"></script> | |
| <script> | |
| $(function() { | |
| var socket = io.connect('http://localhost'); | |
| socket.on('news', function (data) { | |
| console.log(data); | |
| socket.emit('my other event', { my: 'data' }); | |
| }); | |
| socket.on('server-message', function (message) { | |
| $("ul").append("<li>" + message.word + "</li>"); | |
| }); | |
| $('button').on('click', function () { | |
| socket.emit('message', { word: $('#news').val() }); | |
| }); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <input id="news" type="text"/> | |
| <button id="cool">hey</button> | |
| <ul> | |
| </ul> | |
| </body> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment