Skip to content

Instantly share code, notes, and snippets.

@lanceharper
Created February 15, 2013 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanceharper/4963554 to your computer and use it in GitHub Desktop.
Save lanceharper/4963554 to your computer and use it in GitHub Desktop.
dabble with socket.io
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);
});
});
<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