Created
April 4, 2012 04:46
-
-
Save smith/2297857 to your computer and use it in GitHub Desktop.
Ping with socket.io
This file contains 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
<input placeholder="ping something" /><button>ping!</button> | |
<pre></pre> | |
<script src="//localhost:8888/socket.io/socket.io.js"></script> | |
<script> | |
(function () { | |
var socket = io.connect("//localhost:8888"), | |
pre = document.querySelectorAll("pre")[0], | |
input = document.querySelectorAll("input")[0], | |
button = document.querySelectorAll("button")[0]; | |
button.addEventListener("click", function () { | |
socket.emit('ping', input.value); | |
}); | |
socket.on("pong", function (data) { | |
pre.innerHTML += data; | |
}); | |
})(); | |
</script> |
This file contains 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 io = require('socket.io').listen(8888), | |
ch = require('child_process'); | |
io.sockets.on('connection', function (socket) { | |
socket.on('ping', function (data) { | |
var ping = ch.spawn('ping', [data]); | |
ping.stdout.on("data", function (data) { | |
socket.emit("pong", data.toString()); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment